Update style of i18n, pull it in via preload instead of .html
This commit is contained in:
parent
1ad2b080c8
commit
f86a6ef752
7 changed files with 47 additions and 40 deletions
|
@ -19,6 +19,7 @@ test/views/*.js
|
||||||
!js/backup.js
|
!js/backup.js
|
||||||
!js/database.js
|
!js/database.js
|
||||||
!js/logging.js
|
!js/logging.js
|
||||||
|
!js/i18n.js
|
||||||
!js/models/conversations.js
|
!js/models/conversations.js
|
||||||
!js/views/attachment_view.js
|
!js/views/attachment_view.js
|
||||||
!js/views/conversation_search_view.js
|
!js/views/conversation_search_view.js
|
||||||
|
|
|
@ -909,7 +909,6 @@
|
||||||
<script type='text/javascript' src='js/expire.js'></script>
|
<script type='text/javascript' src='js/expire.js'></script>
|
||||||
<script type='text/javascript' src='js/conversation_controller.js'></script>
|
<script type='text/javascript' src='js/conversation_controller.js'></script>
|
||||||
<script type='text/javascript' src='js/emoji_util.js'></script>
|
<script type='text/javascript' src='js/emoji_util.js'></script>
|
||||||
<script type='text/javascript' src='js/i18n.js'></script>
|
|
||||||
|
|
||||||
<script type='text/javascript' src='js/views/whisper_view.js'></script>
|
<script type='text/javascript' src='js/views/whisper_view.js'></script>
|
||||||
<script type='text/javascript' src='js/views/last_seen_indicator_view.js'></script>
|
<script type='text/javascript' src='js/views/last_seen_indicator_view.js'></script>
|
||||||
|
|
57
js/i18n.js
57
js/i18n.js
|
@ -1,29 +1,34 @@
|
||||||
/*
|
/* eslint-env node */
|
||||||
* vim: ts=4:sw=4:expandtab
|
|
||||||
*/
|
|
||||||
;(function() {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// preload.js loads this, pulling it from main.js (where it was loaded from disk)
|
exports.setup = (locale, messages) => {
|
||||||
var messages = window.config.localeMessages;
|
if (!locale) {
|
||||||
var locale = window.config.locale;
|
throw new Error('i18n: locale parameter is required');
|
||||||
|
}
|
||||||
|
if (!messages) {
|
||||||
|
throw new Error('i18n: messages parameter is required');
|
||||||
|
}
|
||||||
|
|
||||||
window.i18n = function (message, substitutions) {
|
function getMessage(key, substitutions) {
|
||||||
if (!messages[message]) {
|
const entry = messages[key];
|
||||||
return;
|
if (!entry) {
|
||||||
}
|
console.error(`i18n: Attempted to get translation for nonexistent key '${key}'`);
|
||||||
var s = messages[message].message;
|
return '';
|
||||||
if (substitutions instanceof Array) {
|
}
|
||||||
substitutions.forEach(function(sub) {
|
|
||||||
s = s.replace(/\$.+?\$/, sub);
|
|
||||||
});
|
|
||||||
} else if (substitutions) {
|
|
||||||
s = s.replace(/\$.+?\$/, substitutions);
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
};
|
|
||||||
|
|
||||||
i18n.getLocale = function() {
|
const { message } = entry;
|
||||||
return locale;
|
if (substitutions instanceof Array) {
|
||||||
};
|
return substitutions.reduce(
|
||||||
})();
|
(result, substitution) => result.replace(/\$.+?\$/, substitution),
|
||||||
|
message
|
||||||
|
);
|
||||||
|
} else if (substitutions) {
|
||||||
|
return message.replace(/\$.+?\$/, substitutions);
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
getMessage.getLocale = () => locale;
|
||||||
|
|
||||||
|
return getMessage;
|
||||||
|
};
|
||||||
|
|
|
@ -5,15 +5,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
window.Whisper = window.Whisper || {};
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
moment.updateLocale(i18n.getLocale(), {
|
|
||||||
relativeTime : {
|
|
||||||
s: i18n('timestamp_s') || 'now',
|
|
||||||
m: i18n('timestamp_m') || '1 minute',
|
|
||||||
h: i18n('timestamp_h') || '1 hour'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
moment.locale(i18n.getLocale());
|
|
||||||
|
|
||||||
Whisper.TimestampView = Whisper.View.extend({
|
Whisper.TimestampView = Whisper.View.extend({
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
extension.windows.onClosed(this.clearTimeout.bind(this));
|
extension.windows.onClosed(this.clearTimeout.bind(this));
|
||||||
|
|
13
preload.js
13
preload.js
|
@ -110,6 +110,19 @@ window.nodeNotifier = require('node-notifier');
|
||||||
window.ProxyAgent = require('proxy-agent');
|
window.ProxyAgent = require('proxy-agent');
|
||||||
window.moment = require('moment');
|
window.moment = require('moment');
|
||||||
|
|
||||||
|
const { setup } = require('./js/i18n');
|
||||||
|
|
||||||
|
const { locale, localeMessages } = window.config;
|
||||||
|
window.i18n = setup(locale, localeMessages);
|
||||||
|
window.moment.updateLocale(locale, {
|
||||||
|
relativeTime: {
|
||||||
|
s: window.i18n('timestamp_s'),
|
||||||
|
m: window.i18n('timestamp_m'),
|
||||||
|
h: window.i18n('timestamp_h'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
window.moment.locale(locale);
|
||||||
|
|
||||||
// ES2015+ modules
|
// ES2015+ modules
|
||||||
const attachmentsPath = Attachments.getPath(app.getPath('userData'));
|
const attachmentsPath = Attachments.getPath(app.getPath('userData'));
|
||||||
const deleteAttachmentData = Attachments.createDeleter(attachmentsPath);
|
const deleteAttachmentData = Attachments.createDeleter(attachmentsPath);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
describe('i18n', function() {
|
describe('i18n', function() {
|
||||||
describe('i18n', function() {
|
describe('i18n', function() {
|
||||||
it('returns undefined for unknown string', function() {
|
it('returns empty string for unknown string', function() {
|
||||||
assert.strictEqual(i18n('random'), undefined);
|
assert.strictEqual(i18n('random'), '');
|
||||||
});
|
});
|
||||||
it('returns message for given string', function() {
|
it('returns message for given string', function() {
|
||||||
assert.equal(i18n('reportIssue'), 'Report an issue');
|
assert.equal(i18n('reportIssue'), 'Report an issue');
|
||||||
|
|
|
@ -562,8 +562,6 @@
|
||||||
<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>
|
||||||
|
|
Loading…
Reference in a new issue