Language/Locale

How do I check what language a computer is set to?

Dim theLocale As Xojo.Core.Locale = Xojo.Core.Locale.Current currentLocale = theLocale.Identifier.Left(2)

Thanks Beatrix :slight_smile:

If you are using a newer version of Xojo, you can just use ‘Locale’ instead of ‘Xojo.Core.Locale’. Just an FYI. Both ways will work.

[quote=476428:@Beatrix Willius]Dim theLocale As Xojo.Core.Locale = Xojo.Core.Locale.Current currentLocale = theLocale.Identifier.Left(2)[/quote]
Although that is correct for most languages, it isn’t always correct.

Examples:
pt_BR <> pt_PT
zh_CN <> zh_TW <> zh_HK

So is it best to check .Right(2) ?

It really depends on what you want to achieve.
If you are making an international app that may be downloaded in China and/or used in Chinese, or other languages that have variants such as pt_BR and pt_PT you will need to use the entire locale.identifier.

If your app is only distributed in English, using theLocale.Identifier.Left(2) is pretty safe.

Checking theLocale.Identifier.Right(2) is even worse, the computer might be set to something like en_FR, meaning the region is France, but apps should run in English.

EDIT: It is never safe to use theLocale.Identifier.Right(2) to get the country either.
I.E: This is a valid identifier: en_FR@calendar=buddhist

I’m loading a couple of images depending on the locale and do translations for mailboxes. Everything else is handled by Xojo.

So how do I get the language that the app is supposed to show? Nthfield on the @ and then right(2)?

I am sorry if I confused everyone saying that theLocale.Identifier.Left(2) isn’t always correct.

The following code should correctly identify 99% of the available language codes

[code]Dim identifier As Text = locale.current.Identifier
If identifier.IndexOf("@") > -1 Then
identifier = identifier.Left(identifier.IndexOf("@"))
End If

If identifier.BeginsWith(“zh_CN”) or identifier.BeginsWith(“zh-Hans”) then
//Chinese (Simplified)

Elseif identifier.BeginsWith(“zh_TW”) or identifier.BeginsWith(“zh-Hant”) then
//Chinese (Traditional)

Elseif identifier.BeginsWith(“zh”) then
//Assume it is Chinese (Simplified)

Elseif identifier.BeginsWith(“pt_BR”) then
//Portuguese (Brazil)

Elseif identifier.IndexOf("") > -1 then
identifier = identifier.Left(identifier.IndexOf("
"))

//Identifier will be the language code
//Note that it isn’t necessarly a 2 letter code
//Example: ko-Kore, ckb-Arab, fur-Latn, ms-Arab, fil, ain

Elseif identifier.length = 2 then
//Identifier is a 2 letter code

Else
//Raise an exception to treat this special case

End If
[/code]

This is based on 79 different language codes extracted from 1467 different locale.current.identifier from 600.000+ devices using one of my apps.