Use correct locale, fall back to en if we don't have translations
FREEBIE
This commit is contained in:
parent
18b8907c96
commit
4402a91976
5 changed files with 69 additions and 9 deletions
12
js/i18n.js
12
js/i18n.js
|
@ -3,12 +3,16 @@
|
||||||
*/
|
*/
|
||||||
;(function() {
|
;(function() {
|
||||||
'use strict';
|
'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) {
|
window.i18n = function (message, substitutions) {
|
||||||
if (!json[message]) {
|
if (!messages[message]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var s = json[message].message;
|
var s = messages[message].message;
|
||||||
if (substitutions instanceof Array) {
|
if (substitutions instanceof Array) {
|
||||||
substitutions.forEach(function(sub) {
|
substitutions.forEach(function(sub) {
|
||||||
s = s.replace(/\$.+?\$/, sub);
|
s = s.replace(/\$.+?\$/, sub);
|
||||||
|
@ -20,6 +24,6 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
i18n.getLocale = function() {
|
i18n.getLocale = function() {
|
||||||
return window.config.locale;
|
return locale;
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
36
main.js
36
main.js
|
@ -89,11 +89,39 @@ function createWindow () {
|
||||||
mainWindow.flashFrame(false);
|
mainWindow.flashFrame(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load locale
|
function loadLocale() {
|
||||||
const locale = 'en'; // FIXME
|
// possible locales: https://github.com/electron/electron/blob/master/docs/api/locales.md
|
||||||
const localeData = JSON.parse(fs.readFileSync(path.join(__dirname, '_locales', locale, 'messages.json'), 'utf-8'))
|
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) {
|
ipc.on('locale-data', function(event, arg) {
|
||||||
event.returnValue = localeData;
|
event.returnValue = messages;
|
||||||
});
|
});
|
||||||
|
|
||||||
function prepareURL(pathSegments) {
|
function prepareURL(pathSegments) {
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
window.config = require('url').parse(window.location.toString(), true).query;
|
window.config = require('url').parse(window.location.toString(), true).query;
|
||||||
|
|
||||||
const ipc = electron.ipcRenderer
|
const ipc = electron.ipcRenderer
|
||||||
window.config.locale_json = ipc.sendSync('locale-data');
|
window.config.localeMessages = ipc.sendSync('locale-data');
|
||||||
|
|
||||||
window.setBadgeCount = function(count) {
|
window.setBadgeCount = function(count) {
|
||||||
ipc.send('set-badge-count', count);
|
ipc.send('set-badge-count', count);
|
||||||
|
|
25
test/i18n_test.js
Normal file
25
test/i18n_test.js
Normal file
|
@ -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', ['<strong>', '</strong>']);
|
||||||
|
assert.equal(actual, 'You may wish to <strong> verify </strong> 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -574,6 +574,8 @@
|
||||||
<script type="text/javascript" src="../js/reliable_trigger.js" data-cover></script>
|
<script type="text/javascript" src="../js/reliable_trigger.js" data-cover></script>
|
||||||
<script type="text/javascript" src="test.js"></script>
|
<script type="text/javascript" src="test.js"></script>
|
||||||
|
|
||||||
|
<script type='text/javascript' src='../js/i18n.js'></script>
|
||||||
|
|
||||||
<script type='text/javascript' src='../js/registration.js' data-cover></script>
|
<script type='text/javascript' src='../js/registration.js' data-cover></script>
|
||||||
<script type="text/javascript" src="../js/expire.js" data-cover></script>
|
<script type="text/javascript" src="../js/expire.js" data-cover></script>
|
||||||
<script type="text/javascript" src="../js/chromium.js" data-cover></script>
|
<script type="text/javascript" src="../js/chromium.js" data-cover></script>
|
||||||
|
@ -651,6 +653,7 @@
|
||||||
<script type="text/javascript" src="emoji_util_test.js"></script>
|
<script type="text/javascript" src="emoji_util_test.js"></script>
|
||||||
<script type="text/javascript" src="reliable_trigger_test.js"></script>
|
<script type="text/javascript" src="reliable_trigger_test.js"></script>
|
||||||
<script type="text/javascript" src="backup_test.js"></script>
|
<script type="text/javascript" src="backup_test.js"></script>
|
||||||
|
<script type="text/javascript" src="i18n_test.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="fixtures.js"></script>
|
<script type="text/javascript" src="fixtures.js"></script>
|
||||||
<script type="text/javascript" src="fixtures_test.js"></script>
|
<script type="text/javascript" src="fixtures_test.js"></script>
|
||||||
|
|
Loading…
Reference in a new issue