Move translation function to locale module; load translations from language files

This commit is contained in:
Martin Kleinschrodt 2019-08-13 18:34:22 +02:00
parent 0c14eae434
commit 781a10eeeb
58 changed files with 3234 additions and 2690 deletions

3957
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,7 +15,7 @@
"packages/*"
],
"devDependencies": {
"lerna": "^3.13.4",
"lerna": "^3.16.4",
"typescript": "^3.4.3"
},
"dependencies": {},

View File

@ -955,12 +955,14 @@
"@types/node": {
"version": "12.6.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.6.2.tgz",
"integrity": "sha512-gojym4tX0FWeV2gsW4Xmzo5wxGjXGm550oVUII7f7G5o4BV6c7DBdiG1RRQd+y1bvqRyYtPfMK85UM95vsapqQ=="
"integrity": "sha512-gojym4tX0FWeV2gsW4Xmzo5wxGjXGm550oVUII7f7G5o4BV6c7DBdiG1RRQd+y1bvqRyYtPfMK85UM95vsapqQ==",
"dev": true
},
"@types/papaparse": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@types/papaparse/-/papaparse-5.0.0.tgz",
"integrity": "sha512-JxYinxHA59+Vk3H61fjrPp1v0tjeT+WcyL/TGYDzmiig8125ndlbI5c6ZKVEaXQbsU8RF3TyCvH7tWsG2bI/Nw==",
"dev": true,
"requires": {
"@types/node": "*"
}
@ -1011,12 +1013,14 @@
"@types/workbox-sw": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/@types/workbox-sw/-/workbox-sw-4.2.0.tgz",
"integrity": "sha512-eKk2yrlCuLNmBInQ08FqIcRlMT1ghIo2P7wZW/jAQw0l9ozPYrebcqxnsg2NN+TGRVcMEYGfP05AxmIaxQIArg=="
"integrity": "sha512-eKk2yrlCuLNmBInQ08FqIcRlMT1ghIo2P7wZW/jAQw0l9ozPYrebcqxnsg2NN+TGRVcMEYGfP05AxmIaxQIArg==",
"dev": true
},
"@types/workbox-window": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/@types/workbox-window/-/workbox-window-4.3.1.tgz",
"integrity": "sha512-6xHJhIkDTrnAWbNR5Td6X7/WQje/MLvv0jMAtHPU508hepcX8NdiXOCsyYBhomcVOTyX19B6/xa+br+UfTFE2g=="
"integrity": "sha512-6xHJhIkDTrnAWbNR5Td6X7/WQje/MLvv0jMAtHPU508hepcX8NdiXOCsyYBhomcVOTyX19B6/xa+br+UfTFE2g==",
"dev": true
},
"@types/zxcvbn": {
"version": "4.4.0",
@ -1315,6 +1319,15 @@
"integrity": "sha512-Y9J6ZjXtoYh8RnXVCMOU/ttDmk1aBjunq9vO0ta5x85WDQiQfUF9sIPBITdbiiIVcBo03Hi3jMxigBtsddlXRw==",
"dev": true
},
"argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
"dev": true,
"requires": {
"sprintf-js": "~1.0.2"
}
},
"arr-diff": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/arr-diff/-/arr-diff-4.0.0.tgz",
@ -3330,6 +3343,12 @@
"estraverse": "^4.1.1"
}
},
"esprima": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"dev": true
},
"esrecurse": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.2.1.tgz",
@ -5658,6 +5677,16 @@
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
"dev": true
},
"js-yaml": {
"version": "3.13.1",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz",
"integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==",
"dev": true,
"requires": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
}
},
"jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
@ -8650,6 +8679,12 @@
"extend-shallow": "^3.0.0"
}
},
"sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
"dev": true
},
"sshpk": {
"version": "1.16.1",
"resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz",
@ -10725,6 +10760,15 @@
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=",
"dev": true
},
"yaml-loader": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/yaml-loader/-/yaml-loader-0.5.0.tgz",
"integrity": "sha512-p9QIzcFSNm4mCw/m5NdyMfN4RE4aFZJWRRb01ERVNGCym8VNbKtw3OYZXnvUIkim6U/EjqE/2yIh9F/msShH9A==",
"dev": true,
"requires": {
"js-yaml": "^3.5.2"
}
},
"yargs": {
"version": "13.2.4",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-13.2.4.tgz",

View File

@ -6,6 +6,7 @@
"license": "GPL-3.0",
"dependencies": {
"@padloc/core": "^3.0.0",
"@padloc/locale": "^3.0.0",
"@webcomponents/webcomponentsjs": "^2.0.0",
"autosize": "^4.0.2",
"date-fns": "^1.30.1",
@ -20,12 +21,12 @@
"zxcvbn": "^4.4.2"
},
"devDependencies": {
"@types/papaparse": "^5.0.0",
"@types/workbox-sw": "^4.2.0",
"@types/workbox-window": "^4.3.1",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/papaparse": "^5.0.0",
"@types/ua-parser-js": "^0.7.33",
"@types/workbox-sw": "^4.2.0",
"@types/workbox-window": "^4.3.1",
"@types/zxcvbn": "^4.4.0",
"chai": "^4.2.0",
"clean-webpack-plugin": "^3.0.0",

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { BaseElement } from "./elements/base";
import "./elements/generator";
import "./elements/alert-dialog";

View File

@ -1,6 +1,6 @@
import "../../assets/fonts/fonts.css";
import { Plan } from "@padloc/core/src/billing";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { biometricAuth } from "@padloc/core/src/platform";
import { config, shared, mixins } from "../styles";
import { app, router } from "../init";

View File

@ -1,5 +1,5 @@
import { TemplateResult } from "lit-element";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { VaultItemID } from "@padloc/core/src/item";
import { Attachment, AttachmentInfo } from "@padloc/core/src/attachment";
import { app } from "../init";

View File

@ -1,5 +1,5 @@
import { Attachment, AttachmentInfo } from "@padloc/core/src/attachment";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { RequestProgress } from "@padloc/core/src/transport";
import { shared, mixins } from "../styles";
// import { app } from "../init";

View File

@ -1,4 +1,5 @@
import { localize as $l, countries } from "@padloc/core/src/locale";
import { countries } from "@padloc/locale/src/countries";
import { translate as $l } from "@padloc/locale/src/translate";
import { BillingInfo, BillingAddress, UpdateBillingParams } from "@padloc/core/src/billing";
import { loadScript } from "../util";
import { app } from "../init";

View File

@ -1,4 +1,5 @@
import { countries, localize as $l } from "@padloc/core/src/locale";
import { countries } from "@padloc/locale/src/countries";
import { translate as $l } from "@padloc/locale/src/translate";
import { BillingInfo } from "@padloc/core/src/billing";
import { shared } from "../styles";
import { dialog, alert } from "../dialog";
@ -34,7 +35,7 @@ export class BillingInfoElement extends BaseElement {
this._editButton.success();
} catch (e) {
this._editButton.fail();
alert(e.message || $l("Something went wrong. Please try again later!"), {type: "warning"});
alert(e.message || $l("Something went wrong. Please try again later!"), { type: "warning" });
throw e;
}
}

View File

@ -1,5 +1,5 @@
import { unsafeHTML } from "lit-html/directives/unsafe-html";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Plan, PlanType } from "@padloc/core/src/billing";
import { mixins } from "../styles";
import { app } from "../init";

