i18n: remove unused 'app' code

We'll be adding a 'web' version next...
This commit is contained in:
infinite-persistence 2021-12-31 08:34:19 +08:00
parent 77ebb7111e
commit 49c6559049
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0

View file

@ -1,7 +1,3 @@
// @if TARGET='app'
let fs = require('fs');
// @endif
const isProduction = process.env.NODE_ENV === 'production';
let knownMessages = null;
let localStorageAvailable;
@ -13,45 +9,16 @@ try {
window.i18n_messages = window.i18n_messages || {};
// @if TARGET='app'
function saveMessageDesktop(message) {
const messagesFilePath = __static + '/app-strings.json';
if (knownMessages === null) {
try {
knownMessages = JSON.parse(fs.readFileSync(messagesFilePath, 'utf-8'));
} catch (err) {
throw new Error('Error parsing i18n messages file: ' + messagesFilePath + ' err: ' + err);
}
}
if (!knownMessages[message]) {
const END = '--end--';
delete knownMessages[END];
knownMessages[message] = removeContextMetadata(message);
knownMessages[END] = END;
fs.writeFile(messagesFilePath, JSON.stringify(knownMessages, null, 2) + '\n', 'utf-8', err => {
if (err) {
throw err;
}
});
}
}
// @endif
/*
I dislike the below code (and note that it ships all the way to the distributed app),
but this seems better than silently having this limitation and future devs not knowing.
*/
// @if TARGET='web'
function saveMessageWeb(message) {
if (!isProduction && knownMessages === null) {
console.log('Note that i18n messages are not saved in web dev mode.'); // eslint-disable-line
knownMessages = {};
}
}
// @endif
function removeContextMetadata(message) {
// Example string entries with context-metadata:
@ -87,7 +54,7 @@ export function __(message, tokens) {
? window.localStorage.getItem('language') || 'en'
: window.navigator.language.slice(0, 2) || 'en';
if (!isProduction) {
IS_WEB ? saveMessageWeb(message) : saveMessageDesktop(message);
saveMessageWeb(message);
}
let translatedMessage = window.i18n_messages[language] ? window.i18n_messages[language][message] || message : message;
@ -97,7 +64,7 @@ export function __(message, tokens) {
return translatedMessage;
}
return translatedMessage.replace(/%([^%]+)%/g, function($1, $2) {
return translatedMessage.replace(/%([^%]+)%/g, ($1, $2) => {
return tokens.hasOwnProperty($2) ? tokens[$2] : $2;
});
}