lang-add.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. # insert single text to english dictionary
  12. # $1 - text to insert
  13. insert_en()
  14. {
  15. #replace '[' and ']' in string with '\[' and '\]'
  16. str=$(echo "$1" | sed "s/\[/\\\[/g;s/\]/\\\]/g")
  17. # extract english texts, merge new text, grep line number
  18. ln=$((cat lang_en.txt; echo "$1") | sed "/^$/d;/^#/d" | sort | grep -n "$str" | sed "s/:.*//")
  19. # calculate position for insertion
  20. ln=$((3*(ln-2)+1))
  21. # insert new text
  22. sed -i "$ln"'i\\' lang_en.txt
  23. sed -i "$ln"'i\'"$1"'\' lang_en.txt
  24. sed -i "$ln"'i\#\' lang_en.txt
  25. }
  26. # insert single text to translated dictionary
  27. # $1 - text to insert
  28. # $2 - sufix
  29. insert_xx()
  30. {
  31. #replace '[' and ']' in string with '\[' and '\]'
  32. str=$(echo "$1" | sed "s/\[/\\\[/g;s/\]/\\\]/g")
  33. # extract english texts, merge new text, grep line number
  34. ln=$((cat lang_en_$2.txt; echo "$1") | sed "/^$/d;/^#/d" | sed -n 'p;n' | sort | grep -n "$str" | sed "s/:.*//")
  35. # calculate position for insertion
  36. ln=$((4*(ln-2)+1))
  37. # insert new text
  38. sed -i "$ln"'i\\' lang_en_$2.txt
  39. sed -i "$ln"'i\"\x00"\' lang_en_$2.txt
  40. sed -i "$ln"'i\'"$1"'\' lang_en_$2.txt
  41. sed -i "$ln"'i\#\' lang_en_$2.txt
  42. }
  43. # check if input file exists
  44. if ! [ -e lang_add.txt ]; then
  45. echo "file lang_add.txt not found"
  46. exit 1
  47. fi
  48. cat lang_add.txt | sed 's/^/"/;s/$/"/' | while read new_s; do
  49. if grep "$new_s" lang_en.txt >/dev/null; then
  50. echo "text already exist:"
  51. echo "$new_s"
  52. echo
  53. else
  54. echo "adding text:"
  55. echo "$new_s"
  56. echo
  57. insert_en "$new_s"
  58. insert_xx "$new_s" 'cz'
  59. insert_xx "$new_s" 'de'
  60. insert_xx "$new_s" 'es'
  61. insert_xx "$new_s" 'fr'
  62. insert_xx "$new_s" 'it'
  63. insert_xx "$new_s" 'pl'
  64. fi
  65. done
  66. read -t 5
  67. exit 0