View File

@ -1,6 +1,6 @@
import { VaultItem, Field } from "@padloc/core/src/item";
import { setClipboard } from "@padloc/core/src/platform";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { totp } from "@padloc/core/src/otp";
import { base32ToBytes } from "@padloc/core/src/encoding";
import { shared, mixins } from "../styles";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Org } from "@padloc/core/src/org";
import { Invite } from "@padloc/core/src/invite";
import { app } from "../init";

View File

@ -1,6 +1,6 @@
import { Vault } from "@padloc/core/src/vault";
import { VaultItem, ItemTemplate, ITEM_TEMPLATES } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { app } from "../init";
import { alert } from "../dialog";
import { element, html, css, query } from "./base";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Org } from "@padloc/core/src/org";
import { Plan, BillingInfo, UpdateBillingParams } from "@padloc/core/src/billing";
import { dialog } from "../dialog";

View File

@ -1,5 +1,5 @@
import { Vault } from "@padloc/core/src/vault";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { CSV, PBES2, ImportFormat } from "../import";
import { supportedFormats, asCSV, asPBES2Container } from "../export";
import { app } from "../init";

View File

@ -1,5 +1,5 @@
import { FieldType, FIELD_DEFS } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { element, html, css } from "./base";
import { Dialog } from "./dialog";

