简体   繁体   中英

Xcode: How to create a multi-langual iOS application with language settings within the application (NOT using localization)

Hi I want to create an application for the iPhone/ iPad, and on the initial page I want the user to have the option to change the language of the application (default language is English). Depending on the language the user chooses, for example Spanish, the initial page text and buttons will all be in Spanish. Then, after a button is pressed, the next view is also in Spanish. (And the back button is also in Spanish)

How could I do this? Remember, I DO NOT want to use Localization. Instead, I want the user to have to possibility to change the language of the app, WITHIN the same app, and NOT the Settings app.

Help with maybe a few examples would be very much appreciated.

Thank you very much in advance!

I've done this a few times. iOS is not designed for this and the first thing you should do is reconsider your approach or go back to the client and make absolutely sure it is necessary. There are only a few situations where this approach is the right one, it's mostly a mistake.

Your first issue is with localising string resources. NSBundle offers methods to load localised strings. Have a singleton class to manage which language you've currently got selected. When you change the language, it can load the en.lproj directory (or whichever language is selected) as a bundle. From that point, instead of using the usual macros to get localised string resources, use a method on your singleton to fetch them from the current bundle.

The same applies to images and other resources - your singleton knows which localisation bundle is in play, so ask it for the resources you need.

When you change language, tell your singleton, and your singleton can load the relevant bundle and post a notification so that your view controllers know to update themselves.

If you are lucky, your nibs layouts work independently of language without modification. This means you can set up a base view controller that others inherit from that has an outlet collection for localised interface elements. It should listen for the notification and walk the collection to update the necessary items.

If you are unlucky and different localisations need different layouts, you'll probably have to save state, get the relevant nib from your singleton, then reload everything and restore state.

You also have a problem that can't be entirely fixed - iOS will still think it's in the main system language. Where there is iOS-supplied content that is out of your control, it will be localised to the main system language. You can mitigate that a little by setting the user preference AppleLanguages to an array with a string containing the locale before the application is fully initialised (ie in main.c ). However this can only change languages when the application is launched.

A possible implementation:

Use CoreData to create a table of localized strings

Store a Name/Key, language, and value.

You'll then want to build out a manager class to help you read the values. Using a singleton here would probably be a good approach. You could then do things like this:

[[LanguageManager] instance] getLocalizedString:@"mainView_title" forLang:@"en"];

Have you tried to checkout this article ?

I see you are asking to NOT to use localization, but the only reason I figured out is because you'd like to change language at runtime.

That post then offers (and explains how to use) a custom utility class to meet your requirements leveraging part of the already existing localization functionalities of the SDK.

Hope that's going to be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM