Locale / Identifier during test on device show always the same

Hello Community,

I am testing an iOS app on a iPhone SE (iOS 12.1.2). I am using Xojo 2018r4 on macOS 10.14.3.

The goal is to show text of labels in the proper language.

I am using the following code to determine the language and the region.

[code]Dim buttons() As Text
buttons.Append(“Yes”)
buttons.Append(“No”)

dim wobinich as Xojo.Core.Locale
wobinich = Xojo.Core.Locale.Current
Lang = wobinich.Identifier

MessageBox1.Buttons = buttons
MessageBox1.Message = Lang

MessageBox1.Show[/code]

In my test

  • I switch in the iOS settings to different languages (German, Englisch, French). The region is always Germany
  • confirm the new language
  • The system is switching to the new language
  • Press Home Button
  • Open my App
  • Press the button with MessageBox1.Message

The result in “Lang” is always en_DE.

I have placed this code in the Event Handlers “Activate” and “Open”. It makes no difference to the result.
I have restarted the iPhone SE several times. No change.

Do I have an error in the code or placed it in the wrong location?

Best regards

@Christian Jeannot — Are you sure your app was not still running from a previous test? If you launched it before setting the new language, you should force-quit it before.

Hi Christian,

Adding a language in your iPhone settings might not be enough. You need to make sure the selected language is at the top in the list of preferred languages.

Stphane is correct, you need to close the app then re-open it to make sure the language is correctly switched.

[h]Minimal setup[/h]
To show text of labels in the correct language this is what I highly recommend:

  1. Xojo Navigator (on the left) > Build Settings > Shared > Inspector (on the right) > Language > Set it to English

  2. All Text displayed to the user should be stored in Text constants, activate the Dynamic switch. Then write each constant’s translation for each language, and the Default value in English.

  1. Selection of language will then be automatic based on the users iPhone / iPad. The default language will be English for all locales that aren’t translated (Chinese people will see the app in English for example).

[h]Advanced setup[/h]
If you need your app users to be able to change the language of the app it is possible in two ways.

  1. Store the language code the user chooses in a persistant storage (database, nsuserdefaults, text file…)

  2. Refer to each Text constant as constantName(langcode) forcing the selected langcode to be displayed.

Advantage
This solution enables the user to switch language without closing the app. You will need to build the appropriate UI for the user to select a language.
Drawback
Absolutely each text constant must be used with the language code as parameter. This is prone to error.

OR

  1. Store the language code the user chooses with the following code (needs iOSKit by Jason King)

[code]Dim Str As New Foundation.NSString(langcode)
Dim obj() As Foundation.NSObject
obj.Append Str

Dim arr As New Foundation.NSArray(obj)

Foundation.NSUserDefaults.StandardUserDefaults.SetObjectForKey(arr, “AppleLanguages”)
Call Foundation.NSUserDefaults.StandardUserDefaults.Synchronize[/code]

  1. Refer to each Text constant as constantName. No need to refer to the selected language code.

Advantage
This solution is easier for the developer because you refer to each constant as constantName. You will also need to build the appropriate UI for the user to change language.
Drawback
You need to inform the user to close the app and re-open it to apply change of language.

@Stephane
I thought that it is not allowed to quit an App, only to use the Home Button.
I had also removed the app from the device and installed it new.
I will test the forced quit.

@Jeremie
The wanted language was on top and also I deleted the other language so that only the one remained I wanted to test.
I will test the Minimal Setup. What I have seen so far is that I have not the scope global. Only public, protected and private.

@Christian Jeannot — I do not know about quitting an app being allowed or not… just that it is useful sometimes that the end-user can do it

The global scope is because I place language constants in a module.
If you place them in a class or an iOSView, the possible scopes will be public, protected and private.

I have continuing testing with closing my app properly. I used the procedure described by Apple to close my app.

There is only one language (German) configured in the settings of the iPhone SE.
The region is set to Germany.

Expected result: de_DE
Result: en_DE

From my understanding the code above should work as it is described in the Xojo documentation and used in other posts in the forum here.

Has someone running a properly detection of language and region with Xojo 2018r4 and iOS 12.1.2?

Update.

I made some more tests.

The region is properly recognized. Only the language is not recognized properly. It is always “en”.

Update.

It seems to be a bug. iOS 10.3.1 show the correct value for the language. iOS 12.1.2 and 12.1.4 does not (thanks to Paul for his help).
I have created Feedback case 54977.

As a workaround I use only the region to define the language of the labels and the buttons.

E.G.

...
dim tmptext(-1) As Text
tmptext = Lang.Split("_")

MessageBox1.Buttons = buttons

'tmptext(0) contain the value for the language
'tmptext(1) contain the value for the region
If tmptext(1) = "DE" Then 
MessageBox1.Message = "Sprache DE gefunden " + tmptext(1) 
Else
MessageBox1.Message = "Sprache DE nicht gefunden "
End If

MessageBox1.Show
...

Using the region to define language is not a good idea.
From my user base 20-30% of users have a different language than preferred language for country.

And what about a region like Austria AT or Belgium BE?
Will those be set respectfully to German and French / Dutch?

@Jeremie Leroy
I understand your point. I will think about a language switch in the app.