简体   繁体   中英

Smart ordering Localized.strings file

In my Localizable.Strings I try to have all pairs in alphabetical order. Is it possible to reorder them alphabetically my Localizable.strings ? Maby using genstring or special bash script?

Here I have additional requirements to complete:

1. Ordering should be case insensitive.

2. First X (eg five) lines should be copied, not ordered.

This requirement should be met because in Localized.strings file I have author, company name and product name as a comment on top.

3. Keep comments

I want to keep comments to the translated strings and keep new lines between each translation. This comments are generetad by special genstrings command for iOS developers (eg find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj find all NSLocalizedString(@"Param",@"Comment") in my code and generate pairs /* Comment */ /r/n "Param" = "Param"; to file). Comment line before translation is optional and may have only 1 line . For example file:

/* 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 output should be:

/* 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";

This question is the more sophisticated variant ot my own question that you can find here: Reorder .strings file

Think this is what you want
In 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

output

/* 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";

EDIT

To skip the first 5 lines and still print them

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

EDIT 2

I think this should work on 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

Here's another way.

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

Explained.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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