繁体   English   中英

智能排序Localized.strings文件

[英]Smart ordering Localized.strings file

在我的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") in我的代码和生成对/* 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";

这个问题是我在这里可以找到的更复杂的变体: 重新排序.strings文件

认为这就是你想要的
在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

我认为这应该适用于Mac

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

这是另一种方式。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM