IOS APP 國際化(實現不跟隨系統語言,不用重啟應用,代碼切換stroyboard ,xib ,圖片,其他資源)


此問題已解決。請看:

 IOS APP 國際化 程序內切換語言實現 不重新啟動系統(完美解決方案) 

 

 接了個變態的需求,要在程序內切換程序語言實現國際化。

可以先看看這個,比較詳細。

http://blog.csdn.net/xwren362922604/article/details/17190061

看完之后問題來了,

1,工程里面大量的 xib  或 storyboard 里面的UI 設定了text ,怎么實現讓它們動態加載語言【非設置系統語言重啟】

2,一個簡單的思路,當代碼觸發切換語言 發個通知讓 內存里的viewController 刷新UI的text 。[工程里這么都vc 每個都加 豈不累死]

所有 接下來還可完善下。

1,切換語言,重新加載window 的根視圖。根據不同語言。【搜下網上的storyboard 國際化】

你會發現 你操作后 xcode 為Main storyboard 添加了三個 文件

這里依這為理。base和你添加的語言。
你在這些文件上右鍵 show in finder 。

看到那三個文件夾沒 .lproj  這里是對應語言的 資源文件。你切換語言是 拿到當前語言eg: en 拼路徑en.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 基類。注冊 和 移除  通知。對於自動刷新vc 內的 UI 。可以在基類里加個數組變量 【blok 的】

在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文件

格式如下:

/* Class = "IBUILabel"; text = "Test2"; ObjectID = "NT7-J3-MH3"; */

"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   ....  fi 里的東西是 為了調試故意讓它報錯的,不影響

搞定后會再次更新。。。

 

可以在這里下載我的demo工程: 

刷新storyboard + 代碼刷新 文案

 git clone   git@github.com:githhhh/Test_Local_Two.git   

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM