From 1d956f3f9046e4d8204d99adb182c7e0f756a6d4 Mon Sep 17 00:00:00 2001 From: Marcus Spiegel Date: Sat, 5 Mar 2022 17:39:06 +0100 Subject: [PATCH] fix #493 (call stack bug) & tests --- i18n.js | 4 +- test/i18n.retryInDefaultLocaleWithSync.js | 101 ++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 test/i18n.retryInDefaultLocaleWithSync.js diff --git a/i18n.js b/i18n.js index 16986ff..f667d7c 100644 --- a/i18n.js +++ b/i18n.js @@ -1025,7 +1025,7 @@ const i18n = function I18n(_OPTS = false) { ' retrying in ' + defaultLocale ) - mutator(translate(defaultLocale, singular, plural)) + mutator(translate(defaultLocale, singular, plural, true)) } else { mutator({ one: defaultSingular || singular, @@ -1047,7 +1047,7 @@ const i18n = function I18n(_OPTS = false) { ' retrying in ' + defaultLocale ) - mutator(translate(defaultLocale, singular, plural)) + mutator(translate(defaultLocale, singular, plural, true)) } else { mutator(defaultSingular || singular) } diff --git a/test/i18n.retryInDefaultLocaleWithSync.js b/test/i18n.retryInDefaultLocaleWithSync.js new file mode 100644 index 0000000..ad59c5d --- /dev/null +++ b/test/i18n.retryInDefaultLocaleWithSync.js @@ -0,0 +1,101 @@ +const { I18n } = require('..') +const should = require('should') +const fs = require('fs') + +describe('retryInDefaultLocaleWithSync', () => { + const DIRECTORY = './locales_in_sync' + const CONFIG = { + locales: ['en', 'de'], + directory: DIRECTORY, + defaultLocale: 'en', + retryInDefaultLocale: true, + syncFiles: true + } + + const readJson = (locale) => { + return JSON.parse(fs.readFileSync(`${DIRECTORY}/${locale}.json`)) + } + + const writeJson = (locale, data) => { + fs.writeFileSync( + `${DIRECTORY}/${locale}.json`, + JSON.stringify(data, null, '\t') + ) + } + + describe('writing', () => { + const i18n = new I18n(CONFIG) + const req = {} + i18n.init(req) + after(() => { + try { + fs.unlinkSync(`${DIRECTORY}/de.json`) + fs.unlinkSync(`${DIRECTORY}/en.json`) + fs.rmdirSync(DIRECTORY) + } catch (e) {} + }) + + it('should not throw', () => { + req.setLocale('en') + should.equal(req.__('test'), 'test') + + req.setLocale('de') + should.equal(req.__('test'), 'test') + + req.setLocale('fr') + should.equal(req.__('test'), 'test') + }) + + it('should have written all files', () => { + const statsen = fs.lstatSync(`${DIRECTORY}/en.json`) + const statsde = fs.lstatSync(`${DIRECTORY}/de.json`) + should.exist(statsen) + should.exist(statsde) + }) + + it('should not have written unsupported locale files', () => { + let statsfr + try { + statsfr = fs.lstatSync(`${DIRECTORY}/fr.json`) + } catch (e) { + should.equal(e.code, 'ENOENT') + } + should.not.exist(statsfr) + }) + + it('should have written same data to all files', () => { + const dataEn = readJson('en') + const dataDe = readJson('de') + should.deepEqual(dataEn, dataDe) + }) + }) + + describe('reading', () => { + writeJson('en', { test: 'test', welcome: 'welcome' }) + writeJson('de', { test: 'test', welcome: 'Willkommen' }) + const i18n = new I18n(CONFIG) + const req = {} + i18n.init(req) + after(() => { + try { + fs.unlinkSync(`${DIRECTORY}/de.json`) + fs.unlinkSync(`${DIRECTORY}/en.json`) + fs.rmdirSync(DIRECTORY) + } catch (e) {} + }) + + it('should still return default locales value', () => { + req.setLocale('en') + should.equal(req.__('test'), 'test') + should.equal(req.__('welcome'), 'welcome') + + req.setLocale('de') + should.equal(req.__('test'), 'test') + should.equal(req.__('welcome'), 'Willkommen') + + req.setLocale('fr') + should.equal(req.__('test'), 'test') + should.equal(req.__('welcome'), 'welcome') + }) + }) +})