iOS development-Features-internationalization | Nsuserdefaults

Source: Internet
Author: User

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

1. Introduction:

Software internationalization is mainly to add multiple languages in the software, in order to achieve the needs of people in different countries.

There are two main ways of application: 1. Automatically switch according to the language environment of the system; 2. Make an active switch by adding an artificial button.

There are two main methods to implement at present: 1.Xcode visualization interface to add localizations operation. 2. Use nslocalizedstring with genstrings to achieve. The second method is mainly described here.

2. Achieve:

2.1 Nsuserdefaults: Suitable for storing lightweight local data, such as to save a login username, password, language environment, etc., using nsuserdefaults is preferred.

nsdictionary* Defaultsee = [[Nsuserdefaults standarduserdefaults] dictionaryrepresentation];

NSLog (@ "Defaults:%@", defaultsee);//Use a dictionary to see all the Nsuserdefaults settings

2.2 nslocalizedstring: A basic internationalization function, look at the definition

#define Nslocalizedstring (key, comment) \    [[NSBundle Mainbundle] Localizedstringforkey: (key) Value:@ "" Table:nil]//key-value,comment: Notes

The above functions are expanded and customized, the language packs are stored in en.lproj (English), Zh-hans.lproj (Han), etc., and are obtained in key-value manner.

#define Customlocalizedstring (key, comment) \    [[NSBundle bundlewithpath:[[nsbundle Mainbundle] pathforresource:[ NSString stringWithFormat:@ "%@", [[Nsuserdefaults Standarduserdefaults] Objectforkey:@ "applanguage"] OfType:@ "lproj"] Localizedstringforkey: (key) Value:@ "" table:nil "// use key to remove the corresponding object

2.3. lproj language files using genstrings to generate the appropriate language localizable.strings

Genstrings-o ~/.../zh-hans.lproj *.m//-o generated target folder, scan all. m files in the current directory

Genstrings-o ~/.../en.lproj *.M

2.4 Then add "login" = "Login" to the corresponding localizable.strings file;

[[Nsuserdefaults Standarduserdefaults] SetObject:@ "zh-hans" forkey:applanguage]; // Add a language file    Nsarray *array = [[Nsuserdefaults standarduserdefaults] Objectforkey:@ "applelanguages"]; // get the current language    of the system NSString *currentlanguage = [array Objectatindex:0];

Note: 1. The generated localizable.strings file name is not identified, so you can add a folder or comment to the generated file to differentiate

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

3.NSUserDefaults just plays the role of storing attribute values and paths, obtaining data using a dictionary-like approach

/*===========================================nsuserdefaults detailed ==========================================*/

1. Introduction: The Nsuserdefaults object is used to save application-related preferences, configuration data and so on, mainly used to restore the basic settings, record passwords and system of some language and keyboard and other default information. Data is stored in <uuid for your app>\library\preferences\<your App "s bundle id>.plist files

2. Function:

2.1 Nsuserdefaults can only hold 5 data formats: NSString, NSNumber, NSDate, Nsarray, Nsdictionary.

2.2 View all current configuration data:

nsdictionary* defaultsee = [[nsuserdefaults standarduserdefaults] dictionaryrepresentation];    NSLog (@ "Defaults:%@", Defaultsee);

2.3 Singleton mode: + (Nsuserdefaults *) standarduserdefaults//get an instance of the current class

2.4 Key-value Mode:

-(void) SetObject: (ID) value forkey: (NSString *) DefaultName; // setting up data    -(ID) Objectforkey: (NSString *) DefaultName; // Fetch Data

2.5 Because Nsuserdefaults is a persistent data storage method, restarting the device does not change the data that is set on it, and can only be deleted in two ways:

1. Delete Sandbox (reload)

2.-(void) Removeobjectforkey: (NSString *) defaultname;//function method

2.6 Other:-(nsdictionary *) dictionaryrepresentation;//using a dictionary for configuration viewing

-(BOOL) synchronize;//data synchronization

iOS Development-Features-internationalization | Nsuserdefaults

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.