Skip to content

Commit

Permalink
feat: plurals support for localisation
Browse files Browse the repository at this point in the history
requires a "one" and a "many", implements declension for russian and
polish. localisations that havent been updated will still work ૮˶• ﻌ •˶ა

Co-authored-by: Andrii Beznosiuk <[email protected]>
  • Loading branch information
puppy-girl and hydall committed Sep 24, 2024
1 parent 7fe55fc commit eb10c8c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lang/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"stats-body-ttk": "躯干击杀所需子弹数",
"stats-range": "{{distance}}米",
"stats-time": "{{duration}}秒",
"stats-shots": "{{shots}} 发",
"stats-shots": "{{count}} 发",
"enemy-swat": "特警",
"enemy-heavy-swat": "重型特警",
"enemy-specials": "特种特警",
Expand Down
5 changes: 4 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@
"stats-body-ttk": "Body Shots to Kill",
"stats-range": "{{distance}}m",
"stats-time": "{{duration}}s",
"stats-shots": "{{shots}} shots",
"stats-shots": {
"one": "{{count}} shot",
"many": "{{count}} shots"
},
"enemy-swat": "SWAT",
"enemy-heavy-swat": "Heavy SWAT",
"enemy-specials": "Specials",
Expand Down
8 changes: 5 additions & 3 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
"stats-body-ttk": "Liczba strzałów w ciało do zabicia",
"stats-range": "{{distance}}m",
"stats-time": "{{duration}}s",
"stats-1shot": "strzał",
"stats-few-shots": "strzały",
"stats-many-shots": "strzałów",
"stats-shots": {
"one": "{{count}} strzał",
"few": "{{count}} strzały",
"many": "{{count}} strzałów"
},
"enemy-swat": "SWAT",
"enemy-heavy-swat": "Ciężki SWAT",
"enemy-specials": "Jednostki specjalne",
Expand Down
8 changes: 5 additions & 3 deletions lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
"stats-body-ttk": "Выстрелы в тело для убийства",
"stats-range": "{{distance}}м.",
"stats-time": "{{duration}}с.",
"stats-1shot": "выстрел",
"stats-few-shots": "выстрела",
"stats-many-shots": "выстрелов",
"stats-shots": {
"one": "{{count}} выстрел",
"few": "{{count}} выстрела",
"many": "{{count}} выстрелов"
},
"enemy-swat": "SWAT",
"enemy-heavy-swat": "Тяжелый SWAT",
"enemy-specials": "Спец. юниты",
Expand Down
2 changes: 1 addition & 1 deletion lang/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"stats-body-ttk": "Leş için Vücut Atım Sayısı",
"stats-range": "{{distance}}m",
"stats-time": "{{duration}}sn",
"stats-shots": "{{shots}} atım",
"stats-shots": "{{count}} atım",
"enemy-swat": "SWAT",
"enemy-heavy-swat": "Ağır SWAT",
"enemy-specials": "Özel Timler",
Expand Down
6 changes: 4 additions & 2 deletions scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1517,8 +1517,9 @@ function updateDamageStats(selectedWeapon) {
damageStat.setAttribute('data-localisation-key', 'stats-shots');
damageStat.setAttribute(
'data-localisation-var',
`{"shots": "${totalShots}"}`
`{"count": "${totalShots}"}`
);
damageStat.setAttribute('data-localisation-count', totalShots);

const damageBreakdown = shotStats.appendChild(
document.createElement('span')
Expand Down Expand Up @@ -1602,8 +1603,9 @@ function updateDamageStats(selectedWeapon) {
damageStat.setAttribute('data-localisation-key', 'stats-shots');
damageStat.setAttribute(
'data-localisation-var',
`{"shots": "${totalShots}"}`
`{"count": "${totalShots}"}`
);
damageStat.setAttribute('data-localisation-count', totalShots);

const damageBreakdown = shotStats.appendChild(
document.createElement('span')
Expand Down
63 changes: 31 additions & 32 deletions scripts/localisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,14 @@ function localise(element) {

let localisation = getLocalisation(key) || '';

const variables = JSON.parse(element.getAttribute('data-localisation-var'));
if (typeof localisation == 'object')
localisation = getPluralForm(
localisation,
element.getAttribute('data-localisation-count')
);

if (variables) {
if (
key == 'stats-shots' &&
['russian', 'polish'].includes(currentLocale)
)
return (element.innerText = shotsDeclension(variables));

for (const variable in variables) {
localisation = localisation.replaceAll(
`{{${variable}}}`,
variables[variable]
);
}
}
const variables = JSON.parse(element.getAttribute('data-localisation-var'));
localisation = interpolateLocalisation(localisation, variables);

return (element.innerText = localisation);
}
Expand All @@ -83,22 +75,29 @@ function getLocalisation(key) {
return localisations[locale][key];
}

function shotsDeclension(count) {
count = count.shots;
const lastDigit = count % 10;
const lastTwoDigits = count % 100;

if (lastDigit === 1 && lastTwoDigits !== 11) {
let shotsTranslated = getLocalisation('stats-1shot') || '';
return `${count} ${shotsTranslated}`;
} else if (
[2, 3, 4].includes(lastDigit) &&
![12, 13, 14].includes(lastTwoDigits)
) {
let shotsTranslated = getLocalisation('stats-few-shots') || '';
return `${count} ${shotsTranslated}`;
} else {
let shotsTranslated = getLocalisation('stats-many-shots') || '';
return `${count} ${shotsTranslated}`;
function getPluralForm(localisation, count) {
if (['ru', 'pl'].includes(currentLocale)) {
if (count % 10 == 1 && count % 100 != 11) return localisation['one'];

if (
[2, 3, 4].includes(count % 10) &&
![12, 13, 14].includes(count % 100)
)
return localisation['few'];
}

if (count == 1) return localisation['one'];

return localisation['many'];
}

function interpolateLocalisation(localisation, variables) {
for (const variable in variables) {
localisation = localisation.replaceAll(
`{{${variable}}}`,
variables[variable]
);
}

return localisation;
}

0 comments on commit eb10c8c

Please sign in to comment.