update-languages.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. set -e
  3. update_linguas() {
  4. local DIR="$1"
  5. local LINGUAS_FILE="$DIR/LINGUAS"
  6. echo "update $LINGUAS_FILE"
  7. ls -1 "$DIR"/*.po | while read FILE; do
  8. basename -s .po "$FILE";
  9. done > "$LINGUAS_FILE"
  10. }
  11. firstword() {
  12. echo "$1"
  13. }
  14. read_linguas() {
  15. local FILE="$1"
  16. echo " - read $FILE"
  17. while read LINE; do
  18. local CODE="$(firstword $LINE)"
  19. if [ ! -z "$CODE" ]; then
  20. CODE_MAP[$CODE]="$CODE"
  21. NAME_MAP[$CODE]=""
  22. COMMENT_MAP[$CODE]=" <------- TODO: Set language name and remove this comment"
  23. else
  24. echo "$LINE" >> "$TMPFILE"
  25. fi
  26. done < "$FILE"
  27. }
  28. update_languages_inc() {
  29. local LANGUAGES_FILE="synfig-studio/src/languages.inc.c"
  30. echo "update LANGUAGES_FILE"
  31. local TMPFILE="$LANGUAGES_FILE.process"
  32. rm -f "$TMPFILE"
  33. declare -A CODE_MAP
  34. declare -A NAME_MAP
  35. declare -A COMMENT_MAP
  36. read_linguas "synfig-core/po/LINGUAS"
  37. read_linguas "synfig-studio/po/LINGUAS"
  38. echo " - read $LANGUAGES_FILE"
  39. while read LINE; do
  40. local COMMENT="$(echo "$LINE" | sed "s|//|#|" | cut -d \# -f 2- -s)"
  41. local SUBLINE="$(echo "$LINE" | sed "s|//|#|" | cut -d \# -f 1)"
  42. local CODE="$(echo "$SUBLINE" | cut -d \" -f 2)"
  43. local NAME="$(echo "$SUBLINE" | cut -d \" -f 4)"
  44. if [ ! -z "$CODE" ] || [ ! -z "$NAME" ]; then
  45. CODE_MAP[$CODE]="$CODE"
  46. NAME_MAP[$CODE]="$NAME"
  47. COMMENT_MAP[$CODE]="$COMMENT"
  48. else
  49. # write all unknown rows to begining of file
  50. echo "$LINE" >> "$TMPFILE"
  51. fi
  52. done < "$LANGUAGES_FILE"
  53. # fit the colums to make beautifulest table in the world
  54. local CODE_LEN=0
  55. local NAME_LEN=0
  56. for i in ${!CODE_MAP[@]}; do
  57. if [ ${#CODE_MAP[$i]} -gt $CODE_LEN ]; then
  58. CODE_LEN=${#CODE_MAP[$i]}
  59. fi
  60. if [ ${#NAME_MAP[$i]} -gt $NAME_LEN ]; then
  61. NAME_LEN=${#NAME_MAP[$i]}
  62. fi
  63. done
  64. CODE_LEN=$((CODE_LEN+3))
  65. NAME_LEN=$((NAME_LEN+3))
  66. # sort
  67. SORTED=$(for i in ${!CODE_MAP[@]}; do echo $i; done | sort | while read l; do echo -n "$l "; done)
  68. # write languages
  69. NEW_LANGS=
  70. for i in $SORTED; do
  71. CODE=${CODE_MAP[$i]}
  72. NAME=${NAME_MAP[$i]}
  73. COMMENT=${COMMENT_MAP[$i]}
  74. if [ ! -z "$COMMENT" ]; then
  75. COMMENT=" // $COMMENT"
  76. fi
  77. if [ -z "$NAME" ]; then
  78. NEW_LANGS="$NEW_LANGS $CODE"
  79. fi
  80. printf "{ %-${CODE_LEN}s %-${NAME_LEN}s },%s\\n" "\"$CODE\"," "\"$NAME\"," "$COMMENT" >> "$TMPFILE"
  81. done
  82. # update file
  83. mv $TMPFILE $LANGUAGES_FILE
  84. if [ ! -z "$NEW_LANGS" ]; then
  85. echo ""
  86. echo "please, enter names for following languages:$NEW_LANGS"
  87. echo ""
  88. echo "see file $LANGUAGES_FILE"
  89. echo ""
  90. fi
  91. }
  92. cd "$(cd `dirname "$0"`; pwd)/.."
  93. update_linguas "synfig-core/po"
  94. update_linguas "synfig-studio/po"
  95. update_languages_inc
  96. echo "done"