1、在Supporting
Files文件夹右键,NewFile… -> iOS -> Resources -> String
Files,命名为Localizable.strings(不要换成别的名字,我测试时,改成其它名字,不能实现本地化);
2.在Project->Info->Localizations里面加上你所需要的语言。(中文有两种简体是zh-Hans,正体是zh-Hant)
3.然后添加一个StringFile,命名为InfoPlist.strings。并且在文件属性里将所需要的多国语言勾上。
4.添加一条新的多语言记录,CFBundleDisplayName,并对其做多语言处理。
5.在targets->info里面添加一条新的记录,Application has localized display name, 并将值设为YES。
例如:
这样具体的里面设置为:
英语的就是
- "CFBundleDisplayName" = "test";
- "CFBundleDisplayName" = "测试";
西班牙:
- "CFBundleDisplayName" = "Me Gusta";
日语:
- "CFBundleDisplayName" = "テスト";
效果:
英语:
日语:
西班牙:
中文:
IOS APP 国际化(实现不跟随系统语言,不用重启应用,代码切换stroyboard ,xib ,图片,其他资源)
此问题已解决。请看:
可以先看看这个,比较详细。
http://blog.csdn.net/xwren362922604/article/details/17190061
看完之后问题来了,
1,工程里面大量的 xib
2,一个简单的思路,当代码触发切换语言 发个通知让 内存里的viewController 刷新UI的text 。[工程里这么都vc 每个都加 岂不累死]
所有 接下来还可完善下。
1,切换语言,重新加载window 的根视图。根据不同语言。【搜下网上的storyboard 国际化】
你会发现 你操作后 xcode 为Main storyboard 添加了三个 文件
这里依这为理。base和你添加的语言。
你在这些文件上右键 show in finder 。
看到那三个文件夹没 .lproj
[[NSBundle mainBundle] pathForResource:userLanguage ofType:@"lproj"];
根据path 得到
NSBundle
熟悉吧。这玩意里面就是我们程序需要得资源文件。其实就是XX.lproj 文件夹里德东东
初始化
[UIStoryboard storyboardWithName:@"Main" bundle:bundle];
我们更习惯于 后面的参数滋味nil 。
写到这有个问题。其实真正的main 放在了那个base 文件夹里。程序蹦了。让我们来干掉base 。千万别直接删除base文件。那样直接把main 。storyboard 删了。
在这里
点文件。右边栏。去掉base 沟。不要管警告。
再看
现在可以了。分别在两个文件夹了。
遗留问题:如果支持很多语言,程序会变的很大.有解决的可以交流下
这里可以附上关于 Base 文件的信息,
http://samwize.com/2012/11/22/warning-do-not-use-base-internationalization-in-ios-5/
如里面介绍,iOS 5不支持国际化 Base。 它是在ios6引入的。
2, 写一个vc 基类。注册 和 移除
在subVC,这里注意防止循环引用。_weak
在superVC
遍历执行。实现自动刷新
如果你国际化后 还有接着修改storyboard 你在手动去添加 ui 到国际化文件 。string
ibtool MainStoryboard.storyboard --generate-strings-file New.strings (你也可以取别的名字(New.strings))
每次改完都要执行下确实挺麻烦。网上有个xcode脚本 可以在每次bulid 时帮我们完成,见下面博客:
http://oleb.net/blog/2013/02/automating-strings-extraction-from-storyboards-for-localization/
整理出得脚本,注释挺详细了。
storyboardExt=".storyboard"
stringsExt=".strings"
newStringsExt=".strings.new"
oldStringsExt=".strings.old"
localeDirExt=".lproj"
# Find storyboard file full path inside project folder
for storyboardPath in `find ${SRCROOT} -name "*$storyboardExt" -print`
do
# Get Base strings file full path
baseStringsPath=$(echo "$storyboardPath" | sed "s/$storyboardExt/$stringsExt/")
# Create strings file only when storyboard file newer
#if find $storyboardPath -prune -newer $baseStringsPath -print | grep -q .; then
# Get storyboard file name and folder
storyboardFile=$(basename "$storyboardPath")
storyboardDir=$(dirname "$storyboardPath")
# Get New Base strings file full path and strings file name
newBaseStringsPath=$(echo "$storyboardPath" | sed "s/$storyboardExt/$newStringsExt/")
stringsFile=$(basename "$baseStringsPath")
ibtool --export-strings-file $newBaseStringsPath $storyboardPath
iconv -f UTF-16 -t UTF-8 $newBaseStringsPath > $baseStringsPath
rm $newBaseStringsPath
# Get all locale strings folder
for localeStringsDir in `find ${SRCROOT} -name "*$localeDirExt" -print`
do
# Skip Base strings folder
if [ $localeStringsDir != $storyboardDir ]; then
localeStringsPath=$localeStringsDir/$stringsFile
# Just copy base strings file on first time
if [ ! -e $localeStringsPath ]; then
cp $baseStringsPath $localeStringsPath
else
oldLocaleStringsPath=$(echo "$localeStringsPath" | sed "s/$stringsExt/$oldStringsExt/")
cp $localeStringsPath $oldLocaleStringsPath
# Merge baseStringsPath to localeStringsPath
awk 'NR == FNR && /^\/\*/ {x=$0; getline; a[x]=$0; next} /^\/\*/ {x=$0; print; getline; $0=a[x]?a[x]:$0; printf $0"\n\n"}' $oldLocaleStringsPath $baseStringsPath > $localeStringsPath
rm $oldLocaleStringsPath
fi
fi
done
#else
#echo "$storyboardPath file not modified."
#fi
done
以上可以做到 在后续修改sb文件后,脚本会自动 将新加内容 添加到 .string文件
格式如下:
"NT7-J3-MH3.text" = "Test2";
默认重新生成.string文件后 会覆盖原来的 内容。但这里脚本使用正则表达式 合并了。有兴趣可以搜下 shell 文件的合并。这玩意挺强大的。
使用脚本的前提是 你工程里用了base.lproj 。但上面讨论的 代码切换 sb 文件不能用 base ,而且要支持ios5 使用base 会奔溃掉。
那只能编辑sb 文件时用 base ,之后运行把base 去掉。在运行;也就去掉base 的勾选。
后续解决能不能 shell 将sb 文件拷贝到APP 包中对应的 语言.lproj 。这样不用每次编辑-运行 都要手动去base沟选。
尝试了下,可以实现 。但app 里文件li sb 文件后缀名为.storyboardc
有兴趣可以下载demo 里de Debug_st.sh 正是解决这个问题的。#debug if
搞定后会再次更新。。。
可以在这里下载我的demo工程:
刷新storyboard + 代码刷新 文案