i18n-add-language.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. LANGS=$@
  3. if [ "$#" -eq 0 ]; then
  4. echo $(basename "$0") "adds i18n catalogue(s) in po/ folders (luci-app-*, luci-mod-*, etc) for each LUCI_LANG.* in luci.mk"
  5. echo "Hint: run in the root of the luci repo or in your luci-app-* folder."
  6. # get existing language codes from luci.mk
  7. language_codes=$(grep -o 'LUCI_LANG\.[a-zA-Z_-]*' $(dirname "$0")/../luci.mk | cut -d '.' -f 2 | sort -u)
  8. LANGS=$language_codes
  9. else
  10. for LANG in $LANGS; do
  11. case "$LANG" in
  12. [a-z][a-z]|[a-z][a-z][_-][A-Za-z][A-Za-z]*) : ;;
  13. *)
  14. echo $(basename "$0") "adds i18n catalogues in each folder (luci-app-*, luci-mod-*, etc)."
  15. echo "Usage: $(basename "$0") <ISO_CODE> [<ISO_CODE> <ISO_CODE> ...]" >&2
  16. exit 1
  17. ;;
  18. esac
  19. done
  20. fi
  21. ADDED=false
  22. for podir in $(find . -type d -name "po"); do
  23. [ -d "$podir/templates" ] || continue
  24. for LANG in $LANGS; do
  25. # if "$podir/$LANG" doesn't exist, mkdir
  26. [ -d "$podir/$LANG" ] || mkdir "$podir/$LANG"
  27. for catalog in $(cd "$podir/templates"; echo *.pot); do
  28. if [ -f "$podir/templates/$catalog" -a ! -f "$podir/$LANG/${catalog%.pot}.po" ]; then
  29. msginit --no-translator -l "$LANG" -i "$podir/templates/$catalog" -o "$podir/$LANG/${catalog%.pot}.po"
  30. git add "$podir/$LANG/${catalog%.pot}.po"
  31. ADDED=true
  32. fi
  33. done
  34. done
  35. done
  36. start_marker="^#LUCI_LANG_START$"
  37. end_marker="^#LUCI_LANG_END$"
  38. if [ $ADDED ]; then
  39. for LANG in $LANGS; do
  40. if [[ $language_codes != *"$LANG"* ]]; then
  41. # Read the contents of the luci.mk file
  42. file_content=$(cat "$(dirname "$0")/../luci.mk")
  43. # Extract the section between start and end markers
  44. section=$(awk -v start="$start_marker" -v end="$end_marker" '
  45. $0 ~ start {RS="\n"; printf ""; flag=1; next}
  46. $0 ~ end {flag=0} flag' <<< "$file_content")
  47. # Add the new language code to the section
  48. section+="\nLUCI_LANG.$LANG=New language"
  49. # Sort the section and remove duplicates
  50. updated_content=$(echo -e "$section" | sort -u | sed -E "/$start_marker/,/$end_marker/{ /$start_marker/{p; r /dev/stdin
  51. }; /$end_marker/p; d
  52. }" $(dirname "$0")/../luci.mk)
  53. # Write the updated content back to the .mk file
  54. echo "$updated_content" > $(dirname "$0")/../luci.mk
  55. echo "Be sure to update the new language name in $(dirname "$0")/../luci.mk"
  56. fi
  57. done
  58. fi