Skip to content

Commit

Permalink
fix(open_graph): set default locales
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Dec 10, 2019
1 parent 979d1f4 commit 9aceb2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
22 changes: 21 additions & 1 deletion lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
const urlFn = require('url');
const moment = require('moment');
const { encodeURL, htmlTag, stripHTML, escapeHTML } = require('hexo-util');
const localeMap = {
'en': 'en_US',
'de': 'de_DE',
'es': 'es_ES',
'fr': 'fr_FR',
'hu': 'hu_HU',
'id': 'id_ID',
'it': 'it_IT',
'ja': 'ja_JP',
'ko': 'ko_KR',
'nl': 'nl_NL',
'ru': 'ru_RU',
'th': 'th_TH',
'tr': 'tr_TR',
'vi': 'vi_VN'
};
const localeRegex = new RegExp(Object.keys(localeMap).join('|'), 'i');

function meta(name, content) {
return `${htmlTag('meta', {
Expand Down Expand Up @@ -79,7 +96,10 @@ function openGraphHelper(options = {}) {
}

if (language) {
if (language.length === 5) {
if (language.length === 2) {
language = language.replace(localeRegex, str => localeMap[str]);
result += og('og:locale', language);
} else if (language.length === 5) {
const territory = language.slice(-2);
const territoryRegex = new RegExp(territory.concat('$'));

Expand Down
4 changes: 2 additions & 2 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ describe('open_graph', () => {
result.should.not.contain(meta({property: 'og:locale'}));
});

it('og:locale - language is not in lang-territory format', () => {
it('og:locale - language is not in lang-TERRITORY format', () => {
hexo.config.language = 'en';

const result = openGraph.call({
Expand All @@ -680,7 +680,7 @@ describe('open_graph', () => {
is_post: isPost
});

result.should.not.contain(meta({property: 'og:locale'}));
result.should.contain(meta({property: 'og:locale', content: 'en_US'}));
});

it('article:author - options.author', () => {
Expand Down

0 comments on commit 9aceb2f

Please sign in to comment.