From a2ea6328a007bdea5b5cffe1d72efbc782ad6425 Mon Sep 17 00:00:00 2001 From: motumas Date: Tue, 25 Feb 2014 12:59:59 +0100 Subject: [PATCH 01/11] Added support for translate() fallback to default locale key/value if requested locale file don't have the requested key/value. --- i18n.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/i18n.js b/i18n.js index 9fe1b632..2b607dce 100644 --- a/i18n.js +++ b/i18n.js @@ -18,6 +18,10 @@ var i18n = module.exports = function(opt) { // Put into dev or production mode this.devMode = process.env.NODE_ENV !== "production"; + // If we can't find wanted string in "active" locale, + // lookup it up in default locale before using key as translated string. + this.fallbackLookupToDefaultLocale = true; + // Copy over options for (var prop in opt) { this[prop] = opt[prop]; @@ -98,9 +102,9 @@ i18n.registerMethods = function(helpers, req) { } else { helpers[method] = function(req) { return req.i18n[method].bind(req.i18n); - }; + }; } - + }); return helpers; @@ -135,9 +139,9 @@ i18n.prototype = { }, setLocale: function(locale) { - + if (!locale) return; - + if (!this.locales[locale]) { if (this.devMode) { console.warn("Locale (" + locale + ") not found."); @@ -251,15 +255,18 @@ i18n.prototype = { this.initLocale(locale, {}); } - if (!this.locales[locale][singular]) { - this.locales[locale][singular] = plural ? - { one: singular, other: plural } : - singular; - - if (this.devMode) { - this.writeFile(locale); - } - } + if (!this.locales[locale][singular]) { + if (!this.fallbackLookupToDefaultLocale || !this.locales[this.defaultLocale][singular]) { + this.locales[locale][singular] = plural ? + { one: singular, other: plural } : + singular; + if (this.devMode) { + this.writeFile(locale); + } + } else { + return this.locales[this.defaultLocale][singular]; + } + } return this.locales[locale][singular]; }, From 1cec175f27de144c40c8593631082f0856c816b0 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Fri, 26 Sep 2014 15:24:34 +0200 Subject: [PATCH 02/11] When in production mode and a key is not found in the current locale, use the value from the default locale --- i18n.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/i18n.js b/i18n.js index 2b607dce..dec84710 100644 --- a/i18n.js +++ b/i18n.js @@ -18,10 +18,6 @@ var i18n = module.exports = function(opt) { // Put into dev or production mode this.devMode = process.env.NODE_ENV !== "production"; - // If we can't find wanted string in "active" locale, - // lookup it up in default locale before using key as translated string. - this.fallbackLookupToDefaultLocale = true; - // Copy over options for (var prop in opt) { this[prop] = opt[prop]; @@ -255,6 +251,16 @@ i18n.prototype = { this.initLocale(locale, {}); } + if (!this.locales[locale][singular]) { + if (this.devMode) { + this.locales[locale][singular] = plural ? { one: singular, other: plural } : singular; + + this.writeFile(locale); + } else { + this.locales[this.defaultLocale][singular] = plural ? { one: singular, other: plural } : singular; + } + } + if (!this.locales[locale][singular]) { if (!this.fallbackLookupToDefaultLocale || !this.locales[this.defaultLocale][singular]) { this.locales[locale][singular] = plural ? From 54d1ef9ff1dd0afecfa4b58fed4297a295313c03 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Fri, 26 Sep 2014 15:25:53 +0200 Subject: [PATCH 03/11] When in production mode and a key is not found in the current locale, use the value from the default locale --- i18n.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/i18n.js b/i18n.js index dec84710..a877bef7 100644 --- a/i18n.js +++ b/i18n.js @@ -261,19 +261,6 @@ i18n.prototype = { } } - if (!this.locales[locale][singular]) { - if (!this.fallbackLookupToDefaultLocale || !this.locales[this.defaultLocale][singular]) { - this.locales[locale][singular] = plural ? - { one: singular, other: plural } : - singular; - if (this.devMode) { - this.writeFile(locale); - } - } else { - return this.locales[this.defaultLocale][singular]; - } - } - return this.locales[locale][singular]; }, From c42f4859e724d263285a2edfb01d308a37e579ca Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Mon, 17 Nov 2014 14:05:18 +0100 Subject: [PATCH 04/11] When in production mode and a key is not found in the current locale, use the value from the default locale, bugfix --- i18n-node-2.iml | 14 ++++++++++++++ i18n.js | 1 + 2 files changed, 15 insertions(+) create mode 100644 i18n-node-2.iml diff --git a/i18n-node-2.iml b/i18n-node-2.iml new file mode 100644 index 00000000..29bd7bfa --- /dev/null +++ b/i18n-node-2.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/i18n.js b/i18n.js index a877bef7..bc91812c 100644 --- a/i18n.js +++ b/i18n.js @@ -257,6 +257,7 @@ i18n.prototype = { this.writeFile(locale); } else { + locale = this.defaultLocale; this.locales[this.defaultLocale][singular] = plural ? { one: singular, other: plural } : singular; } } From 11c12e09899174f6edb2305f30d3b35cbd8afb06 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Mon, 17 Nov 2014 14:18:49 +0100 Subject: [PATCH 05/11] When in production mode and a key is not found in the current locale, use the value from the default locale, bugfix --- i18n.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i18n.js b/i18n.js index bc91812c..2870b43a 100644 --- a/i18n.js +++ b/i18n.js @@ -258,7 +258,7 @@ i18n.prototype = { this.writeFile(locale); } else { locale = this.defaultLocale; - this.locales[this.defaultLocale][singular] = plural ? { one: singular, other: plural } : singular; + this.locales[locale][singular] = plural ? { one: singular, other: plural } : singular; } } From 6177a71fd2006adf97a94f392918e6429d443b84 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Mon, 17 Nov 2014 14:32:15 +0100 Subject: [PATCH 06/11] When in production mode and a key is not found in the current locale, use the value from the default locale, bugfix --- i18n.js | 1 - 1 file changed, 1 deletion(-) diff --git a/i18n.js b/i18n.js index 2870b43a..f6c64427 100644 --- a/i18n.js +++ b/i18n.js @@ -258,7 +258,6 @@ i18n.prototype = { this.writeFile(locale); } else { locale = this.defaultLocale; - this.locales[locale][singular] = plural ? { one: singular, other: plural } : singular; } } From b321b3f1f93c26424169804ea7df6bce93b5c351 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Mon, 17 Nov 2014 14:37:01 +0100 Subject: [PATCH 07/11] upped version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3fe68e0e..9a9c9b53 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "i18n-2", "description": "lightweight simple translation module with dynamic json storage", - "version": "0.4.6", + "version": "0.4.7", "homepage": "http://github.com/jeresig/i18n-node-2", "repository": { "type": "git", From 83bee4387eae6e4fc65a4377bc00e773b9268f90 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Wed, 24 Jun 2015 14:12:38 +0200 Subject: [PATCH 08/11] updated ignored file list --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4a90a2d6..3ce090f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules/ testlocales/ -.DS_Store \ No newline at end of file +.DS_Store +.idea +*.iml \ No newline at end of file From 5561790f9a20a862cfd4a1868d6a893a80364851 Mon Sep 17 00:00:00 2001 From: Ulrik Einarson Date: Wed, 24 Jun 2015 14:14:34 +0200 Subject: [PATCH 09/11] Delete i18n-node-2.iml --- i18n-node-2.iml | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 i18n-node-2.iml diff --git a/i18n-node-2.iml b/i18n-node-2.iml deleted file mode 100644 index 29bd7bfa..00000000 --- a/i18n-node-2.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - From f12702d6695fecb029311aee803252af13d0e3aa Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Wed, 26 Aug 2015 13:36:49 +0200 Subject: [PATCH 10/11] bugfix --- i18n.js | 637 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 318 insertions(+), 319 deletions(-) diff --git a/i18n.js b/i18n.js index a72d2995..75017793 100644 --- a/i18n.js +++ b/i18n.js @@ -9,73 +9,73 @@ // dependencies var vsprintf = require("sprintf").vsprintf, - fs = require("fs"), - path = require("path"); + fs = require("fs"), + path = require("path"); -function dotNotation (obj, str) { - if (obj.hasOwnProperty(str)) { - return obj[str]; - } +function dotNotation(obj, str) { + if (obj.hasOwnProperty(str)) { + return obj[str]; + } - return str.split(".").reduce(function(o, x) { - return o[x]; + return str.split(".").reduce(function (o, x) { + return o[x]; }, obj); } var i18n = module.exports = function (opt) { - var self = this; - - // Put into dev or production mode - this.devMode = process.env.NODE_ENV !== "production"; - - // Copy over options - for (var prop in opt) { - this[prop] = opt[prop]; - } - - // you may register helpers in global scope, up to you - if (typeof this.register === "object") { - i18n.resMethods.forEach(function (method) { - self.register[method] = self[method].bind(self); - }); - } - - // implicitly read all locales - // if it's an array of locale names, read in the data - if (opt.locales && opt.locales.forEach) { - this.locales = {}; - - opt.locales.forEach(function (locale) { - self.readFile(locale); - }); - - this.defaultLocale = opt.locales[0]; - } - - // Set the locale to the default locale - this.setLocale(this.defaultLocale); - - // Check the defaultLocale - if (!this.locales[this.defaultLocale]) { - console.error("Not a valid default locale."); - } - - if (this.request) { - if (this.subdomain) { - this.setLocaleFromSubdomain(this.request); - } - - if (this.query !== false) { - this.setLocaleFromQuery(this.request); - } - - this.prefLocale = this.preferredLocale(); - - if (this.prefLocale !== false && this.prefLocale !== this.locale) { - this.setLocale(this.prefLocale); - } - } + var self = this; + + // Put into dev or production mode + this.devMode = process.env.NODE_ENV !== "production"; + + // Copy over options + for (var prop in opt) { + this[prop] = opt[prop]; + } + + // you may register helpers in global scope, up to you + if (typeof this.register === "object") { + i18n.resMethods.forEach(function (method) { + self.register[method] = self[method].bind(self); + }); + } + + // implicitly read all locales + // if it's an array of locale names, read in the data + if (opt.locales && opt.locales.forEach) { + this.locales = {}; + + opt.locales.forEach(function (locale) { + self.readFile(locale); + }); + + this.defaultLocale = opt.locales[0]; + } + + // Set the locale to the default locale + this.setLocale(this.defaultLocale); + + // Check the defaultLocale + if (!this.locales[this.defaultLocale]) { + console.error("Not a valid default locale."); + } + + if (this.request) { + if (this.subdomain) { + this.setLocaleFromSubdomain(this.request); + } + + if (this.query !== false) { + this.setLocaleFromQuery(this.request); + } + + this.prefLocale = this.preferredLocale(); + + if (this.prefLocale !== false && this.prefLocale !== this.locale) { + this.setLocale(this.prefLocale); + } + } }; i18n.version = "0.4.6"; @@ -84,321 +84,320 @@ i18n.localeCache = {}; i18n.resMethods = ["__", "__n", "getLocale", "isPreferredLocale"]; i18n.expressBind = function (app, opt) { - if (!app) { - return; - } - - app.use(function (req, res, next) { - opt.request = req; - req.i18n = new i18n(opt); - - // Express 3 - if (res.locals) { - i18n.registerMethods(res.locals, req) - } - - next(); - }); - - // Express 2 - if (app.dynamicHelpers) { - app.dynamicHelpers(i18n.registerMethods({})); - } + if (!app) { + return; + } + + app.use(function (req, res, next) { + opt.request = req; + req.i18n = new i18n(opt); + + // Express 3 + if (res.locals) { + i18n.registerMethods(res.locals, req) + } + + next(); + }); + + // Express 2 + if (app.dynamicHelpers) { + app.dynamicHelpers(i18n.registerMethods({})); + } }; i18n.registerMethods = function (helpers, req) { - i18n.resMethods.forEach(function (method) { - if (req) { - helpers[method] = req.i18n[method].bind(req.i18n); - } else { - helpers[method] = function (req) { - return req.i18n[method].bind(req.i18n); - }; - } - - }); - - return helpers; + i18n.resMethods.forEach(function (method) { + if (req) { + helpers[method] = req.i18n[method].bind(req.i18n); + } else { + helpers[method] = function (req) { + return req.i18n[method].bind(req.i18n); + }; + } + + }); + + return helpers; }; i18n.prototype = { - defaultLocale: "en", - extension: ".js", - directory: "./locales", - cookieName: "lang", + defaultLocale: "en", + extension: ".js", + directory: "./locales", + cookieName: "lang", - __: function () { - var msg = this.translate(this.locale, arguments[0]); + __: function () { + var msg = this.translate(this.locale, arguments[0]); - if (arguments.length > 1) { - msg = vsprintf(msg, Array.prototype.slice.call(arguments, 1)); - } + if (arguments.length > 1) { + msg = vsprintf(msg, Array.prototype.slice.call(arguments, 1)); + } - return msg; - }, + return msg; + }, - __n: function (pathOrSingular, countOrPlural, additionalOrCount) { - var msg; - if (typeof countOrPlural === 'number') { - var path = pathOrSingular; - var count = countOrPlural; - msg = this.translate(this.locale, path); - console.log('initial', msg); + __n: function (pathOrSingular, countOrPlural, additionalOrCount) { + var msg; + if (typeof countOrPlural === 'number') { + var path = pathOrSingular; + var count = countOrPlural; + msg = this.translate(this.locale, path); + console.log('initial', msg); - msg = vsprintf(parseInt(count, 10) > 1 ? msg.other : msg.one, Array.prototype.slice.call(arguments, 1)); - } else { - var singular = pathOrSingular; - var plural = countOrPlural; - var count = additionalOrCount; - msg = this.translate(this.locale, singular, plural); + msg = vsprintf(parseInt(count, 10) > 1 ? msg.other : msg.one, Array.prototype.slice.call(arguments, 1)); + } else { + var singular = pathOrSingular; + var plural = countOrPlural; + var count = additionalOrCount; + msg = this.translate(this.locale, singular, plural); - msg = vsprintf(parseInt(count, 10) > 1 ? msg.other : msg.one, [count]); + msg = vsprintf(parseInt(count, 10) > 1 ? msg.other : msg.one, [count]); - if (arguments.length > 3) { - msg = vsprintf(msg, Array.prototype.slice.call(arguments, 3)); - } - } + if (arguments.length > 3) { + msg = vsprintf(msg, Array.prototype.slice.call(arguments, 3)); + } + } - return msg; - }, + return msg; + }, - setLocale: function (locale) { + setLocale: function (locale) { - if (!locale) return; + if (!locale) return; - if (!this.locales[locale]) { - if (this.devMode) { - console.warn("Locale (" + locale + ") not found."); - } + if (!this.locales[locale]) { + if (this.devMode) { + console.warn("Locale (" + locale + ") not found."); + } - locale = this.defaultLocale; - } + locale = this.defaultLocale; + } - return (this.locale = locale); - }, + return (this.locale = locale); + }, - getLocale: function () { - return this.locale; - }, + getLocale: function () { + return this.locale; + }, - isPreferredLocale: function () { - return !this.prefLocale || - this.prefLocale === this.getLocale(); - }, + isPreferredLocale: function () { + return !this.prefLocale || + this.prefLocale === this.getLocale(); + }, - setLocaleFromSessionVar: function (req) { - req = req || this.request; + setLocaleFromSessionVar: function (req) { + req = req || this.request; - var locale = req.session.locale; + var locale = req.session.locale; - if (this.locales[locale]) { - if (this.devMode) { - console.log("Overriding locale from query: " + locale); - } - this.setLocale(locale); - } + if (this.locales[locale]) { + if (this.devMode) { + console.log("Overriding locale from query: " + locale); + } + this.setLocale(locale); + } - }, + }, - setLocaleFromQuery: function (req) { - req = req || this.request; + setLocaleFromQuery: function (req) { + req = req || this.request; - if (!req || !req.query || !req.query.lang) { - return; - } + if (!req || !req.query || !req.query.lang) { + return; + } - var locale = req.query.lang.toLowerCase(); + var locale = req.query.lang.toLowerCase(); - if (this.locales[locale]) { - if (this.devMode) { - console.log("Overriding locale from query: " + locale); - } + if (this.locales[locale]) { + if (this.devMode) { + console.log("Overriding locale from query: " + locale); + } - this.setLocale(locale); - } - }, + this.setLocale(locale); + } + }, - setLocaleFromSubdomain: function (req) { - req = req || this.request; + setLocaleFromSubdomain: function (req) { + req = req || this.request; - if (!req || !req.headers || !req.headers.host) { - return; - } + if (!req || !req.headers || !req.headers.host) { + return; + } - if (/^([^.]+)/.test(req.headers.host) && this.locales[RegExp.$1]) { - if (this.devMode) { - console.log("Overriding locale from host: " + RegExp.$1); - } + if (/^([^.]+)/.test(req.headers.host) && this.locales[RegExp.$1]) { + if (this.devMode) { + console.log("Overriding locale from host: " + RegExp.$1); + } - this.setLocale(RegExp.$1); - } - }, + this.setLocale(RegExp.$1); + } + }, - setLocaleFromCookie: function (req) { - req = req || this.request; + setLocaleFromCookie: function (req) { + req = req || this.request; - if (!req || !req.cookies || !this.cookieName || !req.cookies[this.cookieName]) { - return; - } + if (!req || !req.cookies || !this.cookieName || !req.cookies[this.cookieName]) { + return; + } - var locale = req.cookies[this.cookieName].toLowerCase(); + var locale = req.cookies[this.cookieName].toLowerCase(); - if (this.locales[locale]) { - if (this.devMode) { - console.log("Overriding locale from cookie: " + locale); - } + if (this.locales[locale]) { + if (this.devMode) { + console.log("Overriding locale from cookie: " + locale); + } - this.setLocale(locale); - } - }, + this.setLocale(locale); + } + }, - preferredLocale: function (req) { - req = req || this.request; + preferredLocale: function (req) { + req = req || this.request; - if (!req || !req.headers) { - return; - } + if (!req || !req.headers) { + return; + } - var accept = req.headers["accept-language"] || "", - regExp = /(^|,\s*)([a-z0-9-]+)/gi, - self = this, - prefLocale; + var accept = req.headers["accept-language"] || "", + regExp = /(^|,\s*)([a-z0-9-]+)/gi, + self = this, + prefLocale; - while (!prefLocale && (match = regExp.exec(accept))) { - var locale = match[2].toLowerCase(); - var parts = locale.split("-"); + while (!prefLocale && (match = regExp.exec(accept))) { + var locale = match[2].toLowerCase(); + var parts = locale.split("-"); - if (self.locales[locale]) { - prefLocale = locale; - } else if (parts.length > 1 && self.locales[parts[0]]) { - prefLocale = parts[0]; + if (self.locales[locale]) { + prefLocale = locale; + } else if (parts.length > 1 && self.locales[parts[0]]) { + prefLocale = parts[0]; } - } + } - return prefLocale || this.defaultLocale; - }, + return prefLocale || this.defaultLocale; + }, - // read locale file, translate a msg and write to fs if new - translate: function (locale, singular, plural) { - if (!locale || !this.locales[locale]) { - if (this.devMode) { - console.warn("WARN: No locale found. Using the default (" + - this.defaultLocale + ") as current locale"); - } + // read locale file, translate a msg and write to fs if new + translate: function (locale, singular, plural) { + if (!locale || !this.locales[locale]) { + if (this.devMode) { + console.warn("WARN: No locale found. Using the default (" + + this.defaultLocale + ") as current locale"); + } - locale = this.defaultLocale; + locale = this.defaultLocale; - this.initLocale(locale, {}); - } + this.initLocale(locale, {}); + } - if (!this.locales[locale][singular]) { + if (!this.locales[locale][singular]) { if (this.devMode) { - this.locales[locale][singular] = plural ? - { one: singular, other: plural } : - singular; + this.locales[locale][singular] = plural ? {one: singular, other: plural} : singular; this.writeFile(locale); } else { locale = this.defaultLocale; + this.locales[locale][singular] = plural ? {one: singular, other: plural} : singular; + } + } + + return dotNotation(this.locales[locale], singular); + }, + + // try reading a file + readFile: function (locale) { + var file = this.locateFile(locale); + + if (!this.devMode && i18n.localeCache[file]) { + this.initLocale(locale, i18n.localeCache[file]); + return; + } + + try { + var localeFile = fs.readFileSync(file); + + try { + // parsing filecontents to locales[locale] + this.initLocale(locale, JSON.parse(localeFile)); + + } catch (e) { + console.error('unable to parse locales from file (maybe ' + file + + ' is empty or invalid json?): ', e); + } + + } catch (e) { + // unable to read, so intialize that file + // locales[locale] are already set in memory, so no extra read required + // or locales[locale] are empty, which initializes an empty locale.json file + if (!fs.existsSync(file)) { + this.writeFile(locale); } } + }, + + // try writing a file in a created directory + writeFile: function (locale) { + // don't write new locale information to disk if we're not in dev mode + if (!this.devMode) { + // Initialize the locale if didn't exist already + this.initLocale(locale, {}); + return; + } + + // creating directory if necessary + try { + fs.lstatSync(this.directory); + + } catch (e) { + if (this.devMode) { + console.log('creating locales dir in: ' + this.directory); + } + + fs.mkdirSync(this.directory, 0755); + } + + // Initialize the locale if didn't exist already + this.initLocale(locale, {}); + + // writing to tmp and rename on success + try { + var target = this.locateFile(locale), + tmp = target + ".tmp"; - return dotNotation(this.locales[locale], singular); - }, - - // try reading a file - readFile: function (locale) { - var file = this.locateFile(locale); - - if (!this.devMode && i18n.localeCache[file]) { - this.initLocale(locale, i18n.localeCache[file]); - return; - } - - try { - var localeFile = fs.readFileSync(file); - - try { - // parsing filecontents to locales[locale] - this.initLocale(locale, JSON.parse(localeFile)); - - } catch (e) { - console.error('unable to parse locales from file (maybe ' + file + - ' is empty or invalid json?): ', e); - } - - } catch (e) { - // unable to read, so intialize that file - // locales[locale] are already set in memory, so no extra read required - // or locales[locale] are empty, which initializes an empty locale.json file - if (!fs.existsSync(file)) { - this.writeFile(locale); - } - } - }, - - // try writing a file in a created directory - writeFile: function (locale) { - // don't write new locale information to disk if we're not in dev mode - if (!this.devMode) { - // Initialize the locale if didn't exist already - this.initLocale(locale, {}); - return; - } - - // creating directory if necessary - try { - fs.lstatSync(this.directory); - - } catch (e) { - if (this.devMode) { - console.log('creating locales dir in: ' + this.directory); - } - - fs.mkdirSync(this.directory, 0755); - } - - // Initialize the locale if didn't exist already - this.initLocale(locale, {}); - - // writing to tmp and rename on success - try { - var target = this.locateFile(locale), - tmp = target + ".tmp"; - - fs.writeFileSync(tmp, JSON.stringify( - this.locales[locale], null, "\t"), "utf8"); - - if (fs.statSync(tmp).isFile()) { - fs.renameSync(tmp, target); - - } else { - console.error('unable to write locales to file (either ' + tmp + - ' or ' + target + ' are not writeable?): '); - } - - } catch (e) { - console.error('unexpected error writing files (either ' + tmp + - ' or ' + target + ' are not writeable?): ', e); - } - }, - - // basic normalization of filepath - locateFile: function (locale) { - return path.normalize(this.directory + '/' + locale + this.extension); - }, - - initLocale: function (locale, data) { - if (!this.locales[locale]) { - this.locales[locale] = data; - - // Only cache the files when we're not in dev mode - if (!this.devMode) { - var file = this.locateFile(locale); - if (!i18n.localeCache[file]) { - i18n.localeCache[file] = data; - } - } - } - } + fs.writeFileSync(tmp, JSON.stringify( + this.locales[locale], null, "\t"), "utf8"); + + if (fs.statSync(tmp).isFile()) { + fs.renameSync(tmp, target); + + } else { + console.error('unable to write locales to file (either ' + tmp + + ' or ' + target + ' are not writeable?): '); + } + + } catch (e) { + console.error('unexpected error writing files (either ' + tmp + + ' or ' + target + ' are not writeable?): ', e); + } + }, + + // basic normalization of filepath + locateFile: function (locale) { + return path.normalize(this.directory + '/' + locale + this.extension); + }, + + initLocale: function (locale, data) { + if (!this.locales[locale]) { + this.locales[locale] = data; + + // Only cache the files when we're not in dev mode + if (!this.devMode) { + var file = this.locateFile(locale); + if (!i18n.localeCache[file]) { + i18n.localeCache[file] = data; + } + } + } + } }; From 6206b916d4df4b7e42db5bc3e28651ed8f003254 Mon Sep 17 00:00:00 2001 From: ulrik einarson Date: Thu, 16 Mar 2017 15:21:06 +0100 Subject: [PATCH 11/11] use engish text when translation is not found --- i18n.js | 1 - 1 file changed, 1 deletion(-) diff --git a/i18n.js b/i18n.js index 75017793..d4de8828 100644 --- a/i18n.js +++ b/i18n.js @@ -298,7 +298,6 @@ i18n.prototype = { this.writeFile(locale); } else { locale = this.defaultLocale; - this.locales[locale][singular] = plural ? {one: singular, other: plural} : singular; } }