Localization quick question

Will the correct language be chosen automatically based on where the user is, or the navigator.language value?

Or do I need to do something else to make the correct locale be chosen for my UI text elements, etc?

Also, can you have multiple locales with the same messages? For example, “info”: {
“locales”: [“en-US”, “en-CA”],
,
“messages”: {
},

or maybe configure a local if it starts with “en” or something like that.

You can’t do this, no.

The localisation system handles fallback to the language (the en) if it can’t find a language and country combination.

In this example, it would use en if it can’t find translations for en-GB for example.

1 Like

Had a quick check, no it doesn’t. You will have to set the locale yourself via https://developer.playcanvas.com/en/api/pc.I18n.html#locale

1 Like

Ah ok good to know. I had actually added a manual check for this:

const browserLanguage = navigator.language.split("-")[0];
this.app.i18n.locale = browserLanguage === "fr" ? "fr-FR" : browserLanguage === "es" ? "es-ES" : browserLanguage === "da" ? "da-DK" : "en-US";

So my manual check is unnecessary?

Yes, the check is not needed

German has a million different localization codes:
da, da-DK, de, de-AT, de-CH, de-DE, de-LI, de-LU.

If I use da and it’s not found, will it fallback to de and vice-versa? Or do I need one for da and another for de?

Did a quick test and it tries to fallback to the closest country match.

For example:

{
    "header": {
        "version": 1
    },
    "data": [
        {
            "info": {
                "locale": "en-US"
            },
            "messages": {
                "key": "Single key translation"
            }
        },
        {
            "info": {
                "locale": "de-de"
            },
            "messages": {
                "key": "German"
            }
        }
    ]
}

Changing to de-LI will match de-de because they are the same country. However, changing to da will fallback to en-US

As it would be your app code setting the locale, you may want to do your own check for regions to ensure you have the closest match available.

Ok thanks! One final question, can I use local “en” and “de”, instead of the full “en-US” and “de-de”?

I believe you can yes.

1 Like