Skip to content

Commit

Permalink
fix(i18n): use default language when no language set (#3069)
Browse files Browse the repository at this point in the history
Merge pull request #3069 from bobd91/default-language

Fixes #3070
  • Loading branch information
JLHwung authored Mar 12, 2018
2 parents 3672fff + 65301af commit f9230cc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
14 changes: 4 additions & 10 deletions lib/plugins/filter/template_locals/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function i18nLocalsFilter(locals) {
const page = locals.page;
let lang = page.lang || page.language;
const i18nLanguages = i18n.list();
const i18nConfigLanguages = i18n.languages;

if (!lang) {
const pattern = new Pattern(`${i18nDir}/*path`);
Expand All @@ -20,26 +21,19 @@ function i18nLocalsFilter(locals) {
lang = data.lang;
page.canonical_path = data.path;
} else {
lang = getFirstLanguage(config.language);
// i18n.languages is always an array with at least one argument ('default')
lang = i18nConfigLanguages[0];
}

page.lang = lang;
}

page.canonical_path = page.canonical_path || locals.path;

const languages = _([].concat(lang, i18nLanguages)).compact().uniq().value();
const languages = _([].concat(lang, i18nConfigLanguages)).compact().uniq().value();

locals.__ = i18n.__(languages);
locals._p = i18n._p(languages);
}

module.exports = i18nLocalsFilter;

function getFirstLanguage(lang) {
if (Array.isArray(lang)) {
return lang[0];
}

return lang;
}
54 changes: 50 additions & 4 deletions test/scripts/filters/i18n_locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ describe('i18n locals', () => {
var i18n = theme.i18n;

// Default language
hexo.config.language = 'en';
i18n.languages = ['en', 'default'];

// Fixtures
i18n.set('de', {
Home: 'Zuhause'
});

i18n.set('default', {
Home: 'Default Home'
});

i18n.set('en', {
Home: 'Home'
});
Expand Down Expand Up @@ -88,8 +96,8 @@ describe('i18n locals', () => {
});

it('use config by default - with multiple languages, first language should be used', () => {
var oldConfig = hexo.config.language;
hexo.config.language = ['zh-tw', 'en'];
var oldConfig = i18n.languages;
i18n.languages = ['zh-tw', 'en', 'default'];

var locals = {
config: hexo.config,
Expand All @@ -103,6 +111,44 @@ describe('i18n locals', () => {
locals.page.canonical_path.should.eql('index.html');
locals.__('Home').should.eql('首頁');

hexo.config.language = oldConfig;
i18n.languages = oldConfig;
});

it('use config by default - with no languages, default language should be used', () => {
var oldConfig = i18n.language;
i18n.languages = ['default'];

var locals = {
config: hexo.config,
page: {},
path: 'index.html'
};

i18nFilter(locals);

locals.page.lang.should.eql('default');
locals.page.canonical_path.should.eql('index.html');
locals.__('Home').should.eql('Default Home');

i18n.languages = oldConfig;
});

it('use config by default - with unknown language, default language should be used', () => {
var oldConfig = i18n.languages;
i18n.languages = ['fr', 'default'];

var locals = {
config: hexo.config,
page: {},
path: 'index.html'
};

i18nFilter(locals);

locals.page.lang.should.eql('fr');
locals.page.canonical_path.should.eql('index.html');
locals.__('Home').should.eql('Default Home');

i18n.languages = oldConfig;
});
});

0 comments on commit f9230cc

Please sign in to comment.