View File

@ -1,5 +1,5 @@
import { FieldType, FIELD_DEFS } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { shared } from "../styles";
import { BaseElement, element, html, css, property, query } from "./base";
import "./icon";

View File

@ -1,6 +1,6 @@
import { randomString, chars } from "@padloc/core/src/util";
import { generatePassphrase } from "@padloc/core/src/diceware";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { html, css, property, query, listen } from "./base";
import { Dialog } from "./dialog";
import { Slider } from "./slider";

View File

@ -1,5 +1,5 @@
import { Org, OrgMember, Group } from "@padloc/core/src/org";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { app } from "../init";
import { prompt } from "../dialog";
import { element, html, css, property, query } from "./base";

View File

@ -1,6 +1,6 @@
import { Vault } from "@padloc/core/src/vault";
import { VaultItem } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import * as imp from "../import";
import { prompt, alert } from "../dialog";
import { app } from "../init";

View File

@ -1,6 +1,6 @@
import { until } from "lit-html/directives/until";
import { Invite } from "@padloc/core/src/invite";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { formatDateFromNow } from "../util";
import { app, router } from "../init";
import { alert, dialog } from "../dialog";

View File

@ -1,6 +1,6 @@
import { until } from "lit-html/directives/until";
import { Invite } from "@padloc/core/src/invite";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { formatDateFromNow } from "../util";
import { shared } from "../styles";
import { app } from "../init";

View File

@ -1,6 +1,6 @@
import { until } from "lit-html/directives/until";
import { VaultItemID, Field } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { AttachmentInfo } from "@padloc/core/src/attachment";
import { parseURL } from "@padloc/core/src/otp";
import { formatDateFromNow, fileIcon, fileSize } from "../util";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { VaultID } from "@padloc/core/src/vault";
import { Tag } from "@padloc/core/src/item";
import { StateMixin } from "../mixins/state";

View File

@ -1,6 +1,6 @@
import { VaultItem, Field, Tag, FIELD_DEFS } from "@padloc/core/src/item";
import { Vault, VaultID } from "@padloc/core/src/vault";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { debounce, wait, escapeRegex } from "@padloc/core/src/util";
import { AttachmentInfo } from "@padloc/core/src/attachment";
import { cache } from "lit-html/directives/cache";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { ErrorCode } from "@padloc/core/src/error";
import { app, router } from "../init";
import { element, html, css, property, query } from "./base";

View File

@ -1,6 +1,6 @@
import { Org, OrgMember, OrgRole, Group } from "@padloc/core/src/org";
import { VaultID } from "@padloc/core/src/vault";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { mixins } from "../styles";
import { app } from "../init";
import { confirm, choose } from "../dialog";

View File

@ -1,5 +1,5 @@
import { OrgMember, OrgRole } from "@padloc/core/src/org";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { shared } from "../styles";
import { BaseElement, element, html, css, property } from "./base";
import "./randomart";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { PlanType } from "@padloc/core/src/billing";
import { app, router } from "../init";
import { shared, mixins } from "../styles";

View File

@ -1,6 +1,6 @@
import { Vault } from "@padloc/core/src/vault";
import { VaultItem } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { app } from "../init";
import { element, property, html, css, query } from "./base";
import { Select } from "./select";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Invite } from "@padloc/core/src/invite";
import { OrgMember, OrgRole, Group } from "@padloc/core/src/org";
import { BillingInfo } from "@padloc/core/src/billing";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { StateMixin } from "../mixins/state";
import { app, router } from "../init";
import { element, html, css } from "./base";

