IOS development-function-internationalization | NSUserDefaults, iosnsuserdefaults

Source: Internet
Author: User

IOS development-function-internationalization | NSUserDefaults, iosnsuserdefaults

/* ===================================================== = Software Internationalization = ================= */

1. Introduction:

Software Internationalization is mainly to add multiple languages to the software to meet the needs of different countries.

There are two main application methods: 1. Automatic Switch Based on the system language environment; 2. manual button addition and so on.

There are two main methods to achieve this: 1. Add Localizations on the Xcode visual interface. 2. Use NSLocalizedString and genstrings for implementation. The second method is introduced here.

2. Implementation:

2.1 NSUserDefaults: Suitable for storing lightweight local data, such as saving a login user name, password, and language environment. NSUserDefaults is preferred.

NSDictionary * defaultsee = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];

NSLog (@ "Defaults: % @", defaultsee); // you can view the settings of all NSUserDefaults in the dictionary.

2.2 NSLocalizedString: Basic internationalization function. Check the definition.

# Define NSLocalizedString (key, comment) \ [[NSBundle mainBundle] localizedStringForKey :( key) value: @ "table: nil] // key-value, comment: comment

Extended customization of the above functions, language packs are stored in en. lproj (English), zh-Hans.lproj (Chinese), etc., obtained in key-value mode.

# Define attributes (key, comment) \ [[NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @ "% @", [NSUserDefaults standardUserDefaults] objectForKey: @ "appLanguage"] ofType: @ "lproj"] localizedStringForKey :( key) value: @ "table: nil] // retrieves the corresponding object using the key

2.3 use genstrings to generate the. lproj language file Localizable. strings of the corresponding language

Genstrings-o ~ /../Zh-Hans.lproj *. m //-o generated target folder, scan all. m files under the current directory

Genstrings-o ~ /.../En. lproj *. m

2.4 Add "login" = "login"; and so on to the Localizable. strings file.

[[NSUserDefaults standardUserDefaults] setObject: @ "zh-Hans" forKey: appLanguage]; // Add the language file NSArray * array = [[NSUserDefaults standardUserDefaults] objectForKey: @ "AppleLanguages"]; // obtain the current system language NSString * currentLanguage = [array objectAtIndex: 0];

Note: 1. the generated Localizable. strings file name is not identified, so you can add folders or add annotations to the generated file to differentiate

2. Be sure to scan all. m files that require Internationalization

3. NSUserDefaults only stores attribute values and paths, and uses dictionary-like methods to obtain data.

 

/* ===================================================== ===== NSUserDefaults =========== */

1. Introduction: The NSUserDefaults object is used to save preference settings and configuration data related to the application. It is mainly used to restore basic settings and record default information such as passwords and system languages and keyboards. The data is stored in the <UUID for your App> \ Library \ Preferences \ <your App "s bundle ID>. plist file.

2. features:

2.1 NSUserDefaults can only store five data formats: NSString, NSNumber, NSDate, NSArray, and NSDictionary.

2.2 view all current configuration data:

 NSDictionary* defaultsee = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];    NSLog(@"Defaults: %@", defaultsee);

2.3 Singleton mode: + (NSUserDefaults *) standardUserDefaults // obtain the instance of the current class

2.4 key-value mode:

-(Void) setObject :( id) value forKey :( NSString *) defaultName; // set data-(id) objectForKey :( NSString *) defaultName; // obtain data

2.5 because NSUserDefaults is a persistent data storage method, you can only delete the data set in it in two ways after the device is restarted:

1. Delete sandbox (reload)

2.-(void) removeObjectForKey :( NSString *) defaultName; // Function Method

2.6 others:-(NSDictionary *) dictionaryRepresentation; // view the configuration using the dictionary

-(BOOL) synchronize; // Data Synchronization

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.