How to find out the language of your iPhone when using Unity?

For localizations and stuff, you know? Like say if you want your game to support different languages like English, Chinese, Japanese and stuff.

There’s Application.systemLanguage (I think it’s supported on iOS now, since I tested it on my device and it returns the correct language). It supports these languages: Afrikaans, Arabic, Basque, Belarusian, Bulgarian, Catalan, Chinese, Czech, Danish, Dutch, English, Estonian, Faroese, Finnish, French, German, Greek, Hebrew, Hugarian, Icelandic, Indonesian, Italian, Japanese, Korean, Latvian, Lithuanian, Norwegian, Polish, Portuguese, Romanian, Russian, Serbo-Croatian, Slovak, Slovenian, Spanish, Swedish, Thai, Turkish, Ukrainian, Vietnamese and Hungarian. And if your selected language is not on that list, it returns: Unknown. So if the language that you want to support is on that list, you don’t have to read the rest of this post. Application.systemLanguage is all you need.

But, but, if you are like me (Taiwanese/Chinese), you might need to support not just Chinese, but Simplified and Traditional Chinese (there is a difference), unfortunately Application.systemLanguage, doesn’t know that difference. But the iOS SDK does.

So I want to use iOS SDK’s way of identifying languages using the code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@”AppleLanguages”];
    NSString *currentLanguage = [languages objectAtIndex:0];

I then save the current language to NSUserDefaults:

[[NSUserDefaults standardUserDefaults] setObject:currentLanguage forKey:@”language”];
[[NSUserDefaults standardUserDefaults] synchronize];

So I can access it in Unity, using PlayerPrefs:

PlayerPrefs.GetString(“language”)

So here’s the thing the iOS code needs to be added in AppController.mm that’s generated when you build Unity iOS.

I added that part before Unity gets called ([self startUnity:application]) inapplication didFinishLaunchingWithOptions.

But, but, every time you build, AppController.mm gets overwritten, so, so, what now?

After Unity 3.5, all you have to do is to drag and drop your AppController.mm file into Plugins/iOS folder inside Unity Assets folder and that copy will automatically overwrite whatever that is generated.

And that is it! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s