lang-add.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. #
  3. # lang-add.sh - multi-language support script
  4. # add new texts from list (lang_add.txt) to all dictionary files
  5. #
  6. # Input files:
  7. # lang_add.txt
  8. # Updated files:
  9. # lang_en.txt and all lang_en_xx.txt
  10. #
  11. # Config:
  12. if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fi
  13. if [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo 'Config NG!' >&2; exit 1; fi
  14. if [ ! -z "$COMMUNITY_LANGUAGES" ]; then
  15. LANGUAGES+=" $COMMUNITY_LANGUAGES"
  16. fi
  17. echo "fw-clean languages:$LANGUAGES" >&2
  18. # insert single text to english dictionary
  19. # $1 - text to insert
  20. insert_en()
  21. {
  22. #replace '[' and ']' in string with '\[' and '\]'
  23. str=$(echo "$1" | sed "s/\[/\\\[/g;s/\]/\\\]/g")
  24. # extract english texts, merge new text, grep line number
  25. ln=$((cat lang_en.txt; echo "$1") | sed "/^$/d;/^#/d" | sort | grep -n "$str" | sed "s/:.*//")
  26. # calculate position for insertion
  27. ln=$((3*(ln-2)+1))
  28. # insert new text
  29. sed -i "$ln"'i\\' lang_en.txt
  30. sed -i "$ln"'i\'"$1"'\' lang_en.txt
  31. sed -i "$ln"'i\#\' lang_en.txt
  32. }
  33. # insert single text to translated dictionary
  34. # $1 - text to insert
  35. # $2 - sufix
  36. insert_xx()
  37. {
  38. #replace '[' and ']' in string with '\[' and '\]'
  39. str=$(echo "$1" | sed "s/\[/\\\[/g;s/\]/\\\]/g")
  40. # extract english texts, merge new text, grep line number
  41. ln=$((cat lang_en_$2.txt; echo "$1") | sed "/^$/d;/^#/d" | sed -n 'p;n' | sort | grep -n "$str" | sed "s/:.*//")
  42. # calculate position for insertion
  43. ln=$((4*(ln-2)+1))
  44. # insert new text
  45. sed -i "$ln"'i\\' lang_en_$2.txt
  46. sed -i "$ln"'i\"\x00"\' lang_en_$2.txt
  47. sed -i "$ln"'i\'"$1"'\' lang_en_$2.txt
  48. sed -i "$ln"'i\#\' lang_en_$2.txt
  49. }
  50. # check if input file exists
  51. if ! [ -e lang_add.txt ]; then
  52. echo "file lang_add.txt not found"
  53. exit 1
  54. fi
  55. cat lang_add.txt | sed 's/^/"/;s/$/"/' | while read new_s; do
  56. if grep "$new_s" lang_en.txt >/dev/null; then
  57. echo "text already exist:"
  58. echo "$new_s"
  59. echo
  60. else
  61. echo "adding text:"
  62. echo "$new_s"
  63. echo
  64. #insert_en "$new_s"
  65. for lang in $LANGUAGES; do
  66. insert_xx "$new_s" "$lang"
  67. done
  68. #Use the 2 lines below as a template and replace 'qr'
  69. ##New language
  70. # insert_xx "$new_s" 'qr'
  71. fi
  72. done
  73. read -t 5
  74. exit 0