0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00
ghost/test/unit/shared/i18n_spec.js
Hannah Wolfe e1699afc77 Refactored i18n so all logic is in the base class
- preparation for moving the base class out of Ghost
- refactored so that all the logic for file loading and fallbacks live in the base class
- theme i18n now only overrides init with the properties it needs, filepath generation and error handling
- this makes it much easier to move the i18n file out, and eventually have theme i18n live elsewhere too
- also prepares for using DI for logging
2021-05-05 20:29:34 +01:00

85 lines
2.7 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const I18n = require('../../../core/shared/i18n').I18n;
describe('I18n Class Behaviour', function () {
it('defaults to en', function () {
const i18n = new I18n();
i18n.locale().should.eql('en');
});
it('can have a different locale set', function () {
const i18n = new I18n({locale: 'fr'});
i18n.locale().should.eql('fr');
});
describe('file loading behaviour', function () {
it('will fallback to en file correctly without changing locale', function () {
const i18n = new I18n({locale: 'fr'});
let fileSpy = sinon.spy(i18n, '_readTranslationsFile');
i18n.locale().should.eql('fr');
i18n.init();
i18n.locale().should.eql('fr');
fileSpy.calledTwice.should.be.true();
fileSpy.secondCall.args[0].should.eql('en');
});
});
describe('translation key dot notation (default behaviour)', function () {
const fakeStrings = {
test: {string: {path: 'I am correct'}}
};
let i18n;
beforeEach(function initBasicI18n() {
i18n = new I18n();
sinon.stub(i18n, '_loadStrings').returns(fakeStrings);
i18n.init();
});
it('correctly loads strings', function () {
i18n._strings.should.eql(fakeStrings);
});
it('correctly uses dot notation', function () {
i18n.t('test.string.path').should.eql('I am correct');
});
it('uses key fallback correctly', function () {
i18n.t('unknown.string').should.eql('An error occurred');
});
it('errors for invalid strings', function () {
should(function () {
i18n.t('unknown string');
}).throw('i18n.t() called with an invalid key: unknown string');
});
});
describe('translation key fulltext notation (theme behaviour)', function () {
const fakeStrings = {'Full text': 'I am correct'};
let i18n;
beforeEach(function initFulltextI18n() {
i18n = new I18n({stringMode: 'fulltext'});
sinon.stub(i18n, '_loadStrings').returns(fakeStrings);
i18n.init();
});
it('correctly loads strings', function () {
i18n._strings.should.eql(fakeStrings);
});
it('correctly uses fulltext with bracket notation', function () {
i18n.t('Full text').should.eql('I am correct');
});
it('uses key fallback correctly', function () {
i18n.t('unknown string').should.eql('unknown string');
});
});
});