Skip to content

consider name:* tags with the full locale code when labelling features - #10911

Merged
tyrasd merged 2 commits into
developfrom
kh/script2
Jun 23, 2026
Merged

tyrasd merged 2 commits into
developfrom
kh/script2

Conversation

@k-yle

@k-yle k-yle commented Mar 24, 2025 •

Copy link
Copy Markdown
Collaborator

Closes #10219, Closes #10882, Closes #11315, Closes #11731, Closes #11082 (superseeds that PR)

Thanks to Intl.Locale#maximize, we can expand the user's locale code to add likely subtags, then try every combination of language [+ script [ +region]] until we find a matching name:* tag.

 

For example, we can expand zh-CN to zh-Hans-CN, and expand zh-TW to zh-Hant-TW, without hardcoding these cases.

Then, we use the most appropriate name:* tag, first checking name:zh-Hans-CN, then name:zh-CN, then name:zh-Hans, then name:zh.

This also fixes the pt-BR case from #10882

@tyrasd tyrasd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It improves the current implementation significantly, so 👍 from my side. Feel free to merge as is, or to address the comments below.

  1. while this works well for the case reported in #10882, it does not seem to fully address the original issue from #10219:
  • the common keys for Chinese names include ones with script codes (e.g. name:zh-Hans or name:zh-Hant), but if, for example, a user has navigator.languages = ['zh-Hans-CN'], the labels would not be shown for tags like name:zh-Hans.
  1. It feels slightly odd that this is tied to iD's UI translations.
  • Let's say a user has navigator.language == 'lld'. They will get iD in en, as that's the best available fallback for UI translations. Ideally, they should see labels in lld
  • Similarly, if navigator.languages = ['de-AT', 'de']. iD would be in de, but why would the user not get labels for de-AT
  1. Also, what's your opinion to extend this even further:
  • when a user has multiple languages in their navigator.languages, we could check the full fallback list: e.g. when a user has navigator.languages = ['lld', 'it', 'de', 'en'], features could be labeled by the respective languages in that order
  • we could consider "looking ahead" for any region codes in the name: tag. e.g. if a user has navigator.languages = ['pt'], it might be ok to also display a label for a name:pt-BR tag, if there is no name:pt
  • same as the previous point, but for scripts: if navigator.languages = ['zh-Hans-CN'], (then localizer.localeCodes() == ['zh-CN'],) it would be ok to display labels in name:zh-Hans, maybe even in other scripts like name:zh-Hant or name:zh-Latn-pinyin

@tyrasd

tyrasd commented Mar 24, 2025

Copy link
Copy Markdown
Member

PS: do you have a clue what could cause this CI error?

 Error: AssertionError: expected [ 'en' ] to strictly equal [ 'en-US', 'en' ]

@k-yle

k-yle commented Apr 30, 2025

Copy link
Copy Markdown
Collaborator Author
  1. while this works well for the case reported in #10882, it does not seem to fully address the original issue from #10219

I was planning to send a third PR which does the special-casing for zh, but I thought I'd seperate it from this PR which is much more straightforward. So lets discuss this in #10911

  1. It feels slightly odd that this is tied to iD's UI translations. [...]
  2. Also, what's your opinion to extend this even further: [...]

