-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-l10n.sh
executable file
·151 lines (136 loc) · 4.15 KB
/
helm-l10n.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
DEFAULT_KEYWORDS="--keyword= --keyword=Name --keyword=Instance --keyword=Comment"
function help()
{
cat <<-EOF
Usage:
helm l10n [flags] [command] <chart_path>
Available Commands:
help This help text
init Create basic structure for localization support
update_messages Generate pot file and update any .po files inside po/
compile Generate chart.l10n in chart's directory
Flags:
-k string |
--add-keyword string Add a custom keyword for translation strings
--no-default-keywords Don't use default keywords for translation strings
Init Flags:
--full-instance-name Use full instance name by default
EOF
}
function init()
{
chart=$1
[ -z "$chart" ] && echo "ERROR: chart path parameter is required" && exit 1
chart_name=$(basename $(realpath $1))
if ! grep -q chart.l10n.in "$chart/.helmignore"; then
echo chart.l10n.in >> $chart/.helmignore
echo po/ >> $chart/.helmignore
fi
sed "s/INSTANCE/$INSTANCE/g;s/CHARTNAME/$chart_name/g" $HELM_PLUGIN_DIR/l10n.yaml > $chart/templates/l10n.yaml
sed "s/EXTRA_INSTANCE_DEFAULT/$EXTRA_INSTANCE_DEFAULT/g;s/CHARTNAME/$chart_name/g" $HELM_PLUGIN_DIR/l10n-extra.yaml > $chart/templates/l10n-extra.yaml
mkdir $chart/po 2> /dev/null || :
if [ ! -f "$chart/chart.l10n.in" ]; then
cat > $chart/chart.l10n.in <<-EOF
Name=$chart_name
Instance={{ .Release.Name }}
#[name:instance:alias:3]
#Instance=the instance in {{ .Release.Name }}
#[postgres::bahman]
#Instance=Bahman subchart of postgres
EOF
fi
echo "Basic l10n structure created. You can now add source strings to $chart/chart.l10n.in"
}
function update_messages()
{
chart=$1
[ -z "$chart" ] && echo "ERROR: chart path parameter is required" && exit 1
chart_name=$(basename $(realpath $chart))
echo "Generating message catalogue file: $chart/po/$chart_name.pot"
xgettext -Ldesktop $KEYWORDS "$chart/chart.l10n.in" -o "$chart/po/$chart_name.pot"
POFILES=$(ls "$chart/po/"*.po 2> /dev/null)
if [ -z "$POFILES" ]; then
echo "WARNING: No .po files found in $chart/po directory to update."
else
for pofile in $POFILES; do
echo "Updating translation file: $pofile"
msgmerge -U "$pofile" "$chart/po/$chart_name.pot"
done
fi
}
function compile()
{
chart=$1
[ -z "$chart" ] && echo "ERROR: chart path parameter is required" && exit 1
POFILES=$(ls "$chart/po/"*.po 2> /dev/null)
LINGUAS=()
for pofile in $POFILES; do
LINGUAS+=($(basename "$pofile" .po))
done
cat > "$chart/po/LINGUAS" <<-EOF
# Automatically generated. Will be overwritten by 'helm l10n compile'
${LINGUAS[*]}
EOF
echo "Compiling l10n strings into $chart/chart.l10n..."
msgfmt --desktop $KEYWORDS --template "$chart/chart.l10n.in" -d "$chart/po" -o "$chart/chart.l10n"
}
TEMP=$(getopt -o "hk:" --long 'help,add-keyword:,no-default-keywords,full-instance-name' -n 'helm l10n' -- "$@")
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$TEMP"
unset TEMP
INSTANCE=.Release.Name
EXTRA_INSTANCE_DEFAULT="$.Release.Name"
while true; do
case $1 in
--help|-h)
help
exit 0
;;
--add-keyword|-k)
CUSTOM_KEYWORDS+=("--keyword=$2")
shift 1
;;
--no-default-keywords)
DEFAULT_KEYWORDS="--keyword="
;;
--full-instance-name)
INSTANCE='include "CHARTNAME.instance" .'
EXTRA_INSTANCE_DEFAULT='(printf "%s-%s" $chart._0 $.Release.Name)'
;;
--)
shift
break
;;
*)
# Should've already failed at getopt!
echo "ERROR: Unknown option: $1"
exit 1
;;
esac
shift
done
KEYWORDS="$DEFAULT_KEYWORDS ${CUSTOM_KEYWORDS[*]}"
case $1 in
help|'')
help
;;
init)
shift
init $@
;;
update_messages)
shift
update_messages $@
;;
compile)
shift
compile $@
;;
*)
echo "ERROR: Unknown command: $1"
exit 1
;;
esac