Skip to content

Commit 63cdfd3

Browse files
authored
support localised labels for CLDR overrides (openstreetmap#10703)
1 parent c7cc700 commit 63cdfd3

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

scripts/language_names.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@ const codesToSkip = ['ase', 'mis', 'mul', 'und', 'zxx'];
1515

1616
let referencedScripts = [];
1717

18-
function getLangNamesInNativeLang() {
18+
/**
19+
* @returns {{
20+
* [code: string]: {
21+
* base?: string;
22+
* script?: string;
23+
* nativeName?: string;
24+
* names?: { [code: string]: string };
25+
* }
26+
* }}
27+
*/
28+
function getCLDROverrides() {
1929
// manually add languages we want that aren't in CLDR
2030
// see for example https://github.com/openstreetmap/iD/pull/9241/
21-
let unordered = {
31+
return {
2232
aer: { nativeName: 'Arrernte' },
2333
aoi: { nativeName: 'Anindilyakwa' },
2434
aus: { nativeName: 'Australian Aboriginal Languages' },
@@ -32,7 +42,7 @@ function getLangNamesInNativeLang() {
3242
'brh': {
3343
nativeName: 'براہوئی'
3444
},
35-
coa: { nativeName: 'Basa Pulu Kokos' },
45+
coa: { nativeName: 'Basa Pulu Kokos', names: { en: 'Cocos Malay' } },
3646
'cdo': {
3747
nativeName: '閩東語'
3848
},
@@ -157,7 +167,7 @@ function getLangNamesInNativeLang() {
157167
'oc': {
158168
nativeName: 'Occitan'
159169
},
160-
pih: { nativeName: 'Pitkern–Norfuk' },
170+
pih: { nativeName: 'Pitkern–Norfuk', names: { en: 'Pitcairn-Norfolk', ty: 'Pitcairnais' } },
161171
piu: { nativeName: 'Pintupi' },
162172
pjt: { nativeName: 'Pitjantjatjara' },
163173
'pnb': {
@@ -174,7 +184,7 @@ function getLangNamesInNativeLang() {
174184
'skr': {
175185
nativeName: 'سرائیکی'
176186
},
177-
tcs: { nativeName: 'Yumplatok' },
187+
tcs: { nativeName: 'Yumplatok', names: { en: 'Torres Strait Creole' } },
178188
tiw: { nativeName: 'Tiwi' },
179189
'trw': {
180190
nativeName: 'توروالی'
@@ -202,7 +212,7 @@ function getLangNamesInNativeLang() {
202212
wyi: { nativeName: 'Woiwurrung' },
203213
xdk: { nativeName: 'Dharug' },
204214
xni: { nativeName: 'Ngarigo' },
205-
xph: { nativeName: 'Tyerrernotepanner' },
215+
xph: { nativeName: 'Tyerrernotepanner', names: { en: 'North Midlands Tasmanian' } },
206216
xrd: { nativeName: 'Gundungurra' },
207217
'yue-Hans': {
208218
base: 'yue',
@@ -221,6 +231,13 @@ function getLangNamesInNativeLang() {
221231
},
222232
zku: { nativeName: 'Kaurna' },
223233
};
234+
}
235+
236+
function getLangNamesInNativeLang() {
237+
const unordered = getCLDROverrides();
238+
for (const key in unordered) {
239+
delete unordered[key].names; // this is added later
240+
}
224241

225242
let langDirectoryPaths = fs.readdirSync(cldrMainDir);
226243
langDirectoryPaths.forEach(code => {
@@ -274,11 +291,20 @@ exports.langNamesInNativeLang = langNamesInNativeLang;
274291
exports.languageNamesInLanguageOf = function(code) {
275292
if (rematchCodes[code]) code = rematchCodes[code];
276293

294+
const { language } = new Intl.Locale(code);
295+
277296
let languageFilePath = `${cldrMainDir}${code}/languages.json`;
278297
if (!fs.existsSync(languageFilePath)) return null;
279298

280299
let translatedLangsByCode = JSON.parse(fs.readFileSync(languageFilePath, 'utf8')).main[code].localeDisplayNames.languages;
281300

301+
// add any overrides that have translated names
302+
for (const [key, value] of Object.entries(getCLDROverrides())) {
303+
if (value.names?.[language]) {
304+
translatedLangsByCode[key] ||= value.names?.[language];
305+
}
306+
}
307+
282308
// ignore codes for non-languages
283309
codesToSkip.forEach(skipCode => {
284310
delete translatedLangsByCode[skipCode];

0 commit comments

Comments
 (0)