Yeah, I also think this would be useful, but maybe in a separate PR? While this would be nice for me personally, it might be unexpected for others (#9499 in one such example)

@tyrasd

tyrasd commented Jun 10, 2025

Copy link
Copy Markdown
Member

Let's prioritize #11082 for now, as that one is addressing the more often occurring situation in practice (i.e. a user has multiple locales as their preference and also solves the zh-* issue) and names like pt-BR are quite rare in OSM at the moment.

@k-yle

k-yle commented Dec 29, 2025

Copy link
Copy Markdown
Collaborator Author

Since #11082 is stale, I've redone this PR to use Intl.Locale#maximize, which is an awesome new browser API.

This PR can now match zh-CN to zh-Hans without hardcoding any exceptions, so it solves both issues and superseeds #11082. See updated PR description

@1ec5 1ec5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

Comment thread modules/util/localizer.ts Resolved
Comment thread modules/util/localizer.ts Outdated
@@ -0,0 +1,45 @@
import { utilArrayUniq } from './array';

function collectLocale({ script, region, language }: Intl.Locale) {

@1ec5 1ec5 Dec 29, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some common tags require a variant code, such as the tarask in name:be-tarask=*. maximize() returns be-Cyrl-BY-tarask. There’s a variant property but only Firefox supports it.

This is an edge case that we can ticket out as tail work. iD doesn’t currently have any localizations that require a variant. The closest is our “ar-AA” locale, which should probably be just “ar”. (I think Transifex made up a country code for the Unitag variant.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened #11731 to track this

@1ec5 1ec5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to go from a code perspective. Before merging, please spot-check iD in any tricky localizations like Arabic, Chinese, or Serbian in places that might have secondary localized names in those languages.

@k-yle

k-yle commented Jan 5, 2026

Copy link
Copy Markdown
Collaborator Author

i made one change (5fe8f6e..601f5c8), so that base language is the last fallback, to fix case #3 below.

seems like this works well:

  1. 🆕 locale=zh-CN uses name:zh-Hans
  2. locale=zh-TW prefers name:zh over name:nan
  3. 🆕 locale=zh-HK prefers name:zh-Hant over name:zh.
  4. 🆕 locale=pt-BR prefers name:pt-BR over name:pt
  5. locale=ar-AA with name:ar-Latn
  6. locale=sr prefers name:sr over name:sr-Latn
  7. 🆕 locale=sr prefers name:sr-Cyrl over name:sr-Latn

🆕 means the behaviour was changed by this PR

@tordans
tordans requested a review from 1ec5 January 5, 2026 17:37
@k-yle

k-yle commented Feb 16, 2026

Copy link
Copy Markdown
Collaborator Author

if there are no objections, i think we could merge this one?

@1ec5

1ec5 commented Feb 22, 2026

Copy link
Copy Markdown
Collaborator

I implemented osm-americana/diplomat#30 based on the approach taken here. It works well. Diplomat needs to handle #11731 and even more complex language tags, so I modified the implementation to calculate a powerset instead of hard-coding specific combinations of language tag components. But I think what we have here is more than adequate for the issues iD users are noticing.

@daishu0000

Copy link
Copy Markdown

I implemented osm-americana/diplomat#30 based on the approach taken here. It works well. Diplomat needs to handle #11731 and even more complex language tags, so I modified the implementation to calculate a powerset instead of hard-coding specific combinations of language tag components. But I think what we have here is more than adequate for the issues iD users are noticing.

I agree, we should not hard-coding the language combinations. For example, there are "name:zh name:zh-Hans name:zh-Hant name:zh-Hans-CN name:zh-Hans-SG name:zh-Hant-TW name:zh-Hant-HK name:zh-Hant-MO" and endless name:zh tags. If we just hard-coding, we can't handle all situations.

@daishu0000

daishu0000 commented Feb 23, 2026 •

Copy link
Copy Markdown

Actually, I think we should allow users to customize language fallback solution, in My Advanced Preferences, users can define more than one preferred languages, can we allow users to directly write osm language tag(e.g. name:zh name:zh-Latn-pinyin) there? This way users can customized the language variants order.

@k-yle

k-yle commented Feb 24, 2026

Copy link
Copy Markdown
Collaborator Author

Actually, I think we should allow users to customize language fallback solution, [...]

perhaps that should be discussed in #11730 ?

@matkoniecz

Copy link
Copy Markdown
Collaborator

Closes #10219 (part 2)

so after this PR is merged #10219 should not be closed, right?

@k-yle

k-yle commented Feb 28, 2026

Copy link
Copy Markdown
Collaborator Author

sorry I forgot to update that part of the PR description, fixed now. #10219 will be fully fixed by this PR

Comment thread test/spec/util/localizer.ts Outdated
// between runtimes.
it.each`
input | output
${'en'} | ${['en', 'en-Latn-US', 'en-US', 'en-Latn']}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 'en-Latn-US' is preferred over 'en-US' but en over 'en-Latn'?

(not saying it does not make sense, just I do not understand yet what is going on)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and note it may affect anyone running tests after this is merged, including test attempts of something unrelated

@1ec5 1ec5 Mar 2, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback order I’m using in Diplomat and OSM Americana is en-Latn-US, en-Latn, en-US, en. (That’s what I meant by “powerset” in #10911 (comment).) The middle ones are more or less tied, but I’d err on the side of ensuring readability for someone who doesn’t necessarily know a writing system other than their own.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 'en-Latn-US' is preferred over 'en-US' but en over 'en-Latn'?

My original approach was to make the exact match come first (the user requested en so they get en).

But I implemented this before seeing Minh's more advanced version (which i think is brilliant btw), so we could change it to match diplomat's ordering if that's preferred (?)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think that @1ec5 ordering seems to make more sense, but I lack expertise or experience or anything similar in this area.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ordering from most specific to least specific is important because of languages like Chinese. Browsers generally indicate the maximized locale, but mappers rarely tag that much detail because it doesn’t make a difference to the place name.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the delay, i had to think about this a bit. I've copied the powerset approach from diplomat, with 2 changes that i find useful:

  1. if x !== new Intl.Locale(x).baseName, then include and prioritise x. This is required to support cmn, which the browser transforms into the macrolanguage zh1
  2. if the final array of locale codes does not include the original input, then include and prioritise the original. This is required to support extensions like -u-sd-, for example name:mi-u-sd-nzwko

unit tests added for both cases. I'm not certain if these are a good idea, and/or whether they're worth including in diplomat

Footnotes

  1. this also affects deprecated language codes like iw, but that's irrelevant. Ideally name:iw should not exist in OSM. ↩

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to post issues about any improvements that could potentially apply to Diplomat. I’ve been unsure what to do about cmn/zh. Despite it being a more specific code, BCP 47 and CLDR in practice favor zh-Hans-CN for backwards compatibility. As a result, some mappers have gone around replacing name:cmn=* with name:zh-Hans=*. I suspect this is motivated more by interoperability with the broader software ecosystem rather than a particular OSM data consumer. It probably deserves a forum discussion at some point.

Diplomat does have a later step to prefer the Unicode extensions, but a more elegant approach would be welcome.

@matkoniecz matkoniecz Apr 1, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I found https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Retirements.tab via https://iso639-3.sil.org/code_tables/download_tables listing tripple letter deprecated language codes

are there other double letter deprecated language codes except iw, ji, in ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow i missed your question sorry. yes, the javascript API will transform 9 deprecated language codes:

  • bh → bho (Bhojpuri)
  • in → id (Indonesian)
  • iw → he (Hebrew)
  • ji → yi (Yiddish)
  • jw → jv (Javanese)
  • mo → ro (Romanian)
  • sh → sr (Serbian)
  • tl → fil (Filipino)
  • tw → ak (Akan)
(determined using brute force)
for (let i = 0; i < 26; i++) {
  for (let j = 0; j < 26; j++) {
    const old = String.fromCharCode(97+i) + String.fromCharCode(97+j);
    const nēw = new Intl.Locale(old).language;
    if (old !== nēw) {
      const label = new Intl.DisplayNames(navigator.languages, { type: 'language' }).of(nēw);
      console.log(`${old} → ${nēw} (${label})`);    
    }
  }
}

the only one i was suprised to see was tl, which is quite common in OSM, and iD also uses tl for localisations. i've added a test case for one "deprecated" code, but i don't think we need to anything else.

Comment thread test/spec/util/util.js Resolved
Comment thread test/spec/util/util.js Resolved
@tyrasd
tyrasd merged commit b73eaa1 into develop Jun 23, 2026
4 checks passed
@tyrasd
tyrasd deleted the kh/script2 branch June 23, 2026 10:36

This branch was successfully deployed

1 active deployment
pr-preview-10911 — d41cb202 Deployed Jun 23, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

localization Adapting iD across languages, regions, and cultures

Projects

None yet

5 participants