slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
419 stars 36 forks source link

Fallback to the language code? #219

Open Rapougnac opened 1 month ago

Rapougnac commented 1 month ago

Is there a way to fallback to the language code?

For example, say I have en-GB, de-DE and de-CH. My base_locale is set as en-GB. What I'd like to have, is for example. en-GB:

hello: Hello
cat: cat
fallback: I'm still standing

de-DE:

hello: Hallo
cat: Katze

de-CH:

hello: Grüezi

In this example, the hello would return the value set, but cat should return Katze for both de-DE and de-CH locales. And if the fallback is not set, this would fall again to en-GB.

I hope that was clear :)

Tienisto commented 1 month ago

TLDR: The developer must define fallbacks to every language code similarly to base_locale. For example,

base_locale: en-GB
base_locales:
  de: 'de-DE'
  fr: 'fr-FR'
  en: 'en-GB'

Logically, if there is only de-AT, de-LI, and de-CH while there is a string in de-AT is missing, what should be chosen? de-LI or de-CH? A more relevant issue arises with Chinese languages: zh-CN, zh-HK, zh-TW, here we don't have a clear order if one of them is missing.

Technially, it is important to find the precedence order because we only have 2 viable options:

(1) Fallback during runtime Similarly to the current fallback mechanism, we need to introduce another inheritance step: DeCh extends DeDe extends enGb. What should be the order? It must be defined.

(2) Fallback during code generation We could render the fallback strings during code generation but since slang currently generates each locale one by one, we need an explicit order: en-GB, then de-DE, then de-CH. If de-CH is generated before de-DE, there would be no fallback since there is no other de generated yet.