当前位置:   article > 正文

iOS开发Xcode中切换显示语言实现国际化

xcode languages

1.在Project设置,添加中英两种语言:

 

 

2.新建Localizable.strings文件,作为多语言对应的词典,存储多种语言,点击右侧Localization,勾选中英:

 

3.添加一个字段,

 

在English中,添加:SUBMIT_BTN_TITLE = Go;

在Chinese中,添加:SUBMIT_BTN_TITLE = 开始;
 

4.一个工具类GDLocalizableController,用来切换本地语言:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
//  GDLocalizableController.h
//  guide-book
//
//  Created by why on 7/16/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
# import <foundation foundation.h= "" >
 
@interface GDLocalizableController : NSObject
 
+(NSBundle *)bundle; //获取当前资源文件
 
+( void )initUserLanguage; //初始化语言文件
 
+(NSString *)userLanguage; //获取应用当前语言
 
+( void )setUserlanguage:(NSString *)language; //设置当前语言
 
@end
 
 
 
 
 
 
//
//  GDLocalizableController.m
//  guide-book
//
//  Created by why on 7/16/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
# import GDLocalizableController.h
 
@implementation GDLocalizableController
 
static NSBundle *bundle = nil;
 
+ ( NSBundle * )bundle{
     return bundle;
}
+( void )initUserLanguage{
     
     NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
     NSString *string = [def valueForKey: @userLanguage ];
     if (string.length == 0 ){
         //获取系统当前语言版本
         NSArray* languages = [def objectForKey: @AppleLanguages ];
         NSString *current = [languages objectAtIndex: 0 ];
         string = current;
         [def setValue:current forKey: @userLanguage ];
         [def synchronize]; //持久化,不加的话不会保存
     }
     
     //获取文件路径
     NSString *path = [[NSBundle mainBundle] pathForResource:string ofType: @lproj ];
     bundle = [NSBundle bundleWithPath:path]; //生成bundle
}
 
+(NSString *)userLanguage{
     
     NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
     NSString *language = [def valueForKey: @userLanguage ];
     return language;
}
 
+( void )setUserlanguage:(NSString *)language{
     
     NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
     
     //1.第一步改变bundle的值
     NSString *path = [[NSBundle mainBundle] pathForResource:language ofType: @lproj ];
     bundle = [NSBundle bundleWithPath:path];
     
     //2.持久化
     [def setValue:language forKey: @userLanguage ];
     [def synchronize];
}
 
@end </foundation>



5. 自定义一个宏方便处理:

 

 
1
2
3
4
// ----- 多语言设置
#define CHINESE @zh -Hans
#define ENGLISH @en
#define GDLocalizedString(key) [[GDLocalizableController bundle] localizedStringForKey:(key) value:@ table:nil]


6.使用:

1
2
3
4
[GDLocalizableController setUserlanguage:CHINESE];
NSLog(GDLocalizedString( @SUBMIT_BTN_TITLE ));
[GDLocalizableController setUserlanguage:ENGLISH];
NSLog(GDLocalizedString( @SUBMIT_BTN_TITLE ));

 

转载于:https://www.cnblogs.com/tate-zwt/p/4438800.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/248374
推荐阅读
相关标签
  

闽ICP备14008679号