leonelquinteros / gotext

Go (Golang) GNU gettext utilities package
Other
432 stars 58 forks source link

Locale should expand the string to include country code if necessary #72

Closed m-horky closed 1 year ago

m-horky commented 1 year ago

gettext documentation 17.2.2 states that:

In the LANGUAGE environment variable, but not in the LANG environment variable, ‘ll_CC’ combinations can be abbreviated as ‘ll’ to denote the language’s main dialect. For example, ‘de’ is equivalent to ‘de_DE’ (German as spoken in Germany), and ‘pt’ to ‘pt_PT’ (Portuguese as spoken in Portugal) in this context.


Take a string

fmt.Println(gotext.Get("en_US"))

and translate it in the .po file:

# pt_PT.po
msgid "en_US"
msgstr "pt_PT"
# pt_BR.po
msgid "en_US"
msgstr "pt_BR"

According to the excerpt above, pt should be expanded to pt_PT, when plain pt is not available. However, it is not:

$ LANGUAGE=en_US foo
en_US
$ LANGUAGE=pt_PT foo
pt_PT
$ LANGUAGE=pt_BR foo
pt_BR
$ LANGUAGE=pt foo
en_US
$ # ^ here

Is this a bug, an improvement, a proposal or something else? Describe it.

This is a bug/deviation from the gettext behavior. When language is specified, but translation file is named language_country, msgid is returned instead of the main dialect.

What's the expected behaviour, the current behaviour and the steps to reproduce it?

The main dialect version should be returned instead of the fallback string.

Comments

The library does not deal with environment variables. However, I believe this should be the default behavior; the developers using this library can handle differences between LANG and LANGUAGE themselves; supporting expansion for every language string should not break anyone, and can be handled easily in the application itself.

m-horky commented 1 year ago

Ok, so I checked and gettext does the same as gotext. Fallback pt_PT -> pt does work as expected; the documentation made me think that pt -> pt_PT would work as well.