智能排序Localized.strings文件

时间:2014-11-28 10:33:03

标签: ios objective-c iphone bash localization

在我的Localizable.Strings中,我尝试按字母顺序排列所有对。是否可以按字母顺序对Localizable.strings进行重新排序? Maby使用genstring或特殊的bash脚本?

此处我还有其他要求:

1。订购应该不区分大小写。

2。应复制第一行X(例如五行),而不是订购。

应该满足此要求,因为在Localized.strings文件中,我将作者,公司名称和产品名称作为评论。

3。保留评论

我想对翻译的字符串保留注释,并在每个翻译之间保留新的界限。这个评论是针对iOS开发人员的特殊genstrings命令的基础(例如find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj在我的代码中查找所有NSLocalizedString(@"Param",@"Comment")并生成/* Comment */ /r/n "Param" = "Param";对文件)。翻译前的注释行是可选的,可能只有1行。例如文件:

/* This is Billy */
"Billy" = "The smartest guy in the univererse";

/* The Hitchhiker's Guide to the Galaxy */
"42" = "the answer to life the universe and everything";

"Johny" = "Johny";

/* Optional field */
"Anny" = "Anny";

输出应为:

/* The Hitchhiker's Guide to the Galaxy */
"42" = "the answer to life the universe and everything";

/* Optional field */
"Anny" = "Anny";

/* This is Billy */
"Billy" = "The smartest guy in the univererse";

"Johny" = "Johny";

这个问题是我在这里可以找到的更复杂的变体:Reorder .strings file

2 个答案:

答案 0 :(得分:3)

认为这就是你想要的 在awk

awk 'BEGIN{RS="";FS="\n"}
{t=$NF}

match(t,/^"([^"]+)/,a){
    key[NR]=tolower(a[1])"\t"++x
    b[x]=$0
}

END {
    asort(key)
    for (i=1; i<=x; i++) {
        split(key[i],a,"\t")
        print b[a[2]] "\n"
    }
}' file

输出

/* The Hitchhiker's Guide to the Galaxy */
"42" = "the answer to life the universe and everything";

/* Optional field */
"Anny" = "Anny";

/* This is Billy */
"Billy" = "The smartest guy in the univererse";

"Johny" = "Johny";

修改

要跳过前5行并仍然打印

awk 'NR<6{print;next}
NR==6{RS="";FS="\n"}
{t=$NF}

match(t,/^"([^"]+)/,a){
    key[NR]=tolower(a[1])"\t"++x
    b[x]=$0
}

END {
    asort(key)
    for (i=1; i<=x; i++) {
        split(key[i],a,"\t")
        print b[a[2]] "\n"
    }
}' file

编辑2

我认为这应该适用于Macs

awk 'NR<6{print;next}
NR==6{RS="";FS="\n"}
{t=$NF}

split(t,a,"\""){
    key[NR]=tolower(a[2])"\t"++x
    b[x]=$0
}

END {
    asort(key)
    for (i=1; i<=x; i++) {
        split(key[i],a,"\t")
        print b[a[2]] "\n"
    }
}' file

答案 1 :(得分:1)

这是另一种方式。

X=5; file=<file>; \
head -n $X $file && \
cat $file | sed '1,'$X'd' | \
sed 's/\([^;]\)$/\1@@@/g' | \
tr -d '\n' | \
tr ';' '\n' | \
sed 's/$/;/g' | \
awk -F "@@@" '{print $2"@@@"$1}' | \
sed 's/^@@@//g' | \
sort --ignore-case | \
awk -F "@@@" '{print $2"\n"$1"\n"}' | \
cat -s

解释

X=5; file=<file>; \                     # define variables
head -n $X $file && \                   # output first set of lines
cat $file | sed '1,'$X'd' | \           # process rest of the lines
sed 's/\([^;]\)$/\1@@@/g' | \           # append @@@ to lines not ending with semicolon
tr -d '\n' | \                          # remove all new lines and make a single line string
tr ';' '\n' | \                         # break single string into multiple lines at semicolons
sed 's/$/;/g' | \                       # add semicolons at the end of lines
awk -F "@@@" '{print $2"@@@"$1}' | \    # swap comment and translation
sed 's/^@@@//g' | \                     # remove extra @@@ of translations without comments
sort --ignore-case | \                  # sort
awk -F "@@@" '{print $2"\n"$1"\n"}' | \ # swap translation and comment, print with new lines
cat -s                                  # remove extra new lines