智能排序本地化字符串文件

4
在我的Localizable.Strings文件中,我尝试将所有键值对按字母顺序排列。是否有可能使用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";到文件中)。翻译之前的注释行是可选的,可能只有一行。例如,文件:
/* 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文件

在实际行之前是否可以有多行注释? - anubhava
Programming相关内容的翻译: - Szu
你也会以第二个字段排序吗? - user3442743
我所需要的只是按照第一个参数进行排序(翻译的关键)。 - Szu
2个回答

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

我认为这应该在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

我从未使用过awk,但我猜我应该在终端中使用这个命令。我收到以下错误信息:`awk: 语法错误,源代码第5行 上下文是
match(t,/^"([^"]+)/, <<< awk: 在源代码第16行退出`
- Szu
@szu 输入 awk --version 命令,可能不支持 match。 - user3442743
2
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Szu
2
@Szu:在OSX上,我使用home brew安装了gnu-awk。 - anubhava
我把它放在一个文件里: ``#!/usr/local/Cellar/gawk/4.1.4_1/bin/awk -fNR<7{print;next} NR==7{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" } }``但它没有起作用,有什么想法吗? - Kusan
显示剩余2条评论

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

这是一个脚本文件吗?我们该如何使用它? - Kusan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接