Smart ordering Localized.strings file
bash, ios, iphone, localization, objective-c
Solution
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
Problem
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 (e.g. 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 (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