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

Use default language when no language set #3069

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});
});