2017-05-23 17:16:31 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 MTRNord and Cooperative EITA
|
2017-05-25 13:24:17 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd.
|
2017-05-23 17:16:31 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import request from 'browser-request';
|
2017-05-25 13:24:17 +03:00
|
|
|
import counterpart from 'counterpart';
|
2017-07-12 15:58:14 +03:00
|
|
|
import Promise from 'bluebird';
|
2017-06-12 18:45:36 +03:00
|
|
|
import React from 'react';
|
2017-11-04 08:19:45 +03:00
|
|
|
import SettingsStore, {SettingLevel} from "./settings/SettingsStore";
|
2017-05-25 13:24:17 +03:00
|
|
|
|
2017-05-23 17:16:31 +03:00
|
|
|
const i18nFolder = 'i18n/';
|
|
|
|
|
2017-12-09 16:22:20 +03:00
|
|
|
// Control whether to also return original, untranslated strings
|
|
|
|
// Useful for debugging and testing
|
|
|
|
const ANNOTATE_STRINGS = false;
|
|
|
|
|
2017-05-25 13:24:17 +03:00
|
|
|
// We use english strings as keys, some of which contain full stops
|
|
|
|
counterpart.setSeparator('|');
|
|
|
|
// Fall back to English
|
|
|
|
counterpart.setFallbackLocale('en');
|
|
|
|
|
2017-09-22 22:43:27 +03:00
|
|
|
// Function which only purpose is to mark that a string is translatable
|
|
|
|
// Does not actually do anything. It's helpful for automatic extraction of translatable strings
|
|
|
|
export function _td(s) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:19:33 +03:00
|
|
|
// Wrapper for counterpart's translation function so that it handles nulls and undefineds properly
|
2017-11-14 21:55:47 +03:00
|
|
|
// Takes the same arguments as counterpart.translate()
|
2018-01-12 19:21:30 +03:00
|
|
|
function safeCounterpartTranslate(text, options) {
|
2017-06-06 15:56:37 +03:00
|
|
|
// Horrible hack to avoid https://github.com/vector-im/riot-web/issues/4191
|
|
|
|
// The interpolation library that counterpart uses does not support undefined/null
|
|
|
|
// values and instead will throw an error. This is a problem since everywhere else
|
|
|
|
// in JS land passing undefined/null will simply stringify instead, and when converting
|
|
|
|
// valid ES6 template strings to i18n strings it's extremely easy to pass undefined/null
|
|
|
|
// if there are no existing null guards. To avoid this making the app completely inoperable,
|
|
|
|
// we'll check all the values for undefined/null and stringify them here.
|
2018-01-12 19:21:30 +03:00
|
|
|
let count;
|
|
|
|
|
|
|
|
if (options && typeof options === 'object') {
|
|
|
|
count = options['count'];
|
|
|
|
Object.keys(options).forEach((k) => {
|
|
|
|
if (options[k] === undefined) {
|
2017-11-14 23:34:20 +03:00
|
|
|
console.warn("safeCounterpartTranslate called with undefined interpolation name: " + k);
|
2018-01-12 19:21:30 +03:00
|
|
|
options[k] = 'undefined';
|
2017-06-06 15:56:37 +03:00
|
|
|
}
|
2018-01-12 19:21:30 +03:00
|
|
|
if (options[k] === null) {
|
2017-11-14 23:34:20 +03:00
|
|
|
console.warn("safeCounterpartTranslate called with null interpolation name: " + k);
|
2018-01-12 19:21:30 +03:00
|
|
|
options[k] = 'null';
|
2017-06-06 15:56:37 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-01-12 19:21:30 +03:00
|
|
|
let translated = counterpart.translate(text, options);
|
|
|
|
if (translated === undefined && count !== undefined) {
|
|
|
|
// counterpart does not do fallback if no pluralisation exists
|
|
|
|
// in the preferred language, so do it here
|
2018-01-16 20:45:29 +03:00
|
|
|
translated = counterpart.translate(text, Object.assign({}, options, {locale: 'en'}));
|
2018-01-12 19:21:30 +03:00
|
|
|
}
|
|
|
|
return translated;
|
2017-05-25 13:24:17 +03:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:55:21 +03:00
|
|
|
/*
|
2017-11-13 22:19:33 +03:00
|
|
|
* Translates text and optionally also replaces XML-ish elements in the text with e.g. React components
|
|
|
|
* @param {string} text The untranslated text, e.g "click <a>here</a> now to %(foo)s".
|
|
|
|
* @param {object} variables Variable substitutions, e.g { foo: 'bar' }
|
|
|
|
* @param {object} tags Tag substitutions e.g. { 'a': (sub) => <a>{sub}</a> }
|
2017-05-30 17:55:21 +03:00
|
|
|
*
|
2017-11-14 21:55:47 +03:00
|
|
|
* In both variables and tags, the values to substitute with can be either simple strings, React components,
|
|
|
|
* or functions that return the value to use in the substitution (e.g. return a React component). In case of
|
|
|
|
* a tag replacement, the function receives as the argument the text inside the element corresponding to the tag.
|
|
|
|
*
|
|
|
|
* Use tag substitutions if you need to translate text between tags (e.g. "<a>Click here!</a>"), otherwise
|
|
|
|
* you will end up with literal "<a>" in your output, rather than HTML. Note that you can also use variable
|
|
|
|
* substitution to insert React components, but you can't use it to translate text between tags.
|
2017-05-30 17:55:21 +03:00
|
|
|
*
|
2017-11-13 22:19:33 +03:00
|
|
|
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
|
|
|
|
*/
|
|
|
|
export function _t(text, variables, tags) {
|
2017-11-14 21:55:47 +03:00
|
|
|
// Don't do subsitutions in counterpart. We handle it ourselves so we can replace with React components
|
|
|
|
// However, still pass the variables to counterpart so that it can choose the correct plural if count is given
|
|
|
|
// It is enough to pass the count variable, but in the future counterpart might make use of other information too
|
2017-11-13 22:19:33 +03:00
|
|
|
const args = Object.assign({ interpolate: false }, variables);
|
|
|
|
|
|
|
|
// The translation returns text so there's no XSS vector here (no unsafe HTML, no code execution)
|
2017-11-14 23:34:20 +03:00
|
|
|
const translated = safeCounterpartTranslate(text, args);
|
2017-11-13 22:19:33 +03:00
|
|
|
|
2018-10-12 06:05:59 +03:00
|
|
|
const substituted = substitute(translated, variables, tags);
|
2017-12-09 16:22:20 +03:00
|
|
|
|
|
|
|
// For development/testing purposes it is useful to also output the original string
|
|
|
|
// Don't do that for release versions
|
|
|
|
if (ANNOTATE_STRINGS) {
|
|
|
|
if (typeof substituted === 'string') {
|
2018-10-12 06:05:59 +03:00
|
|
|
return `@@${text}##${substituted}@@`;
|
|
|
|
} else {
|
2017-12-09 16:22:20 +03:00
|
|
|
return <span className='translated-string' data-orig-string={text}>{substituted}</span>;
|
|
|
|
}
|
2018-10-12 06:05:59 +03:00
|
|
|
} else {
|
2017-12-09 16:22:20 +03:00
|
|
|
return substituted;
|
|
|
|
}
|
2017-11-13 22:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Similar to _t(), except only does substitutions, and no translation
|
|
|
|
* @param {string} text The text, e.g "click <a>here</a> now to %(foo)s".
|
|
|
|
* @param {object} variables Variable substitutions, e.g { foo: 'bar' }
|
|
|
|
* @param {object} tags Tag substitutions e.g. { 'a': (sub) => <a>{sub}</a> }
|
2017-05-30 17:55:21 +03:00
|
|
|
*
|
2017-11-13 22:19:33 +03:00
|
|
|
* The values to substitute with can be either simple strings, or functions that return the value to use in
|
|
|
|
* the substitution (e.g. return a React component). In case of a tag replacement, the function receives as
|
|
|
|
* the argument the text inside the element corresponding to the tag.
|
2017-05-30 17:55:21 +03:00
|
|
|
*
|
2017-11-13 22:19:33 +03:00
|
|
|
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
|
2017-05-30 17:55:21 +03:00
|
|
|
*/
|
2017-11-13 22:19:33 +03:00
|
|
|
export function substitute(text, variables, tags) {
|
|
|
|
const regexpMapping = {};
|
|
|
|
|
2017-11-16 14:19:56 +03:00
|
|
|
if (variables !== undefined) {
|
2017-11-13 22:19:33 +03:00
|
|
|
for (const variable in variables) {
|
|
|
|
regexpMapping[`%\\(${variable}\\)s`] = variables[variable];
|
2017-05-30 18:17:42 +03:00
|
|
|
}
|
2017-11-13 22:19:33 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 14:19:56 +03:00
|
|
|
if (tags !== undefined) {
|
2017-11-13 22:19:33 +03:00
|
|
|
for (const tag in tags) {
|
|
|
|
regexpMapping[`(<${tag}>(.*?)<\\/${tag}>|<${tag}>|<${tag}\\s*\\/>)`] = tags[tag];
|
2017-05-30 18:17:42 +03:00
|
|
|
}
|
2017-05-30 17:55:21 +03:00
|
|
|
}
|
2017-11-13 22:19:33 +03:00
|
|
|
return replaceByRegexes(text, regexpMapping);
|
|
|
|
}
|
2017-05-30 17:55:21 +03:00
|
|
|
|
2017-11-13 22:19:33 +03:00
|
|
|
/*
|
|
|
|
* Replace parts of a text using regular expressions
|
|
|
|
* @param {string} text The text on which to perform substitutions
|
|
|
|
* @param {object} mapping A mapping from regular expressions in string form to replacement string or a
|
|
|
|
* function which will receive as the argument the capture groups defined in the regexp. E.g.
|
|
|
|
* { 'Hello (.?) World': (sub) => sub.toUpperCase() }
|
|
|
|
*
|
|
|
|
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
|
|
|
|
*/
|
|
|
|
export function replaceByRegexes(text, mapping) {
|
2017-11-17 10:47:25 +03:00
|
|
|
// We initially store our output as an array of strings and objects (e.g. React components).
|
|
|
|
// This will then be converted to a string or a <span> at the end
|
2017-11-13 22:19:33 +03:00
|
|
|
const output = [text];
|
|
|
|
|
2017-11-14 21:55:47 +03:00
|
|
|
// If we insert any components we need to wrap the output in a span. React doesn't like just an array of components.
|
|
|
|
let shouldWrapInSpan = false;
|
|
|
|
|
2017-11-13 22:19:33 +03:00
|
|
|
for (const regexpString in mapping) {
|
2017-11-14 21:55:47 +03:00
|
|
|
// TODO: Cache regexps
|
2017-11-13 22:19:33 +03:00
|
|
|
const regexp = new RegExp(regexpString);
|
2017-10-11 21:02:14 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
// Loop over what output we have so far and perform replacements
|
|
|
|
// We look for matches: if we find one, we get three parts: everything before the match, the replaced part,
|
|
|
|
// and everything after the match. Insert all three into the output. We need to do this because we can insert objects.
|
|
|
|
// Otherwise there would be no need for the splitting and we could do simple replcement.
|
2017-11-23 22:19:38 +03:00
|
|
|
let matchFoundSomewhere = false; // If we don't find a match anywhere we want to log it
|
2017-11-17 10:47:25 +03:00
|
|
|
for (const outputIndex in output) {
|
|
|
|
const inputText = output[outputIndex];
|
|
|
|
if (typeof inputText !== 'string') { // We might have inserted objects earlier, don't try to replace them
|
|
|
|
continue;
|
2017-11-14 23:34:20 +03:00
|
|
|
}
|
2017-05-30 18:17:42 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
const match = inputText.match(regexp);
|
|
|
|
if (!match) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-23 22:19:38 +03:00
|
|
|
matchFoundSomewhere = true;
|
2017-11-13 22:19:33 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
const capturedGroups = match.slice(2);
|
2017-11-14 21:33:12 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
// The textual part before the match
|
|
|
|
const head = inputText.substr(0, match.index);
|
2017-11-13 22:19:33 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
// The textual part after the match
|
|
|
|
const tail = inputText.substr(match.index + match[0].length);
|
2017-11-13 22:19:33 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
let replaced;
|
|
|
|
// If substitution is a function, call it
|
|
|
|
if (mapping[regexpString] instanceof Function) {
|
|
|
|
replaced = mapping[regexpString].apply(null, capturedGroups);
|
|
|
|
} else {
|
|
|
|
replaced = mapping[regexpString];
|
|
|
|
}
|
2017-11-13 22:19:33 +03:00
|
|
|
|
2017-11-17 10:47:25 +03:00
|
|
|
if (typeof replaced === 'object') {
|
|
|
|
shouldWrapInSpan = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.splice(outputIndex, 1); // Remove old element
|
|
|
|
|
|
|
|
// Insert in reverse order as splice does insert-before and this way we get the final order correct
|
|
|
|
if (tail !== '') {
|
|
|
|
output.splice(outputIndex, 0, tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we also need to check that it actually is a string before comparing against one
|
|
|
|
// The head and tail are always strings
|
|
|
|
if (typeof replaced !== 'string' || replaced !== '') {
|
|
|
|
output.splice(outputIndex, 0, replaced);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (head !== '') { // Don't push empty nodes, they are of no use
|
|
|
|
output.splice(outputIndex, 0, head);
|
|
|
|
}
|
2017-11-14 21:33:12 +03:00
|
|
|
}
|
2017-11-23 22:19:38 +03:00
|
|
|
if (!matchFoundSomewhere) { // The current regexp did not match anything in the input
|
|
|
|
// Missing matches is entirely possible because you might choose to show some variables only in the case
|
|
|
|
// of e.g. plurals. It's still a bit suspicious, and could be due to an error, so log it.
|
|
|
|
// However, not showing count is so common that it's not worth logging. And other commonly unused variables
|
|
|
|
// here, if there are any.
|
|
|
|
if (regexpString !== '%\\(count\\)s') {
|
|
|
|
console.log(`Could not find ${regexp} in ${text}`);
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 17:55:21 +03:00
|
|
|
}
|
2017-05-30 18:17:42 +03:00
|
|
|
|
2017-11-16 14:19:56 +03:00
|
|
|
if (shouldWrapInSpan) {
|
2017-11-13 22:19:33 +03:00
|
|
|
return React.createElement('span', null, ...output);
|
|
|
|
} else {
|
|
|
|
return output.join('');
|
|
|
|
}
|
2017-05-30 17:55:21 +03:00
|
|
|
}
|
|
|
|
|
2017-05-26 17:29:11 +03:00
|
|
|
// Allow overriding the text displayed when no translation exists
|
2017-06-01 04:04:43 +03:00
|
|
|
// Currently only used in unit tests to avoid having to load
|
2017-05-26 17:29:11 +03:00
|
|
|
// the translations in riot-web
|
|
|
|
export function setMissingEntryGenerator(f) {
|
|
|
|
counterpart.setMissingEntryGenerator(f);
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
export function setLanguage(preferredLangs) {
|
|
|
|
if (!Array.isArray(preferredLangs)) {
|
|
|
|
preferredLangs = [preferredLangs];
|
|
|
|
}
|
2017-05-23 17:16:31 +03:00
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
let langToUse;
|
|
|
|
let availLangs;
|
|
|
|
return getLangsJson().then((result) => {
|
|
|
|
availLangs = result;
|
2017-05-23 17:16:31 +03:00
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
for (let i = 0; i < preferredLangs.length; ++i) {
|
|
|
|
if (availLangs.hasOwnProperty(preferredLangs[i])) {
|
|
|
|
langToUse = preferredLangs[i];
|
2017-05-27 00:28:35 +03:00
|
|
|
break;
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 18:45:32 +03:00
|
|
|
if (!langToUse) {
|
2017-06-01 22:02:40 +03:00
|
|
|
// Fallback to en_EN if none is found
|
2017-10-11 19:56:17 +03:00
|
|
|
langToUse = 'en';
|
2017-06-01 22:02:40 +03:00
|
|
|
console.error("Unable to find an appropriate language");
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 21:46:25 +03:00
|
|
|
return getLanguage(i18nFolder + availLangs[langToUse].fileName);
|
2017-05-25 18:45:32 +03:00
|
|
|
}).then((langData) => {
|
|
|
|
counterpart.registerTranslations(langToUse, langData);
|
|
|
|
counterpart.setLocale(langToUse);
|
2017-11-04 08:19:45 +03:00
|
|
|
SettingsStore.setValue("language", null, SettingLevel.DEVICE, langToUse);
|
2017-05-25 18:45:32 +03:00
|
|
|
console.log("set language to " + langToUse);
|
|
|
|
|
|
|
|
// Set 'en' as fallback language:
|
2017-06-01 22:10:58 +03:00
|
|
|
if (langToUse != "en") {
|
|
|
|
return getLanguage(i18nFolder + availLangs['en'].fileName);
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
2017-05-25 18:45:32 +03:00
|
|
|
}).then((langData) => {
|
2017-06-01 22:10:58 +03:00
|
|
|
if (langData) counterpart.registerTranslations('en', langData);
|
2017-05-23 17:16:31 +03:00
|
|
|
});
|
2017-10-11 19:56:17 +03:00
|
|
|
}
|
2017-05-23 17:16:31 +03:00
|
|
|
|
2017-06-01 21:46:25 +03:00
|
|
|
export function getAllLanguagesFromJson() {
|
|
|
|
return getLangsJson().then((langsObject) => {
|
2017-10-11 19:56:17 +03:00
|
|
|
const langs = [];
|
|
|
|
for (const langKey in langsObject) {
|
2017-06-01 21:46:25 +03:00
|
|
|
if (langsObject.hasOwnProperty(langKey)) {
|
|
|
|
langs.push({
|
|
|
|
'value': langKey,
|
2017-10-11 19:56:17 +03:00
|
|
|
'label': langsObject[langKey].label,
|
2017-06-01 21:46:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return langs;
|
2017-05-25 18:45:32 +03:00
|
|
|
});
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
export function getLanguagesFromBrowser() {
|
2017-05-29 21:58:18 +03:00
|
|
|
if (navigator.languages && navigator.languages.length) return navigator.languages;
|
|
|
|
if (navigator.language) return [navigator.language];
|
2017-09-21 20:56:34 +03:00
|
|
|
return [navigator.userLanguage || "en"];
|
2017-05-29 21:58:18 +03:00
|
|
|
}
|
2017-05-23 17:16:31 +03:00
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
/**
|
|
|
|
* Turns a language string, normalises it,
|
|
|
|
* (see normalizeLanguageKey) into an array of language strings
|
|
|
|
* with fallback to generic languages
|
|
|
|
* (eg. 'pt-BR' => ['pt-br', 'pt'])
|
|
|
|
*
|
|
|
|
* @param {string} language The input language string
|
|
|
|
* @return {string[]} List of normalised languages
|
|
|
|
*/
|
2017-05-23 20:32:45 +03:00
|
|
|
export function getNormalizedLanguageKeys(language) {
|
2017-05-23 17:16:31 +03:00
|
|
|
const languageKeys = [];
|
|
|
|
const normalizedLanguage = this.normalizeLanguageKey(language);
|
|
|
|
const languageParts = normalizedLanguage.split('-');
|
2017-05-25 18:45:32 +03:00
|
|
|
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
|
2017-05-23 17:16:31 +03:00
|
|
|
languageKeys.push(languageParts[0]);
|
|
|
|
} else {
|
|
|
|
languageKeys.push(normalizedLanguage);
|
2017-05-25 18:45:32 +03:00
|
|
|
if (languageParts.length == 2) {
|
2017-05-23 17:16:31 +03:00
|
|
|
languageKeys.push(languageParts[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return languageKeys;
|
2017-10-11 19:56:17 +03:00
|
|
|
}
|
2017-05-23 17:16:31 +03:00
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
/**
|
|
|
|
* Returns a language string with underscores replaced with
|
|
|
|
* hyphens, and lowercased.
|
|
|
|
*/
|
2017-05-23 20:32:45 +03:00
|
|
|
export function normalizeLanguageKey(language) {
|
2017-10-11 19:56:17 +03:00
|
|
|
return language.toLowerCase().replace("_", "-");
|
|
|
|
}
|
2017-05-25 18:45:32 +03:00
|
|
|
|
2017-05-25 21:53:27 +03:00
|
|
|
export function getCurrentLanguage() {
|
|
|
|
return counterpart.getLocale();
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
function getLangsJson() {
|
2017-08-13 03:56:37 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-03-01 14:44:44 +03:00
|
|
|
let url;
|
|
|
|
try {
|
2019-03-01 15:03:04 +03:00
|
|
|
// $webapp is a webpack resolve alias pointing to the output directory, see webpack config
|
|
|
|
url = require('$webapp/i18n/languages.json');
|
2019-03-01 14:44:44 +03:00
|
|
|
} catch (e) {
|
|
|
|
url = i18nFolder + 'languages.json';
|
|
|
|
}
|
2017-08-13 03:56:37 +03:00
|
|
|
request(
|
2019-02-20 11:36:18 +03:00
|
|
|
{ method: "GET", url },
|
2017-08-13 03:56:37 +03:00
|
|
|
(err, response, body) => {
|
|
|
|
if (err || response.status < 200 || response.status >= 300) {
|
|
|
|
reject({err: err, response: response});
|
2017-08-24 17:42:32 +03:00
|
|
|
return;
|
2017-08-13 03:56:37 +03:00
|
|
|
}
|
|
|
|
resolve(JSON.parse(body));
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2017-08-13 03:56:37 +03:00
|
|
|
);
|
|
|
|
});
|
2017-05-25 18:45:32 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 17:55:58 +03:00
|
|
|
function weblateToCounterpart(inTrs) {
|
|
|
|
const outTrs = {};
|
|
|
|
|
|
|
|
for (const key of Object.keys(inTrs)) {
|
|
|
|
const keyParts = key.split('|', 2);
|
|
|
|
if (keyParts.length === 2) {
|
|
|
|
let obj = outTrs[keyParts[0]];
|
|
|
|
if (obj === undefined) {
|
|
|
|
obj = {};
|
|
|
|
outTrs[keyParts[0]] = obj;
|
|
|
|
}
|
|
|
|
obj[keyParts[1]] = inTrs[key];
|
|
|
|
} else {
|
|
|
|
outTrs[key] = inTrs[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outTrs;
|
|
|
|
}
|
|
|
|
|
2017-05-25 18:45:32 +03:00
|
|
|
function getLanguage(langPath) {
|
2017-08-13 03:56:37 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request(
|
|
|
|
{ method: "GET", url: langPath },
|
|
|
|
(err, response, body) => {
|
|
|
|
if (err || response.status < 200 || response.status >= 300) {
|
|
|
|
reject({err: err, response: response});
|
2017-08-24 17:42:32 +03:00
|
|
|
return;
|
2017-08-13 03:56:37 +03:00
|
|
|
}
|
2017-11-01 17:55:58 +03:00
|
|
|
resolve(weblateToCounterpart(JSON.parse(body)));
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2017-08-13 03:56:37 +03:00
|
|
|
);
|
|
|
|
});
|
2017-05-25 18:45:32 +03:00
|
|
|
}
|