diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 436109114c..3c3b8e381b 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -32,7 +32,7 @@ function openGraphHelper(options = {}) { const twitterCard = options.twitter_card || 'summary'; const date = options.date !== false ? options.date || page.date : false; const updated = options.updated !== false ? options.updated || page.updated : false; - const language = options.language || page.lang || page.language || config.language; + let language = options.language || page.lang || page.language || config.language; const author = options.author || config.author; if (!Array.isArray(images)) images = [images]; @@ -84,7 +84,14 @@ function openGraphHelper(options = {}) { } if (language) { - result += og('og:locale', language); + if (language.length === 5) { + const territory = language.slice(-2); + const territoryRegex = new RegExp(territory.concat('$')); + + language = language.replace('-', '_').replace(territoryRegex, territory.toUpperCase()); + + result += og('og:locale', language); + } } images = images.map(path => { diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 9c9d15ef0b..980fa73de0 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -35,7 +35,6 @@ describe('open_graph', () => { meta({property: 'og:title', content: hexo.config.title}), meta({property: 'og:url'}), meta({property: 'og:site_name', content: hexo.config.title}), - meta({property: 'og:locale', content: 'en'}), meta({property: 'article:published_time', content: post.date.toISOString()}), meta({property: 'article:modified_time', content: post.updated.toISOString()}), meta({property: 'article:author', content: hexo.config.author}), @@ -614,7 +613,7 @@ describe('open_graph', () => { is_post: isPost }, {language: 'es-cr'}); - result.should.contain(meta({property: 'og:locale', content: 'es-cr'})); + result.should.contain(meta({property: 'og:locale', content: 'es_CR'})); }); it('og:locale - page.lang', () => { @@ -624,7 +623,7 @@ describe('open_graph', () => { is_post: isPost }); - result.should.contain(meta({property: 'og:locale', content: 'es-mx'})); + result.should.contain(meta({property: 'og:locale', content: 'es_MX'})); }); it('og:locale - page.language', () => { @@ -634,7 +633,7 @@ describe('open_graph', () => { is_post: isPost }); - result.should.contain(meta({property: 'og:locale', content: 'es-gt'})); + result.should.contain(meta({property: 'og:locale', content: 'es_GT'})); }); it('og:locale - config.language', () => { @@ -646,7 +645,19 @@ describe('open_graph', () => { is_post: isPost }); - result.should.contain(meta({property: 'og:locale', content: 'es-pa'})); + result.should.contain(meta({property: 'og:locale', content: 'es_PA'})); + }); + + it('og:locale - convert territory to uppercase', () => { + hexo.config.language = 'fr-fr'; + + const result = openGraph.call({ + page: {}, + config: hexo.config, + is_post: isPost + }); + + result.should.contain(meta({property: 'og:locale', content: 'fr_FR'})); }); it('og:locale - no language set', () => { @@ -659,6 +670,18 @@ describe('open_graph', () => { result.should.not.contain(meta({property: 'og:locale'})); }); + it('og:locale - language is not in lang-territory format', () => { + hexo.config.language = 'en'; + + const result = openGraph.call({ + page: {}, + config: hexo.config, + is_post: isPost + }); + + result.should.not.contain(meta({property: 'og:locale'})); + }); + it('article:author - options.author', () => { const result = openGraph.call({ page: {},