Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add og:locale support #166

Merged
merged 13 commits into from
Apr 4, 2017
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/pkg/
/spec/reports/
/tmp/
/bin/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the RSpec install recommendation which created that directory. I didn’t want to bundle it with the repo.

*.gem
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ webmaster_verifications:
yandex: 1234
```

* `lang` - The locale these tags are marked up in. Of the format `language_TERRITORY`. Default is `en_US`.

The SEO tag will respect the following YAML front matter if included in a post, page, or document:

* `title` - The title of the post, page, or document
* `description` - A short description of the page's content
* `image` - URL to an image associated with the post, page, or document (e.g., `/assets/page-pic.jpg`)
* `author` - Page-, post-, or document-specific author information (see below)
* `lang` - Page-, post-, or document-specific language information

## Advanced usage

Expand Down
5 changes: 4 additions & 1 deletion lib/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
{% assign seo_page_image = seo_page_image | escape %}
{% endif %}

{% assign seo_page_lang = page.lang | default: site.lang | default: "en_US" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know about default values here, but I suppose I don't have a strong preference either way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be some sort of string normalization applied with maybe a | replace: '-', '_'?

For example it's possible to have a site.lang of en-US for the lang attribute in <html lang="...">. Same for xml:lang in an Atom feed <feed xmlns="http://www.w3.org/2005/Atom" xml:lang="...">.

In both cases it appears they expect values of language-REGION, e.g. en-US, where as og:locale wants en_US with an underscore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I can make that happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And done (assuming I didn’t do anything to annoy Rubocop).


{% if seo_tag.title and seo_title %}
<title>{{ seo_title }}</title>
{% endif %}
Expand All @@ -109,6 +111,8 @@
<meta name="author" content="{{ seo_author_name }}" />
{% endif %}

<meta property="og:locale" content="{{ seo_page_lang | replace:'-','_' }}" />

{% if seo_description %}
<meta name="description" content="{{ seo_description }}" />
<meta property="og:description" content="{{ seo_description }}" />
Expand All @@ -124,7 +128,6 @@
{% endif %}

{% if seo_page_image %}

<meta property="og:image" content="{{ seo_page_image }}" />
{% if page.image.height %}
<meta property="og:image:height" content="{{ page.image.height }}" />
Expand Down
35 changes: 35 additions & 0 deletions spec/jekyll_seo_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
<!-- Begin Jekyll SEO tag v#{version} -->
<title>Foo</title>
<meta property="og:title" content="Foo" />
<meta property="og:locale" content="en_US" />
<link rel="canonical" href="http://example.invalid/page.html" />
<meta property="og:url" content="http://example.invalid/page.html" />
<meta property="og:site_name" content="Foo" />
Expand Down Expand Up @@ -581,4 +582,38 @@
end
end
end

context "with locale" do
it "uses en_US when no locale is specified" do
expected = %r!<meta property="og:locale" content="en_US" />!
expect(output).to match(expected)
end

context "with site.lang" do
let(:site) { make_site("lang" => "en_US") }

it "uses site.lang if page.lang is not present" do
expected = %r!<meta property="og:locale" content="en_US" />!
expect(output).to match(expected)
end

context "with page.lang" do
let(:page) { make_page("lang" => "en_UK") }

it "uses page.lang if both site.lang and page.lang are present" do
expected = %r!<meta property="og:locale" content="en_UK" />!
expect(output).to match(expected)
end
end
end

context "with site.lang hyphenated" do
let(:site) { make_site("lang" => "en-US") }

it "coerces hyphen to underscore" do
expected = %r!<meta property="og:locale" content="en_US" />!
expect(output).to match(expected)
end
end
end
end