Skip to content

Instantly share code, notes, and snippets.

@Flower7C3
Last active January 6, 2023 20:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flower7C3/c565a6b7467f455905041503d79214c0 to your computer and use it in GitHub Desktop.
Save Flower7C3/c565a6b7467f455905041503d79214c0 to your computer and use it in GitHub Desktop.
Rebuild PrestaShop translations in child theme
PHP_PATh="/opt/php-7.1/bin/php"
APP_ROOT_DIR="/web/"
MAIN_LANG="pl-PL"
with_dry_run="y"
themes_dir_name="themes/"
translations_dir_name='translations/'
template_file_name='templates/example.tpl'
echo "REBUILD TRANSLATIONS"
cd $APP_ROOT_DIR
themes_list=($(cd "$themes_dir_name" && ls -d */))
for theme_name in "${themes_list[@]}"; do
theme_dir_path="${themes_dir_name}${theme_name}"
theme_name_no_slash=${theme_name/\//}
theme_dir_path_no_slash="${themes_dir_name}${theme_name_no_slash}"
theme_config_file="${theme_dir_path}config/theme.yml"
if [[ -f "$theme_config_file" ]]; then
is_child_theme=$(grep 'parent: ' "$theme_config_file")
if [[ -n "$is_child_theme" ]]; then
if [[ "$with_project" != "" ]] && [[ "$theme_name_no_slash" != "$with_project" ]]; then
echo "Skip theme $theme_name"
continue
fi
if [[ "$with_dry_run" == "n" ]]; then
echo "Processing theme $theme_name"
else
echo "Would process theme $theme_name"
fi
parent_theme_name=$(grep 'parent: ' "$theme_config_file"| cut -d " " -f 2 -)
parent_theme_dir_path="${themes_dir_name}${parent_theme_name}"
template_file_path="${theme_dir_path}${template_file_name}"
template_dir_path="$(dirname "$template_file_path")"
translation_dir_path="${theme_dir_path}${translations_dir_name}"
languages_list=($(cd "$translation_dir_path" && ls -d */))
if [[ "$with_dry_run" == "n" ]]; then
mkdir -p "$template_dir_path"
rm -rf "$template_file_path"
touch "$template_file_path"
fi
if [[ "$with_dry_run" == "n" ]]; then
echo "Processing theme $theme_name templates"
grep -r -h -o "$parent_theme_dir_path" --exclude="example.tpl" -e "{l s=\(.\+\) d=\(.\+\)}" >>"$template_file_path"
else
echo "Would process theme $theme_name templates"
fi
if [[ "${#languages_list[@]}" -gt "0" ]]; then
for language_name in "${languages_list[@]}"; do
language_dir_path="${translation_dir_path}${language_name}"
if [[ "$with_dry_run" == "n" ]]; then
info_line "Processing $theme_name in $language_name language"
rm -rf "${language_dir_path}"
translation_files_list=($(cd "$language_dir_path" && ls -f *.xlf))
translation_files_list_amount=${#translation_files_list}
if [[ "${#translation_files_list[@]}" -gt "0" ]]; then
for index in "${!translation_files_list[@]}"; do
file_name=${translation_files_list[$index}
translation_file_path="${language_dir_path}${file_name}"
translation_domain=${file_name}
translation_domain=$(echo "$translation_domain" | sed 's/\.pl-PL\.xlf//g')
translation_domain=$(echo "$translation_domain" | sed 's/^pl-PL/advanceblog/g')
translation_domain=$(echo "$translation_domain" | sed 's/[[:upper:]]/.&/g' | sed 's/^\.//g')
info_line ""
info_line "$(printf "Process file %d / %d (%s)" "$index" "$translation_files_list_amount" "$theme_name $language_name $translation_domain")"
oldIFS=$IFS
IFS=$'\n'
translations=($(sed -ne 's?.*<source>\(.*\)</source>.*?\1?p' "$translation_file_path"))
IFS=$oldIFS
for key in "${translations[@]}"; do
printf "{l s='%s' d='%s'}\n" "$key" "$translation_domain" >>"$template_file_path"
done
done
fi
else
echo "Would process theme $theme_name in $language_name language"
fi
done
fi
else
echo "Skip $theme_name"
fi
fi
done
if [[ "$with_dry_run" == "n" ]]; then
echo "Processing app/Resources/translations/default"
else
echo "Would process app/Resources/translations/default"
fi
cd $APP_ROOT_DIR
base_language_dir_path="app/Resources/translations/default/"
translation_files_list=($(cd "$base_language_dir_path" && ls -f *.xlf))
translation_files_list_amount=${#translation_files_list[@]}
if [[ "${#translation_files_list[@]}" -gt "0" ]]; then
for index in "${!translation_files_list[@]}"; do
file_name=${translation_files_list[$index]}
translation_file_path="${base_language_dir_path}${file_name}"
translation_domain=${file_name}
translation_domain=$(echo "$translation_domain" | sed 's/\.pl-PL\.xlf//g')
translation_domain=$(echo "$translation_domain" | sed 's/\.xlf//g')
translation_domain=$(echo "$translation_domain" | sed 's/^pl-PL/advanceblog/g')
translation_domain=$(echo "$translation_domain" | sed 's/[[:upper:]]/.&/g' | sed 's/^\.//g')
oldIFS=$IFS
IFS=$'\n'
translations=($(sed -ne 's?.*<source>\(.*\)</source>.*?\1?p' "$translation_file_path"))
IFS=$oldIFS
if [[ "$with_dry_run" == "n" ]]; then
info_line "$(printf "Process file %d / %d (%s)" "$index" "$translation_files_list_amount" "$translation_domain")"
for translation_key in "${translations[@]}"; do
for theme_name in "${themes_list[@]}"; do
theme_name_no_slash=${theme_name/\//}
if [[ "$with_project" != "" ]] && [[ "$theme_name_no_slash" != "$with_project" ]]; then
continue
fi
theme_dir_path="${themes_dir_name}${theme_name}"
template_file_path="${theme_dir_path}${template_file_name}"
theme_config_file="${theme_dir_path}config/theme.yml"
if [[ -f "$theme_config_file" ]]; then
is_child_theme=$(grep 'parent: ' "$theme_config_file")
if [[ -n "$is_child_theme" ]]; then
if [[ $translation_key == *"'"* ]]; then
printf "{l s=\"%s\" d='%s'}\n" "$translation_key" "$translation_domain" >>"$template_file_path"
else
printf "{l s='%s' d='%s'}\n" "$translation_key" "$translation_domain" >>"$template_file_path"
fi
fi
fi
done
done
else
echo "Would process file $translation_domain"
fi
done
fi
if [[ "$with_dry_run" == "n" ]]; then
echo "Processing debug:translation"
translations_debug=$(${PHP_PATH} ${APP_ROOT_DIR}bin/console debug:translation ${MAIN_LANG})
readarray -t arr <<<"$translations_debug"
first_line=(${arr[0]})
column_sizes=()
for col_no in "${!first_line[@]}"; do
column_sizes[$col_no]=${#first_line[$col_no]}
done
printf "${IYellow}> Process line "
arr_size=${#arr[@]}
arr_size_bytes=${#arr_size}
for line_no in "${!arr[@]}"; do
printf "%-${arr_size_bytes}d / %-${arr_size_bytes}d" "$line_no" "$arr_size"
line="${arr[$line_no]}"
translation_domain_start=$((${column_sizes[0]} + 2))
translation_domain_size=${column_sizes[1]}
translation_domain=$(trim "${line:$translation_domain_start:$translation_domain_size}")
translation_key_start=$((${column_sizes[0]} + 2 + ${column_sizes[1]} + 1))
translation_key_size=${column_sizes[2]}
translation_key=$(trim "${line:$translation_key_start:$translation_key_size}")
if [[ "$translation_domain" != "Domain" ]] && [[ "$translation_domain" != "${first_line[1]}" ]]; then
for theme_name in "${themes_list[@]}"; do
theme_name_no_slash=${theme_name/\//}
if [[ "$with_project" != "" ]] && [[ "$theme_name_no_slash" != "$with_project" ]]; then
continue
fi
theme_dir_path="${themes_dir_name}${theme_name}"
template_file_path="${theme_dir_path}${template_file_name}"
theme_config_file="${theme_dir_path}config/theme.yml"
if [[ -f "$theme_config_file" ]]; then
is_child_theme=$(grep 'parent: ' "$theme_config_file")
if [[ -n "$is_child_theme" ]]; then
if [[ $translation_key == *"'"* ]]; then
printf "{l s=\"%s\" d='%s'}\n" "$translation_key" "$translation_domain" >>"$template_file_path"
else
printf "{l s='%s' d='%s'}\n" "$translation_key" "$translation_domain" >>"$template_file_path"
fi
fi
fi
done
fi
for ((i = 0; i < "$arr_size_bytes"; i++)); do
printf "\b\b"
done
printf "\b\b\b"
done
printf "${Color_Off}\n"
else
echo "Would process debug:translation"
fi
if [[ "$with_dry_run" == "n" ]]; then
info_line "Processing sort translations"
for theme_name in "${themes_list[@]}"; do
theme_name_no_slash=${theme_name/\//}
if [[ "$with_project" != "" ]] && [[ "$theme_name_no_slash" != "$with_project" ]]; then
continue
fi
theme_dir_path="${themes_dir_name}${theme_name}"
template_file_path="${theme_dir_path}${template_file_name}"
theme_config_file="${theme_dir_path}config/theme.yml"
if [[ -f "$theme_config_file" ]]; then
is_child_theme=$(grep 'parent: ' "$theme_config_file")
if [[ -n "$is_child_theme" ]]; then
sort ${template_file_path} | uniq > ${template_file_path}.uniq
mv ${template_file_path}.uniq ${template_file_path}
fi
fi
done
else
echo "Would process sort translations"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment