From 4402a9197669add95205b62313e821bc0498d966 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Mon, 15 May 2017 14:48:19 -0700 Subject: [PATCH] Use correct locale, fall back to en if we don't have translations FREEBIE --- js/i18n.js | 12 ++++++++---- main.js | 36 ++++++++++++++++++++++++++++++++---- preload.js | 2 +- test/i18n_test.js | 25 +++++++++++++++++++++++++ test/index.html | 3 +++ 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 test/i18n_test.js diff --git a/js/i18n.js b/js/i18n.js index d47ffad03c23..d5985d29eba5 100644 --- a/js/i18n.js +++ b/js/i18n.js @@ -3,12 +3,16 @@ */ ;(function() { 'use strict'; - var json = window.config.locale_json; + + // preload.js loads this, pulling it from main.js (where it was loaded from disk) + var messages = window.config.localeMessages; + var locale = window.config.locale; + window.i18n = function (message, substitutions) { - if (!json[message]) { + if (!messages[message]) { return; } - var s = json[message].message; + var s = messages[message].message; if (substitutions instanceof Array) { substitutions.forEach(function(sub) { s = s.replace(/\$.+?\$/, sub); @@ -20,6 +24,6 @@ }; i18n.getLocale = function() { - return window.config.locale; + return locale; }; })(); diff --git a/main.js b/main.js index 748e4ac5baa2..2d68eab32b9e 100644 --- a/main.js +++ b/main.js @@ -89,11 +89,39 @@ function createWindow () { mainWindow.flashFrame(false); }); - // Load locale - const locale = 'en'; // FIXME - const localeData = JSON.parse(fs.readFileSync(path.join(__dirname, '_locales', locale, 'messages.json'), 'utf-8')) + function loadLocale() { + // possible locales: https://github.com/electron/electron/blob/master/docs/api/locales.md + const locale = app.getLocale(); + + if (/^en-/.test(locale)) { + return 'en'; + } + + return locale; + } + + function loadLocaleMessages(locale) { + const onDiskLocale = locale.replace('-', '_'); + const targetFile = path.join(__dirname, '_locales', onDiskLocale, 'messages.json'); + + return JSON.parse(fs.readFileSync(targetFile, 'utf-8')) + } + + // Load locale - if we can't load messages for the current locale, we default to 'en' + var locale = loadLocale(); + var messages; + try { + messages = loadLocaleMessages(locale); + } + catch (e) { + console.log('Problem loading messages for locale ', locale, e.stack); + locale = 'en'; + messages = loadLocaleMessages(locale); + } + + // Ingested in preload.js via a sendSync call ipc.on('locale-data', function(event, arg) { - event.returnValue = localeData; + event.returnValue = messages; }); function prepareURL(pathSegments) { diff --git a/preload.js b/preload.js index 7e354148ca57..164b89addd3a 100644 --- a/preload.js +++ b/preload.js @@ -6,7 +6,7 @@ window.config = require('url').parse(window.location.toString(), true).query; const ipc = electron.ipcRenderer - window.config.locale_json = ipc.sendSync('locale-data'); + window.config.localeMessages = ipc.sendSync('locale-data'); window.setBadgeCount = function(count) { ipc.send('set-badge-count', count); diff --git a/test/i18n_test.js b/test/i18n_test.js new file mode 100644 index 000000000000..d8b6f8cf91c4 --- /dev/null +++ b/test/i18n_test.js @@ -0,0 +1,25 @@ +describe('i18n', function() { + describe('i18n', function() { + it('returns undefined for unknown string', function() { + assert.strictEqual(i18n('random'), undefined); + }); + it('returns message for given string', function() { + assert.equal(i18n('reportIssue'), 'Report an issue'); + }); + it('returns message with single substitution', function() { + const actual = i18n('attemptingReconnection', 5); + assert.equal(actual, 'Attempting reconnect in 5 seconds') + }); + it('returns message with multiple substitutions', function() { + const actual = i18n('verifyContact', ['', '']); + assert.equal(actual, 'You may wish to verify your safety number with this contact.'); + }); + }); + + describe('getLocale', function() { + it('returns a string with length two or greater', function() { + const locale = i18n.getLocale(); + assert.isAtLeast(locale.trim().length, 2); + }); + }); +}); diff --git a/test/index.html b/test/index.html index 78e14025b52c..7cc4b65f9677 100644 --- a/test/index.html +++ b/test/index.html @@ -574,6 +574,8 @@ + + @@ -651,6 +653,7 @@ +