View File

@ -1,5 +1,5 @@
import { unsafeHTML } from "lit-html/directives/unsafe-html";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { BillingInfo, Plan, PlanType, UpdateBillingParams } from "@padloc/core/src/billing";
import { dialog } from "../dialog";
import { mixins } from "../styles";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { scanQR, stopScanQR } from "@padloc/core/src/platform";
import { mixins } from "../styles";
import { alert } from "../dialog";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { ErrorCode } from "@padloc/core/src/error";
import { EmailVerificationPurpose } from "@padloc/core/src/email-verification";
import { app, router } from "../init";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { BillingInfo } from "@padloc/core/src/billing";
import { mixins } from "../styles";
import { alert, confirm, prompt, dialog } from "../dialog";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { ErrorCode } from "@padloc/core/src/error";
import { generatePassphrase } from "@padloc/core/src/diceware";
import { passwordStrength, isTouch } from "../util";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Org } from "@padloc/core/src/org";
import { PlanType, SubscriptionStatus, UpdateBillingParams, Subscription } from "@padloc/core/src/billing";
import { shared } from "../styles";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Vault } from "@padloc/core/src/vault";
import { Tag } from "@padloc/core/src/item";
import { app } from "../init";

View File

@ -1,5 +1,5 @@
import { ItemTemplate, ITEM_TEMPLATES } from "@padloc/core/src/item";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { element, html, css } from "./base";
import { Dialog } from "./dialog";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { ErrorCode } from "@padloc/core/src/error";
import { isBiometricAuthAvailable, biometricAuth } from "@padloc/core/src/platform";
import { wait } from "@padloc/core/src/util";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { Org } from "@padloc/core/src/org";
import { Plan, UpdateBillingParams } from "@padloc/core/src/billing";
import { dialog } from "../dialog";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { VaultItemID } from "@padloc/core/src/item";
import { Attachment } from "@padloc/core/src/attachment";
import { ErrorCode } from "@padloc/core/src/error";

View File

@ -1,6 +1,6 @@
import { VaultID } from "@padloc/core/src/vault";
import { Org, Group, OrgMember } from "@padloc/core/src/org";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { mixins } from "../styles";
import { app } from "../init";
import { prompt } from "../dialog";

View File

