首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >智能排序Localized.strings文件

智能排序Localized.strings文件
EN

Stack Overflow用户
提问于 2014-11-28 10:33:02
回答 2查看 1.1K关注 0票数 4

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

这里我还有额外的要求要完成:

1.排序应不区分大小写。

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

这一要求应该得到满足,因为在Localized.strings文件中,我有作者、公司名称和产品名称作为评论。

3.保留意见

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

代码语言:javascript
复制
/* 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";

产出应是:

代码语言:javascript
复制
/* 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-28 11:13:24

你觉得这是你想要的

在awk

代码语言:javascript
复制
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

输出

代码语言:javascript
复制
/* 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行并仍打印它们

代码语言:javascript
复制
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电脑

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2014-11-28 14:26:56

这是另一种方法。

代码语言:javascript
复制
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

解释一下。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27186763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档