? Add concern author Farmguo 2016.01.27 15:36* wrote 6453 words, was paid attention to by 44 people, gained 56 likes iOS in-app language switching function words 1372 read 681 Reviews 7 likes 7
When our application is only for the domestic user base, generally support only one language-the Chinese can be. When facing foreign users need to internationalization, not only the language changes, but also may include the design style, page layout, interactive effect of the change, such as Weibo, QQ, such applications have the ability to switch languages.
International processes common to iOS
1. Create a strings file. 2. In localization, select the supported language and set the title in the strings of the same file name as the suffix. 3. Use Nslocalizedstringfromtable (key, tbl, comment) to remove the value corresponding to the key.
This approach is quick and easy, but has some drawbacks. 1. It is completely internationalized according to the language set by the mobile phone system. 2. After changing the language of the mobile phone system, it is necessary to re-enter the app after it is changed. Can be seen to bring great inconvenience to users, some users do not know how to set the language, but also to jump out of the application, and then kill the application. What we need is an in-app switching language, WYSIWYG.
In-app switching language
First look at the practice
111.gif
You can see the selection of a new language, save and then switch directly. This kind of user experience is better.
Key 1. NSBundle
An NSBundle object represents a at the file system that groups code and resources so can be used in a PROGRAM.N Sbundle Objects Locate program resources, dynamically load and unload executable code, and assist in localization. You build a bundle in Xcode using one of these project Types:application, Framework, Plug-ins.
2. Macro Nslocalizedstringfromtableinbundle
It's defined
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
Returns a localized version of a string. The key for a string in the specified table. Returns the value of the corresponding key from the table in the specified bundle.
The above two points is the key to in-app switching
Explore the demo architecture first
Structure.png
Using the storyboard method, the common Tabbar+navigation form, the first page of the button to toggle the language, the second to display the current language.
Set up the Fglanguagetool class to manage the switching of the language, as many places are used to design it as a singleton. and define a macro to load the set language.
#define FGGetStringWithKeyFromTable(key, tbl) [[FGLanguageTool sharedInstance] getStringForKey:key withTable:tbl]#import <Foundation/Foundation.h>@interface FGLanguageTool : NSObject+(id)sharedInstance;/** * 返回table中指定的key的值 * * @param key key * @param table table * * @return 返回table中指定的key的值 */-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table;/** * 改变当前语言 */-(void)changeNowLanguage;/** * 设置新的语言 * * @param language 新语言 */-(void)setNewLanguage:(NSString*)language;@end
The corresponding. m file
#define CNS @ "Zh-hans" #define en @ "en" #define language_set @ "Langeuageset" #import "AppDelegate.h" #import " FGLanguageTool.h "Static Fglanguagetool *sharedmodel; @interface fglanguagetool () @property (Nonatomic,strong) NSBundle *bundle; @property (nonatomic,copy) nsstring *language; @end @implementation fglanguagetool+ (ID) sharedinstance{if (!sharedmodel) {Sharedmodel = [[Fglanguagetool alloc]init]; } return Sharedmodel;} -(instancetype) init{self = [super init]; if (self) {[self initlanguage]; } return self;} -(void) initlanguage{nsstring *tmp = [[Nsuserdefaults standarduserdefaults]objectforkey:language_set]; NSString *path; The default is Chinese if (!tmp) {tmp = CNS; } else {tmp = EN; } self.language = tmp; Path = [[NSBundle mainbundle]pathforresource:self.language oftype:@ "Lproj"]; Self.bundle = [NSBundle bundlewithpath:path];} -(NSString *) Getstringforkey: (NSString *) key withtable: (NSString *) table{if (self.bundLe) {return nslocalizedstringfromtableinbundle (key, table, Self.bundle, @ ""); } return Nslocalizedstringfromtable (key, table, @ "");} -(void) changenowlanguage{if ([Self.language isequaltostring:en]) {[Self setnewlanguage:cns]; } else {[self setnewlanguage:en]; }}-(void) Setnewlanguage: (NSString *) language{if ([language isEqualToString:self.language]) {return; } if ([Language isequaltostring:en] | | [Language Isequaltostring:cns]) {NSString *path = [[NSBundle mainbundle]pathforresource:language oftype:@ ' lproj ']; Self.bundle = [NSBundle Bundlewithpath:path]; } self.language = language; [[Nsuserdefaults standarduserdefaults]setobject:language Forkey:language_set]; [[Nsuserdefaults standarduserdefaults]synchronize]; [Self resetrootviewcontroller];} Reset-(void) resetrootviewcontroller{appdelegate *appdelegate = (appdelegate *) [[uiapplication Sharedapplication] D Elegate]; UistoryboaRd *storyboard = [Uistoryboard storyboardwithname:@ "Main" bundle:nil]; Uinavigationcontroller *rootnav = [StoryBoard instantiateviewcontrollerwithidentifier:@ "Rootnav"]; Uinavigationcontroller *personnav = [StoryBoard instantiateviewcontrollerwithidentifier:@ "Personnav"]; Uitabbarcontroller *TABVC = (uitabbarcontroller*) AppDelegate.window.rootViewController; Tabvc.viewcontrollers = @[rootnav,personnav];} @end
In Viewcontroller, use the following similar method to assign a value
self.navigationItem.title = FGGetStringWithKeyFromTable(@"RootTitle", @"Main");self.languageLabel.text = FGGetStringWithKeyFromTable(@"NowLanguage", @"Person");
"Main", "person" is the name of the strings file. "Roottitle" and "Nowlanguage" are key.
Setting.png
These are our code sections, and there are some resource files to configure, which is our multilingual file.
1. Add the supported languages and select the project.
Add.png
Click the + sign in the localizations area to add a new language, and here's an example to select Simplified Chinese.
2, Build Strings file
Method 1. Select a storyboard, where we use the project default Main.storyboard, in file Inspecter. Check the supported languages in the localization bar. The corresponding file will be generated by the system. Method 2. We create a new strings resource file directly. Check the supported languages in the Localization column of the file Inspecter.
Strings.png
The main in the figure is created with Method 1, and the person is created in Method 2. These files of the same name are only distinguished by the suffix, corresponding to the corresponding resources. Add the corresponding key and value in these files, such as
Content in Person.strings (中文版) files
"Nowlanguage" = "中文版";
"Persontitle" = "person";
Content in Person.strings (Chinese (Simplified)) file
"Nowlanguage" = "Chinese";
"Persontitle" = "personal";
That is, different value is set in different suffix name files of the same file name.
Several key points
1, when we set up support multi-language after the creation of multi-language files, the project file structure of a number of folders, base is the project default, and the latter two is we set up the multi-language automatically generated, corresponding to the corresponding English and Simplified Chinese. Start with en and Zh-hans respectively, so Fglanguagetool defines both Chinese and English as en and zh-hans to load the corresponding bundles.
Directory.png
The corresponding strings file is stored inside.
Stings.png
2.NSLocalizedStringFromTableInBundle, specify the value of table key from the specified bundle.
3. After changing the language, reset the Rootviewcontroller so that the language can be reloaded.
Final effect
Change.gif
? Add concern author Farmguo 2016.01.27 15:36* wrote 6453 words, was paid attention to by 44 people, gained 56 likes iOS in-app language switching function words 1372 read 681 Reviews 7 likes 7
When our application is only for the domestic user base, generally support only one language-the Chinese can be. When facing foreign users need to internationalization, not only the language changes, but also may include the design style, page layout, interactive effect of the change, such as Weibo, QQ, such applications have the ability to switch languages.
International processes common to iOS
1. Create a strings file. 2. In localization, select the supported language and set the title in the strings of the same file name as the suffix. 3. Use Nslocalizedstringfromtable (key, tbl, comment) to remove the value corresponding to the key.
This approach is quick and easy, but has some drawbacks. 1. It is completely internationalized according to the language set by the mobile phone system. 2. After changing the language of the mobile phone system, it is necessary to re-enter the app after it is changed. Can be seen to bring great inconvenience to users, some users do not know how to set the language, but also to jump out of the application, and then kill the application. What we need is an in-app switching language, WYSIWYG.
In-app switching language
First look at the practice
111.gif
You can see the selection of a new language, save and then switch directly. This kind of user experience is better.
Key 1. NSBundle
An NSBundle object represents a at the file system that groups code and resources so can be used in a PROGRAM.N Sbundle Objects Locate program resources, dynamically load and unload executable code, and assist in localization. You build a bundle in Xcode using one of these project Types:application, Framework, Plug-ins.
2. Macro Nslocalizedstringfromtableinbundle
It's defined
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
Returns a localized version of a string. The key for a string in the specified table. Returns the value of the corresponding key from the table in the specified bundle.
The above two points is the key to in-app switching
Explore the demo architecture first
Structure.png
Using the storyboard method, the common Tabbar+navigation form, the first page of the button to toggle the language, the second to display the current language.
Set up the Fglanguagetool class to manage the switching of the language, as many places are used to design it as a singleton. and define a macro to load the set language.
#define FGGetStringWithKeyFromTable(key, tbl) [[FGLanguageTool sharedInstance] getStringForKey:key withTable:tbl]#import <Foundation/Foundation.h>@interface FGLanguageTool : NSObject+(id)sharedInstance;/** * 返回table中指定的key的值 * * @param key key * @param table table * * @return 返回table中指定的key的值 */-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table;/** * 改变当前语言 */-(void)changeNowLanguage;/** * 设置新的语言 * * @param language 新语言 */-(void)setNewLanguage:(NSString*)language;@end
The corresponding. m file
#define CNS @ "Zh-hans" #define en @ "en" #define language_set @ "Langeuageset" #import "AppDelegate.h" #import " FGLanguageTool.h "Static Fglanguagetool *sharedmodel; @interface fglanguagetool () @property (Nonatomic,strong) NSBundle *bundle; @property (nonatomic,copy) nsstring *language; @end @implementation fglanguagetool+ (ID) sharedinstance{if (!sharedmodel) {Sharedmodel = [[Fglanguagetool alloc]init]; } return Sharedmodel;} -(instancetype) init{self = [super init]; if (self) {[self initlanguage]; } return self;} -(void) initlanguage{nsstring *tmp = [[Nsuserdefaults standarduserdefaults]objectforkey:language_set]; NSString *path; The default is Chinese if (!tmp) {tmp = CNS; } else {tmp = EN; } self.language = tmp; Path = [[NSBundle mainbundle]pathforresource:self.language oftype:@ "Lproj"]; Self.bundle = [NSBundle bundlewithpath:path];} -(NSString *) Getstringforkey: (NSString *) key withtable: (NSString *) table{if (self.bundLe) {return nslocalizedstringfromtableinbundle (key, table, Self.bundle, @ ""); } return Nslocalizedstringfromtable (key, table, @ "");} -(void) changenowlanguage{if ([Self.language isequaltostring:en]) {[Self setnewlanguage:cns]; } else {[self setnewlanguage:en]; }}-(void) Setnewlanguage: (NSString *) language{if ([language isEqualToString:self.language]) {return; } if ([Language isequaltostring:en] | | [Language Isequaltostring:cns]) {NSString *path = [[NSBundle mainbundle]pathforresource:language oftype:@ ' lproj ']; Self.bundle = [NSBundle Bundlewithpath:path]; } self.language = language; [[Nsuserdefaults standarduserdefaults]setobject:language Forkey:language_set]; [[Nsuserdefaults standarduserdefaults]synchronize]; [Self resetrootviewcontroller];} Reset-(void) resetrootviewcontroller{appdelegate *appdelegate = (appdelegate *) [[uiapplication Sharedapplication] D Elegate]; UistoryboaRd *storyboard = [Uistoryboard storyboardwithname:@ "Main" bundle:nil]; Uinavigationcontroller *rootnav = [StoryBoard instantiateviewcontrollerwithidentifier:@ "Rootnav"]; Uinavigationcontroller *personnav = [StoryBoard instantiateviewcontrollerwithidentifier:@ "Personnav"]; Uitabbarcontroller *TABVC = (uitabbarcontroller*) AppDelegate.window.rootViewController; Tabvc.viewcontrollers = @[rootnav,personnav];} @end
In Viewcontroller, use the following similar method to assign a value
self.navigationItem.title = FGGetStringWithKeyFromTable(@"RootTitle", @"Main");self.languageLabel.text = FGGetStringWithKeyFromTable(@"NowLanguage", @"Person");
"Main", "person" is the name of the strings file. "Roottitle" and "Nowlanguage" are key.
Setting.png
These are our code sections, and there are some resource files to configure, which is our multilingual file.
1. Add the supported languages and select the project.
Add.png
Click the + sign in the localizations area to add a new language, and here's an example to select Simplified Chinese.
2, Build Strings file
Method 1. Select a storyboard, where we use the project default Main.storyboard, in file Inspecter. Check the supported languages in the localization bar. The corresponding file will be generated by the system. Method 2. We create a new strings resource file directly. Check the supported languages in the Localization column of the file Inspecter.
Strings.png
The main in the figure is created with Method 1, and the person is created in Method 2. These files of the same name are only distinguished by the suffix, corresponding to the corresponding resources. Add the corresponding key and value in these files, such as
Content in Person.strings (中文版) files
"Nowlanguage" = "中文版";
"Persontitle" = "person";
Content in Person.strings (Chinese (Simplified)) file
"Nowlanguage" = "Chinese";
"Persontitle" = "personal";
That is, different value is set in different suffix name files of the same file name.
Several key points
1, when we set up support multi-language after the creation of multi-language files, the project file structure of a number of folders, base is the project default, and the latter two is we set up the multi-language automatically generated, corresponding to the corresponding English and Simplified Chinese. Start with en and Zh-hans respectively, so Fglanguagetool defines both Chinese and English as en and zh-hans to load the corresponding bundles.
Directory.png
The corresponding strings file is stored inside.
Stings.png
2.NSLocalizedStringFromTableInBundle, specify the value of table key from the specified bundle.
3. After changing the language, reset the Rootviewcontroller so that the language can be reloaded.
Final effect
Change.gif
iOS in-app language switching feature