qsgen2/tools/migrate_messages_to_system.sh
Stig-Ørjan Smelror 91b0bbd112 feat(i18n): add Norwegian (nb_NO) translation and clean up
- Add complete Norwegian (nb_NO) language file
- Update .gitignore to exclude temporary and backup files
- Remove temporary and backup files from language directory
- Clean up scripts directory and add to .gitignore
- Update language file format to use key-value pairs
2025-05-18 19:01:39 +02:00

69 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env zsh
# Script to migrate hardcoded messages to the message system
set -e
# Configuration
SOURCE_DIR="$(dirname "$0")/.."
LANG_FILE="${SOURCE_DIR}/include/qsgen2/lang/qsgen2.en"
TEMP_FILE="${SOURCE_DIR}/.qsgen2.tmp"
# Create a backup of the original file
backup_file() {
local file="$1"
local backup="${file}.bak.$(date +%s)"
cp "$file" "$backup"
echo "Created backup at: $backup"
}
# Replace a message in the code
replace_message() {
local old_msg="$1"
local new_key="$2"
local file="$3"
# Escape special characters for sed
local escaped_old=$(printf '%s\n' "$old_msg" | sed 's/[&/\^$*.]/\\&/g')
local escaped_new="_msg i18n \"$new_key\""
# Handle messages with parameters
if [[ "$old_msg" == *"%s"* ]]; then
escaped_new="${escaped_new} \"\${1:-\"\"}\""
fi
# Replace in file
sed -i "s/_msg \(info\|warning\|error\|debug\|other\) \"$escaped_old\"/$escaped_new/g" "$file"
}
# Process a single file
process_file() {
local file="$1"
echo "Processing file: $file"
# Create a backup
backup_file "$file"
# Replace messages
while IFS= read -r line; do
if [[ "$line" =~ ^([a-z.]+)[[:space:]]*=[[:space:]]*"([^"]+)" ]]; then
local key="${match[1]}"
local msg="${match[2]}"
# Skip comments and empty lines
[[ "$key" == "#"* ]] && continue
[[ -z "$key" ]] && continue
# Replace in file
replace_message "$msg" "$key" "$file"
fi
done < "$LANG_FILE"
}
# Find all shell scripts in the project
find "$SOURCE_DIR" -type f \( -name "*.zsh" -o -name "*.sh" \) -not -path "*/.*" | while read -r file; do
process_file "$file"
done
echo "Migration complete. Please review the changes and test the application."