| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | #!/usr/bin/env bash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # -------------------------------------------------------------------------------------------------- | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Create PO files to make easier the labor of translation. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Info: | 
					
						
							|  |  |  | # 	https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html | 
					
						
							|  |  |  | # 	https://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/json2po.html | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Dependencies: | 
					
						
							|  |  |  | # 	jq | 
					
						
							|  |  |  | # 	translate-toolkit | 
					
						
							|  |  |  | # 		python-wcwidth | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Created by @hasecilu | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # -------------------------------------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | number_of_keys() { | 
					
						
							|  |  |  | 	[ -f "$1" ] && jq 'path(..) | select(length == 2) | .[1]' "$1" | wc -l || echo "0" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | stats() { | 
					
						
							|  |  |  | 	# Print the number of existing strings on the JSON files for each locale | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 	s=$(number_of_keys "${paths[0]}/en/server.json") | 
					
						
							|  |  |  | 	c=$(number_of_keys "${paths[1]}/en/translation.json") | 
					
						
							|  |  |  | 	echo "| locale |server strings |client strings |" | 
					
						
							|  |  |  | 	echo "|--------|---------------|---------------|" | 
					
						
							|  |  |  | 	echo "|   en   |      ${s}      |     ${c}      |" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	for locale in "${locales[@]}"; do | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 		s=$(number_of_keys "${paths[0]}/${locale}/server.json") | 
					
						
							|  |  |  | 		c=$(number_of_keys "${paths[1]}/${locale}/translation.json") | 
					
						
							|  |  |  | 		n1=$(((8 - ${#locale}) / 2)) | 
					
						
							|  |  |  | 		n2=$((n1 == 1 ? n1 + 1 : n1)) | 
					
						
							|  |  |  | 		echo "|$(printf "%${n1}s")${locale}$(printf "%${n2}s")|      ${s}      |     ${c}      |" | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | update_1() { | 
					
						
							|  |  |  | 	# Update PO files from English and localized JSON files as source | 
					
						
							|  |  |  | 	# NOTE: if you want a new language you need to first create the JSON files | 
					
						
							|  |  |  | 	# on their corresponding place with `{}` as content to avoid error on `json2po` | 
					
						
							|  |  |  | 	local locales=("$@") | 
					
						
							|  |  |  | 	for path in "${paths[@]}"; do | 
					
						
							|  |  |  | 		for locale in "${locales[@]}"; do | 
					
						
							|  |  |  | 			json2po -t "${path}/en" "${path}/${locale}" "${path}/po-${locale}" | 
					
						
							|  |  |  | 		done | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | update_2() { | 
					
						
							|  |  |  | 	# Recover translation from PO files to localized JSON files | 
					
						
							|  |  |  | 	local locales=("$@") | 
					
						
							|  |  |  | 	for path in "${paths[@]}"; do | 
					
						
							|  |  |  | 		for locale in "${locales[@]}"; do | 
					
						
							|  |  |  | 			po2json -t "${path}/en" "${path}/po-${locale}" "${path}/${locale}" | 
					
						
							|  |  |  | 		done | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | help() { | 
					
						
							|  |  |  | 	echo -e "\nDescription:" | 
					
						
							|  |  |  | 	echo -e "\tCreate PO files to make easier the labor of translation" | 
					
						
							|  |  |  | 	echo -e "\nUsage:" | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 	echo -e "\t./translation.sh [--stats] [--update1 <OPT_LOCALE>] [--update2 <OPT_LOCALE>]" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	echo -e "\nFlags:" | 
					
						
							|  |  |  | 	echo -e "  --clear\n\tClear all po-* directories" | 
					
						
							|  |  |  | 	echo -e "  --stats\n\tPrint the number of existing strings on the JSON files for each locale" | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 	echo -e "  --update1 <LOCALE>\n\tUpdate PO files from English and localized JSON files as source" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	echo -e "  --update2 <LOCALE>\n\tRecover translation from PO files to localized JSON files" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Main function ------------------------------------------------------------------------------------ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Get script directory to set file path relative to it | 
					
						
							|  |  |  | file_path="$( | 
					
						
							|  |  |  | 	cd -- "$(dirname "${0}")" >/dev/null 2>&1 || exit | 
					
						
							|  |  |  | 	pwd -P | 
					
						
							|  |  |  | )"
 | 
					
						
							|  |  |  | paths=("${file_path}/../translations/" "${file_path}/../src/public/translations/") | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | locales=(cn de es fr pt_br ro tw) | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | if [ $# -eq 1 ]; then | 
					
						
							|  |  |  | 	if [ "$1" == "--clear" ]; then | 
					
						
							|  |  |  | 		for path in "${paths[@]}"; do | 
					
						
							|  |  |  | 			for locale in "${locales[@]}"; do | 
					
						
							|  |  |  | 				[ -d "${path}/po-${locale}" ] && rm -r "${path}/po-${locale}" | 
					
						
							|  |  |  | 			done | 
					
						
							|  |  |  | 		done | 
					
						
							|  |  |  | 	elif [ "$1" == "--stats" ]; then | 
					
						
							|  |  |  | 		stats | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 	elif [ "$1" == "--update1" ]; then | 
					
						
							|  |  |  | 		update_1 "${locales[@]}" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	elif [ "$1" == "--update2" ]; then | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 		update_2 "${locales[@]}" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		help | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | elif [ $# -eq 2 ]; then | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 	if [ "$1" == "--update1" ]; then | 
					
						
							|  |  |  | 		update_1 "$2" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	elif [ "$1" == "--update2" ]; then | 
					
						
							| 
									
										
										
										
											2024-11-20 12:35:44 -06:00
										 |  |  | 		update_2 "$2" | 
					
						
							| 
									
										
										
										
											2024-10-17 12:05:21 -06:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		help | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  | 	help | 
					
						
							|  |  |  | fi |