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