Smart ordering of Localized.strings file
In mine, Localizable.Strings
I try to have all the pairs in alphabetical order. Can I reorder them alphabetically on mine Localizable.Strings
? Maby uses genstring or a custom bash script?
Here I have additional requirements to complete:
1. The order must be case insensitive.
2. The first X (for example, five) lines should be copied, not ordered.
This requirement has to be met because in the Localized.strings file I have the author, company name and product name as comment above.
3. Save comments
I want to keep comments on translated strings and keep newlines between each translation. These comments are generated by a dedicated genstrings command for iOS developers (e.g. find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj
find everything NSLocalizedString(@"Param",@"Comment")
in my code and generate pairs /* Comment */ /r/n "Param" = "Param";
to a file). The pre-translation comment line is optional and can only contain one line. For example file:
/* This is Billy */
"Billy" = "The smartest guy in the univererse";
/* The Hitchhiker 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 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 a more complex variation on my own question, which you can find here: Change the order of the .strings file
source to share
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 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 print them again
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 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
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
Clarifications.
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
source to share