开发工作iOS 开发 iOS

iOS分割、模块化国际化文件Localizable.string

2016-08-07  本文已影响206人  西蒙SIMON

关于国际化已经有很多博文,就是使用Localizable.strings文件和
NSLocalizedString函数,根据不同的系统语言环境,会使用不同的语言文本。

但是这样要修改系统设置才能修改语言,如果需要在App内修改语言,就要自己去指定语言文件了。

下面定义一个宏来指定使用的语言文件。

#define LSTR(STRING, LOC)                    CustomLocalizedString((STRING), @"", LOC)
#define CustomLocalizedString(key, comment, LOC) [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:LOC ofType:@"lproj"]] localizedStringForKey:key value:@"" table:nil]

说明一下,这里的LOC就是语言文件的名字,是设置了国际化之后系统生成的

Paste_Image.png

en:英语
zh-Hans:简体中文
zh-Hant:繁体中文

一个Demo:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _simpleLabel.text = LSTR(@"test_text", @"zh-Hans");
    _traditionalLabel.text = LSTR(@"test_text", @"zh-Hant");
    _enLabel.text = LSTR(@"test_text", @"en");
    
    _simpleLabelB.text = LSTR(@"test_textB", @"zh-Hans");
    _traditionalLabelB.text = LSTR(@"test_textB", @"zh-Hant");
    _enLabelB.text = LSTR(@"test_textB", @"en");
}
Paste_Image.png

由于系统默认一定要使用Localizable.strings文件,改了名字也不行(除非指定table,但是这样用起来就要加上table名),此时文件内容应该是这样的(简体中文为例):

Paste_Image.png
"test_text" = "测试文本";
"test_textB" = "测试文本B";

那如果当项目代码越来越多,所有模块的国际化文本都会掺杂在一起,十分难管理,这时候就需要对Localizable.strings进行分割。我找不到系统提供的配置方法,硬要用这个死名字。
所以这里我把文本分割到新建的strings文件中,在使用python脚本,在项目build的时候清空Localizable.strings文件,再依次读入模块的strings文件,写到Localizable.strings中,解决了这个问题。

这里分了a.strings和b.strings出来:

Paste_Image.png

依然是简体中文为例
a.strings:

/* 
  a.strings
  HVWSubLocalizableStrings

  Created by SimonHuang on 16/8/7.
  Copyright © 2016年 hellovoidworld. All rights reserved.
*/

"test_text" = "测试文本";

b.strings:

/* 
  b.strings
  HVWSubLocalizableStrings

  Created by SimonHuang on 16/8/7.
  Copyright © 2016年 hellovoidworld. All rights reserved.
*/

"test_textB" = "测试文本B";

python脚本:

在targets中创建一个Run Script,优先级排到上面

Paste_Image.png

代码在这里:

#!/usr/bin/python
#-*-coding:utf-8-*-

import os
import fnmatch

for parent, folders, files in os.walk("./HVWSubLocalizableStrings/Localizable"):

    if fnmatch.fnmatch(parent, "*.lproj"):
        #clear Localizable.strings first
        try:
            localizableStrings = open(parent + "/Localizable.strings", "w")
            localizableStrings.truncate()

        except:
            pass
        finally:
            localizableStrings.close()

        try:
            localizableStrings = open(parent + "/Localizable.strings", "a")

            #load other .strings files
            for file in files:
                if fnmatch.fnmatch(file, "*.strings") and file != "Localizable.strings" and file != "InfoPlist.strings":
                    try:
                        moduleStrings = open(parent + "/" + file, "r")
                        localizableStrings.write("\n")
                        localizableStrings.write(moduleStrings.read())

                    except:
                        pass
                    finally:
                        moduleStrings.close()

        except:
            pass
        finally:
            localizableStrings.flush()
            localizableStrings.close()


print "Generate Localizable.strings done"

build完之后,Localizable.strings文本的内容就生成了。


/* 
  a.strings
  HVWSubLocalizableStrings

  Created by SimonHuang on 16/8/7.
  Copyright © 2016年 hellovoidworld. All rights reserved.
*/

"test_text" = "测试文本";
/* 
  b.strings
  HVWSubLocalizableStrings

  Created by SimonHuang on 16/8/7.
  Copyright © 2016年 hellovoidworld. All rights reserved.
*/

"test_textB" = "测试文本B";
上一篇下一篇

猜你喜欢

热点阅读