Prompt
How do I manage locale variants like en-GB and pt-BR in one tool?
Latest observation
Nov 2, 2026
OpenAI APIWeb search: off
A good way to manage locale variants like en-GB and pt-BR in one tool is to treat them as:
-
A base language
en,pt,fr, etc.
-
A locale variant / region-specific override
en-GBvsen-USpt-BRvspt-PT
Recommended approach
1) Use locale codes as keys
Store translations in a structure like:
{
"en": {
"hello": "Hello"
},
"en-GB": {
"hello": "Hello"
},
"pt": {
"hello": "Olá"
},
"pt-BR": {
"hello": "Oi"
}
}
If a variant is missing a string, fall back to the base language:
- Try
pt-BR - If missing, use
pt - If missing, use default locale like
en
2) Build a fallback chain
For example:
en-GB→en→ defaultpt-BR→pt→ default
This avoids duplicating almost identical translations.
3) Separate shared from variant-specific content
A common pattern:
- Shared base locale: general translation strings
- Variant overrides: only store differences
Example:
{
"pt": {
"currency": "moeda",
"color": "cor"
},
"pt-BR": {
"currency": "real"
}
}
Here pt-BR inherits color from pt, but overrides currency.
4) Normalize and validate locale tags
Use standard tags like BCP 47:
en-GBpt-BRzh-Hans
This helps keep tooling consistent and makes fallback logic predictable.
5) Let the tool show inheritance
If you’re building or choosing a translation tool, it helps if it can:
- display base strings and overridden strings
- highlight missing keys
- compare variants side by side
- export/import locale files cleanly
If you mean in code
A simple resolver could be:
function getTranslation(locale, key, translations, fallback = "en") {
const parts = locale.split("-");
const candidates = [locale, parts[0], fallback];
for (const candidate of candidates) {
if (translations[candidate]?.[key]) return translations[candidate][key];
}
return key;
}
Best practice summary
- Use full locale tags for variant support
- Fallback from variant → base language → default
- Store only differences in region variants
- Keep locale data normalized and consistent
If you want, I can also show:
- a folder structure for locale files,
- a database schema for storing variants,
- or an example using a specific framework/tool like i18next, Laravel, Rails, or Django.