@ -4,7 +4,7 @@ import { validateLegacyContainer, parseLegacyContainer } from "@padloc/core/src/
import { VaultItem, Field, createVaultItem, guessFieldType } from "@padloc/core/src/item";
import { Err, ErrorCode } from "@padloc/core/src/error";
import { uuid } from "@padloc/core/src/util";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
export interface ImportFormat {
format: "csv" | "padlock-legacy" | "lastpass" | "padloc";

View File

@ -1,4 +1,4 @@
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
export function trialingMessage(days: number) {
return $l(

View File

@ -1,5 +1,5 @@
import { Err, ErrorCode } from "@padloc/core/src/error";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { app, router } from "../init";
import { alert, confirm } from "../dialog";
// import { notify } from "../elements/notification";

View File

@ -1,5 +1,5 @@
import { Workbox } from "workbox-window";
import { localize as $l } from "@padloc/core/src/locale";
import { translate as $l } from "@padloc/locale/src/translate";
import { confirm } from "../dialog";
type Constructor<T> = new (...args: any[]) => T;

View File

@ -32,6 +32,14 @@ module.exports = {
{
test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
use: ["file-loader"]
},
{
test: /\.lang$/,
use: [
{
loader: path.resolve("../locale/loaders/lang-loader.js")
}
]
}
]
},

View File

@ -6,6 +6,9 @@
"author": "Martin Kleinschrodt <martin@maklesoft.com>",
"license": "GPLv3",
"private": false,
"dependencies": {
"@padloc/locale": "^3.0.0"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",

View File

@ -0,0 +1,7 @@
const { parse } = require("yaml");
module.exports = function(source) {
const items = parse(source);
return `export default ${JSON.stringify(items)}`;
};

View File

@ -1,6 +1,6 @@
{
"name": "@padloc/tooling",
"version": "1.0.0",
"name": "@padloc/locale",
"version": "3.0.0",
"description": "",
"main": "index.js",
"scripts": {

File diff suppressed because it is too large Load Diff

View File

@ -1,50 +1,3 @@
import { resolveLanguage } from "./util";
import { getDeviceInfo } from "./platform";
/**
* Simple map of translations
*/
export interface Translations {
[lang: string]: { [msg: string]: string };
}
let translations: Translations = {};
let language: string;
/**
* Translate `msg` into the current language. The message can contain simple numbered
* placeholders that are substituted after translation with the corresponding arguments
* passed after `msg`. E.g:
*
* ```ts
* localize("Hello! My name is {0}. I am from {1}. How are you?", name, country);
* ```
*/
export function localize(msg: string, ...fmtArgs: string[]) {
// Choose translations for current language
const lang = translations[language];
// Look up translation. If no translation is found, use the original message.
let res = (lang && lang[msg]) || msg;
// Replace placeholders with function arguments
for (let i = 0; i < fmtArgs.length; i++) {
res = res.replace(new RegExp(`\\{${i}\\}`, "g"), fmtArgs[i]);
}
return res;
}
/**
* Set available translations. This function will usually be called by an
* auto-generated script which is loaded during app initialization, removing
* the need for asynchronous fetching of translation files.
*/
export async function loadTranslations(t: Translations) {
translations = t;
language = resolveLanguage((await getDeviceInfo()).locale, translations);
}
export const countries = [
{ code: "AF", name: "Afghanistan" },
{ code: "AX", name: "Åland Islands" },

View File

@ -159,7 +159,7 @@ export function merge(curr: Translation, prev: Translation) {
}
export function updateTranslation(sources: string[], language: string, dest: string) {
const destPath = resolve(dest, language + ".yaml");
const destPath = resolve(dest, language + ".lang");
// const backupPath = resolve(dest, language + "_backup.yaml");
const translation = fromSource(sources, language);

View File

@ -0,0 +1,65 @@
const loadLanguagePromises = new Map<string, Promise<void>>();
const languages = new Map<string, Map<string, string>>();
let defaultLanguage = "en";
export async function loadLanguage(lang: string, setDefault = true): Promise<void> {
if (loadLanguagePromises.has(lang)) {
return loadLanguagePromises.get(lang);
}
const promise = (async () => {
const { default: items } = await import(`../res/translations/${lang}.lang`);
languages.set(lang, new Map<string, string>(items));
if (setDefault) {
defaultLanguage = lang;
}
})();
loadLanguagePromises.set(lang, promise);
return promise;
}
/**
* Resolves a given locale string to the approprivate available language
*/
export function resolveLanguage(locale: string, supportedLanguages: { [lang: string]: any }): string {
const localeParts = locale.toLowerCase().split("-");
while (localeParts.length) {
const l = localeParts.join("-");
if (supportedLanguages[l]) {
return l;
}
localeParts.pop();
}
return Object.keys(supportedLanguages)[0];
}
/**
* Translate `msg` into the current language. The message can contain simple numbered
* placeholders that are substituted after translation with the corresponding arguments
* passed after `msg`. E.g:
*
* ```ts
* translate("Hello! My name is {0}. I am from {1}. How are you?", name, country);
* ```
*/
export function translate(msg: string, language = defaultLanguage, ...fmtArgs: string[]) {
// Choose translations for current language
const lang = languages.get(language);
// Look up translation. If no translation is found, use the original message.
let res = (lang && lang.get(msg)) || msg;
// Replace placeholders with function arguments
for (let i = 0; i < fmtArgs.length; i++) {
res = res.replace(new RegExp(`\\{${i}\\}`, "g"), fmtArgs[i]);
}
return res;
}

View File

@ -3,7 +3,7 @@
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"allowJs": true,
"declaration": "false",
"baseUrl": "."
},
"include": ["src/*.ts"],