localization/language

Is there a way to change the language of an App even the language of iOS does not change?
…without changing every single text manually.

Sorry your question isn’t completely clear.

I suppose you are asking how to change the language of an app WITHOUT changing the language in the iOS settings.
Yes that is possible and can be achieved in two different ways.

[h]1. Dynamically changing the language[/h]

Each time you use a Text constant, you will need to refer to it as kCONSTANT_NAME(language_code)
Using Jason King’s iOSKit, copy/paste this code into a module

[code]Public Property kLang as Text
Get

Using Foundation

Dim defaults As NSUserDefaults = NSUserDefaults.StandardUserDefaults
If Defaults.TextForKey(“userLanguage”).Empty Then
Return Language.localLangCode

Else
Return Defaults.TextForKey(“userLanguage”)
End If
End Get

Set

Using Foundation

Dim defaults As NSUserDefaults = NSUserDefaults.StandardUserDefaults
Defaults.SetTextForKey(value, “userLanguage”)
End Set

End Property
[/code]

Language is a module I use to store all localized text constants
localLangCode is a Text constant that contains the language identifier for each language used in my app: “en” for English, “fr” for French

Each time you use a text constant you will need to call it like CONSTANT_NAME(kLang)
If your app already contains many text constants, it can be quite difficult to find each of them and add “(kLang)” to get the language specific value.
However for a new app, this solution is interesting because it doesn’t require the user to quit and relaunch your app to change language.

[h]2. Changing the language using declares.[/h]

In one of my apps that is already in production since 9 months, I use over 400 text constants and was to lazy to search for each Text constant to add “(kLang)” at the end.

So I found another solution using declares.

The following code will set the language code that needs to be used the next time the app is opened:

[code]Dim Str As New Foundation.NSString(langcode) //langcode is the two character language identifier such as “en” or “fr”
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]

Just before calling the above code, I display a MessageBox asking the user to confirm the change of language and warning him that the app will close and he must restart the app to change language.

Finally I call the following code to quit the app, hoping the user will open it back again

Declare Sub os_exit Lib "/usr/lib/libSystem.dylib" Alias "exit"(ret As Int32) os_exit(0)


Solution number 1 is dynamic, better for the user and looks cleaner. But it is up to the developer to make sure that all Text constants are referred to using the language code parameter.

Solution number 2 requires a restart, but is easy and quick to setup. However you aren’t sure that the user will actually restart the app after changing language.

This may help:

https://blog.xojo.com/2014/03/05/picking-a-language-at-runtime/