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/package-lock.json b/package-lock.json index 2e4daa5..6480c8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "i18n", - "version": "0.14.1", + "version": "0.14.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "i18n", - "version": "0.14.1", + "version": "0.14.2", "license": "MIT", "dependencies": { "@messageformat/core": "^3.0.0", @@ -6426,9 +6426,9 @@ } }, "node_modules/url-parse": { - "version": "1.5.3", - "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.3.tgz", - "integrity": "sha512-IIORyIQD9rvj0A4CLWsHkBBJuNqWpFQe224b6j9t/ABmquIS0qDU2pY6kl6AuOrL5OkCXHMCFNe1jBcuAggjvQ==", + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz", + "integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==", "dev": true, "dependencies": { "querystringify": "^2.1.1", @@ -11726,9 +11726,9 @@ } }, "url-parse": { - "version": "1.5.3", - "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.3.tgz", - "integrity": "sha512-IIORyIQD9rvj0A4CLWsHkBBJuNqWpFQe224b6j9t/ABmquIS0qDU2pY6kl6AuOrL5OkCXHMCFNe1jBcuAggjvQ==", + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz", + "integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==", "dev": true, "requires": { "querystringify": "^2.1.1", diff --git a/package.json b/package.json index 6cf7e56..5609744 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "i18n", "description": "lightweight translation module with dynamic json storage", - "version": "0.14.1", + "version": "0.14.2", "homepage": "http://github.com/mashpie/i18n-node", "repository": { "type": "git", 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') + }) + }) +})