Translations keyed by labels instead of English text
tr('pnis.delivery_2_to_6_weeks') instead of tr('Delivery: 2 to 6 weeks'): the
English lives once, in src/i18n/en.js (inlined into the page at build, so no
extra request), and it/pt/fr map the same labels. Changing English wording no
longer renames a key in every dictionary.
- 2,899 labels, made from where each text is used and its first words; short
shared words are common.*
- plurals are one label with {one, other}: trn('label', n)
- static markup: data-i18n="label", data-i18n-aria-label, data-i18n-title
- saves from 1.33 and older stored English text in a few places; tr() still
resolves it
- i18n-check: undefined/unused labels, markup drift, plural shape; i18n-add.js
works on labels and --rename updates src/ too
- the old-branch port tool gives old English-keyed code and dictionaries the
same labels
This commit is contained in:
parent
d0ba85ef8f
commit
e817beed1a
94 changed files with 13681 additions and 10633 deletions
|
|
@ -1,11 +1,18 @@
|
|||
/* ================= LANGUAGE =================
|
||||
English is the source text: tr(text,vars) looks it up in the dictionary that
|
||||
site/i18n/<lang>.js registered and falls back to the English. trn() picks the singular or plural
|
||||
form first; N_() only marks text that doubles as an id, so it can be translated where it is shown. */
|
||||
Text is looked up by label: tr('pnis.delivery_2_to_6_weeks',vars) returns the player's language, or the
|
||||
English from src/i18n/en.js (inlined above the game) when a translation is missing, and fills {placeholders}.
|
||||
trn(label,n,vars) picks the "one" or "other" form of a plural. N_() marks a label kept in a table or a
|
||||
save, translated where it is shown. A label that is not in any dictionary comes back as it is. */
|
||||
// English when the dictionary did not load (offline, or a copy without the i18n folder).
|
||||
const LANGS=window.SOT_LANGS,LANG=window.I18N?.[window.SOT_LANG]?window.SOT_LANG:'en';
|
||||
document.documentElement.lang=LANG;
|
||||
const DICT=Object.assign(Object.create(null),window.I18N?.[LANG]);
|
||||
function tr(s,v){const o=DICT[s]||s;return v?o.replace(/\{(\w+)\}/g,(m,k)=>k in v?v[k]:m):o;}
|
||||
function trn(one,other,n,v){return tr(n===1?one:other,{n,...v});}
|
||||
const EN=window.I18N.en,DICT=Object.assign(Object.create(null),EN,LANG!=='en'&&window.I18N[LANG]);
|
||||
// Saves from 1.33 and older hold English text where newer ones hold a label: map it on first use.
|
||||
let EN_TEXT=null;
|
||||
function trKey(k){const o=DICT[k];if(o!==undefined)return o;
|
||||
if(!EN_TEXT){EN_TEXT=new Map();for(const l in EN){const e=EN[l],t=DICT[l];if(typeof e==='string')EN_TEXT.set(e,t);else{EN_TEXT.set(e.one,t.one||e.one);EN_TEXT.set(e.other,t.other||e.other);}}}
|
||||
return EN_TEXT.get(k)??k;}
|
||||
const fillIn=(o,v)=>v?o.replace(/\{(\w+)\}/g,(m,k)=>k in v?v[k]:m):o;
|
||||
function tr(k,v){const o=trKey(k);return fillIn(typeof o==='string'?o:o.other,v);}
|
||||
function trn(k,n,v){const o=trKey(k);return fillIn(typeof o==='string'?o:n===1?o.one:o.other,{n,...v});}
|
||||
function N_(s){return s;}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue