Last active
April 26, 2023 13:38
-
-
Save ku-enza/eca898e4134c159f63811c75a20888e7 to your computer and use it in GitHub Desktop.
Generate po files based on .pot file of a custom moduke in drupal 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Check if a command exists | |
| command_exists() { | |
| command -v "$1" >/dev/null 2>&1 | |
| } | |
| # Ensure drush is available | |
| if ! command_exists drush; then | |
| echo "Error: drush command not found. Please install drush and try again." | |
| exit 1 | |
| fi | |
| # Ensure awk is available | |
| if ! command_exists awk; then | |
| echo "Error: awk command not found. Please install awk and try again." | |
| exit 1 | |
| fi | |
| # Set default module path | |
| MODULE_PATH="$(pwd)" | |
| MODULE_NAME=$(grep -oP "name:\s*\K[^\n]+" *.info.yml | tr '[:lower:]' '[:upper:]' | tr ' ' '_' | sed 's/_$//') | |
| # Help function to display script usage | |
| display_help() { | |
| echo "Usage: $0 [options]" | |
| echo "Options:" | |
| echo " -mn, --module-name <module_name> Specify the name of the custom module (default: current directory)" | |
| echo " -h, --help Display this help message" | |
| echo " -v, --verbose Verbose output" | |
| exit 0 | |
| } | |
| # Parse the arguments | |
| while [ $# -gt 0 ]; do | |
| case "$1" in | |
| --module-name|-mn) | |
| MODULE_NAME_ARG="$2" | |
| shift 2 | |
| ;; | |
| -h|--help) | |
| display_help | |
| ;; | |
| -v|--verbose) | |
| VERBOSE=true | |
| shift | |
| ;; | |
| *) | |
| echo "Unknown argument: $1" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| # If module name is provided, search for the module folder | |
| if [ -n "${MODULE_NAME_ARG}" ]; then | |
| MODULE_PATH=$(find . -type d -path "*/modules/custom/${MODULE_NAME_ARG}") | |
| if [ -z "${MODULE_PATH}" ]; then | |
| echo "Module ${MODULE_NAME_ARG} not found!" | |
| exit 1 | |
| else | |
| if [ "$VERBOSE" = true ]; then | |
| echo "Module ${MODULE_NAME_ARG} found." | |
| fi | |
| fi | |
| else | |
| if [ "$VERBOSE" = true ]; then | |
| echo "Current module: ${MODULE_NAME}" | |
| fi | |
| fi | |
| # Function to get the .pot file and filename | |
| get_pot_file_and_name() { | |
| local POT_FILES=($(find "${MODULE_PATH}/translations" -type f -iname "*.pot")) | |
| if [ ${#POT_FILES[@]} -eq 0 ]; then | |
| echo "No .pot file!" | |
| exit 1 | |
| elif [ ${#POT_FILES[@]} -gt 1 ]; then | |
| echo "More than 1 .pot file!" | |
| exit 1 | |
| else | |
| POT_FILE="${POT_FILES[0]}" | |
| POT_FILE_NAME=$(basename "${POT_FILES[0]}" .pot) | |
| fi | |
| } | |
| # Function to get the list of language codes | |
| get_language_codes() { | |
| drush php:eval '$languages = \Drupal::languageManager()->getLanguages(); $default_language = \Drupal::languageManager()->getDefaultLanguage(); foreach ($languages as $language) { if ($language->getId() !== $default_language->getId()) { echo $language->getId() . PHP_EOL; } }' | |
| } | |
| # Function to get the custom module name | |
| get_module_name() { | |
| grep -oP "name:\s*\K[^\n]+" *.info.yml | tr '[:lower:]' '[:upper:]' | tr ' ' '_' | sed 's/_$//' | |
| } | |
| # Function to generate .po file based on .pot file | |
| generate_po_file() { | |
| local LANG_CODE="$1" | |
| local POT_FILE="$2" | |
| local OUTPUT_FILE="$3" | |
| # Add header to the .po file | |
| cat <<EOF >"$OUTPUT_FILE" | |
| # [${LANG_CODE}] translation of ${POT_FILE_NAME} module. | |
| # | |
| msgid "" | |
| msgstr "" | |
| "Project-Id-Version: ${POT_FILE_NAME}\n" | |
| "POT-Creation-Date: $(date -u +"%Y-%m-%d %H:%M%z")\n" | |
| "PO-Revision-Date: $(date -u +"%Y-%m-%d %H:%M%z")\n" | |
| "Last-Translator: NAME <EMAIL@ADDRESS>\n" | |
| "Language-Team: LANGUAGE <EMAIL@ADDRESS>\n" | |
| "Language: ${LANG_CODE}\n" | |
| "MIME-Version: 1.0\n" | |
| "Content-Type: text/plain; charset=utf-8\n" | |
| "Content-Transfer-Encoding: 8bit\n" | |
| "Plural-Forms: nplurals=2; plural=(n > 1);\n" | |
| EOF | |
| # Copy the msgid and msgstr from the .pot file starting after the first 'msgstr' line | |
| awk 'BEGIN {skip = 1} /^msgstr/ && skip == 1 {skip = 0; next} skip == 0 {print}' "$POT_FILE" >> "$OUTPUT_FILE" | |
| } | |
| # Function to generate .po files for all languages | |
| generate_po_files() { | |
| # Get the .pot file and filename | |
| get_pot_file_and_name | |
| # Get the list of language codes | |
| LANG_CODES=$(get_language_codes) | |
| # Get the custom module name | |
| MODULE_NAME=$(get_module_name) | |
| echo "Generating .po files for ${POT_FILE_NAME}..." | |
| # Generate the .po files for each language code | |
| for LANG_CODE in $LANG_CODES; do | |
| # Skip the "und" (undefined) language code | |
| if [ "$LANG_CODE" != "und" ]; then | |
| OUTPUT_FILE="${MODULE_PATH}/translations/${POT_FILE_NAME}-${LANG_CODE}.po" | |
| generate_po_file "$LANG_CODE" "$POT_FILE" "$OUTPUT_FILE" | |
| if [ "$VERBOSE" == "true" ]; then | |
| echo "Generated $OUTPUT_FILE" | |
| fi | |
| fi | |
| done | |
| } | |
| # Generate the .po files | |
| generate_po_files | |
| echo | |
| echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment