I\'ve been trying to sort an i18n translations YAML file with Ruby so I can manage new translations in a better and organized way, but I\'ve been wondering if there is something
You shouldn't use the YAML library like suggested in the other answers. It will screw up the formatting of long string values, remove your comments and spit unreadable char escapes when you use accents and special characters (which you will, since you are doing i18n). Use this gem I created:
https://github.com/redealumni/i18n_yaml_sorter
It will only sort the lines on the file, so everything will remain the same way it was on the original yaml (your accents, the YAML construct you used to enter the strings, indentation, etc). It will work with deeply nested yamls and results are pretty solid. The gem includes tests and it's good for ruby 1.8 or 1.9.
It comes with a TextMate Bundle (Shift + Command + S) and a Rails rake task so you can sort the files easily and instantly in your editor. It's really fast.
To illustrate the difference:
Original:
pt-BR:
# Note how this is a nice way of inputing
# paragraphs of text in YAML.
apples: >
Maçãs são boas,
só não coma
seus iPods!
grapes: Não comemos elas.
bananas: |
Bananas são "legais":
- Elas são doces .
isto: não é chave
Por isto todos gostam de bananas!
Results by YAML::dump :
pt-BR:
apples: "Ma\xC3\xA7\xC3\xA3s s\xC3\xA3o boas, s\xC3\xB3 n\xC3\xA3o coma seus iPods!\n"
bananas: "Bananas s\xC3\xA3o \"legais\":\n - Elas s\xC3\xA3o doces .\n isto: n\xC3\xA3o \xC3\xA9 chave\n\n\ Por isto todos gostam de bananas!\n"
grapes: "N\xC3\xA3o comemos elas."
Results by i18n_yaml_sorter:
pt-BR:
# Note how this is a nice way of inputing
# paragraphs of text in YAML.
apples: >
Maçãs são boas,
só não coma
seus iPods!
bananas: |
Bananas são "legais":
- Elas são doces .
isto: não é chave
Por isto todos gostam de bananas!
grapes: Não comemos elas.