[英]Extracting localization strings from Settings.bundle plists
如果您在 Settings.bundle 中構建子窗格,您最終會得到幾個 .plist 文件。 當需要本地化您的項目時,您會發現相應的 .strings 文件的創建有點乏味(我知道我這樣做)。
這是一個方便的列表 bash 腳本,它將 (i) 查找標簽,(ii) 提取以下標簽的內容,然后 (iii) output 該值以 ibtool 所需的 "string" = "string" 格式的文本文件.
您將輸入和 output 文件名作為參數傳遞。
#!/bin/sh
echo "// Generated by plist2strings. Manual edits will be overwritten the next time this script runs.\n/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */\n" > plist2stringstmp
sed -n '
# look for a "#" at the end of the line
/<key>Title<\/key>$/ {
# Found one - now read in the next line
N
# delete the "#" and the new line character,
s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2"/gp
}' $1 > plist2stringstmp2
cat plist2stringstmp plist2stringstmp2 > $2
rm plist2stringstmp plist2stringstmp2
復制-N-粘貼到文本文件中。 我將我的保存為 plist2strings。 一定要給它執行權限。 例如,在終端執行:
macbook:~ foo$ chmod 755 plist2strings
要從 Settings.bundle 目錄的根目錄執行腳本(如果保存到您的用戶文件夾),只需使用以下語法:
macbook:~ foo$ ~/plist2strings mysettings.plist en.lprog/mysettings.strings
如果 mysettings.strings 不存在於該文件夾中,它將創建它。 如果它已經存在,它將在沒有警告的情況下自動覆蓋它。
希望有人覺得這很有用。 並隨意使用(和濫用)您認為合適的(買者自負;-)。 如果您進行了任何有用的更改,請考慮將它們張貼在這里以供其他人欣賞。
快樂本地化!
〜扎克
我使用這個更好的 quickfixed 版本(它只是刪除重復項)。
#!/bin/sh
echo "// Generated by plist2strings. Manual edits will be overwritten the next time this script runs.\n/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */\n" > plist2stringstmp
sed -n '
# look for a "#" at the end of the line
/<key>Title<\/key>$/ {
# Found one - now read in the next line
N
# delete the "#" and the new line character,
s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2"/gp
}' $1 > plist2stringstmp2
cat plist2stringstmp2 | sort | uniq > tmp
cat plist2stringstmp2 >> plist2stringstmp
mv plist2stringstmp $2
rm plist2stringstmp2
TODO:像這樣從標題數組中提取字符串:
<key>Titles</key>
<array>
<string>STH</string>
<string>STH2</string>
<string>STH3</string>
</array>
這是一種舊思路。 盡管如此,多虧了 OP Zack,我已經使用了一段時間的原始腳本。
我決定編輯腳本以按照 descent89 的建議從標題 arrays 中提取字符串。
此外,通過使用 2addr sed 語法,它現在更具可讀性。 我的版本不排序也不刪除重復項,因為使用標題 Arrays,保持字符串順序很有用。
這里是:
#!/bin/sh
echo "// Generated by plist2strings. Manual edits will be overwritten the next time this script runs.\n/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */\n\n/* String for Title elements */" > $2
sed -n '/<key>Title<\/key>$/,/<\/string>$/ s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2";/gp' $1 >> $2
echo "\n/* String for Titles arrays elements */" >> $2
sed -n '/<key>Titles<\/key>$/,/<\/array>$/ s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2";/gp' $1 >> $2
僅供參考,您可能會在這里或那里得到一些重復的字符串。 如果有人感到有動力,很高興看到 (a) 重復檢查,和/或 (b) 合並現有文件的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.