i18n: restore ability to retrieve new i18n strings

## Issue
In the original Desktop code, new strings encountered during runtime will be automatically added to the local `app-strings.json` file. The feature is unavailable in Web because writing to file would require explicit permissions.

## Change
Partially restore the functionality by saving the strings to memory and retrieving it from the console via `copy(window.new_strings)`. It's a little bit of manual work, but I think it is good as it forces a sanity check before committing (previously, experimental/developmental strings are committed and being translated in Transifex).
This commit is contained in:
infinite-persistence 2021-12-31 18:24:18 +08:00
parent 2e50497bc1
commit 5948ee0d80
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
3 changed files with 37 additions and 8 deletions

View file

@ -2,20 +2,32 @@
import { isLocalStorageAvailable } from 'util/storage'; import { isLocalStorageAvailable } from 'util/storage';
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
let knownMessages = null;
const localStorageAvailable = isLocalStorageAvailable(); const localStorageAvailable = isLocalStorageAvailable();
window.i18n_messages = window.i18n_messages || {}; window.i18n_messages = window.i18n_messages || {};
/* /**
I dislike the below code (and note that it ships all the way to the distributed app), * Collects new i18n strings encountered during runtime.
but this seems better than silently having this limitation and future devs not knowing. * The output can be retrieved and pasted into app-strings.json.
*
* @param message
*/ */
function saveMessageWeb(message) { function saveMessageWeb(message) {
if (!isProduction && knownMessages === null) { // @if process.env.NODE_ENV!='production'
console.log('Note that i18n messages are not saved in web dev mode.'); // eslint-disable-line if (!window.app_strings) {
knownMessages = {}; return;
} }
if (!window.new_strings) {
console.log('Copy new i18n to clipboard:%c copy(window.new_strings)', 'color:yellow'); // eslint-disable-line
}
window.new_strings = window.new_strings || {};
if (!window.app_strings[message] && !window.new_strings[message]) {
window.new_strings[message] = removeContextMetadata(message);
}
// @endif
} }
function removeContextMetadata(message) { function removeContextMetadata(message) {

View file

@ -15,7 +15,7 @@ import { selectPrefsReady } from 'redux/selectors/sync';
import { Lbryio } from 'lbryinc'; import { Lbryio } from 'lbryinc';
import { getDefaultLanguage } from 'util/default-languages'; import { getDefaultLanguage } from 'util/default-languages';
const { DEFAULT_LANGUAGE } = require('config'); const { DEFAULT_LANGUAGE, URL_DEV } = require('config');
const { SDK_SYNC_KEYS } = SHARED_PREFERENCES; const { SDK_SYNC_KEYS } = SHARED_PREFERENCES;
export const IS_MAC = process.platform === 'darwin'; export const IS_MAC = process.platform === 'darwin';
@ -294,6 +294,16 @@ export function doFetchLanguage(language) {
}); });
}); });
} }
// @if process.env.NODE_ENV!='production'
if (!window.app_strings) {
fetch(`${URL_DEV}/app-strings.json`)
.then((r) => r.json())
.then((j) => {
window.app_strings = j;
});
}
// @endif
}; };
} }

View file

@ -100,6 +100,13 @@ if (fs.existsSync(ROBOTS_TXT_PATH)) {
}); });
} }
if (!isProduction) {
copyWebpackCommands.push({
from: `${STATIC_ROOT}/app-strings.json`,
to: `${DIST_ROOT}/app-strings.json`,
});
}
let plugins = [ let plugins = [
new WriteFilePlugin(), new WriteFilePlugin(),
new CopyWebpackPlugin(copyWebpackCommands), new CopyWebpackPlugin(copyWebpackCommands),