Reputation: 2252
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:
This requirement should be met because in Localized.strings file I have author, company name and product name as a comment on top.
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 (e.g. 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
Upvotes: 4
Views: 1382
Reputation: 2219
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
Upvotes: 1
Reputation:
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
/* 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";
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
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
Upvotes: 3