dev: 18n-push - added ability to push a single language

This commit is contained in:
Lemmy
2026-01-19 15:15:50 -05:00
parent c15db06d58
commit 1a21a465c5
+45 -18
View File
@@ -1,22 +1,33 @@
#!/bin/bash
# Push translations to Noctalia Translate API
# Usage: TRANSLATION_PUSH_SECRET=your_secret ./push-translations.sh [--overwrite] [/path/to/Assets/Translations]
# Usage: TRANSLATION_PUSH_SECRET=your_secret ./push-translations.sh [--overwrite] [--lang <code>] [/path/to/Assets/Translations]
# Or set the secret in environment and pass the path as argument
#
# Options:
# --overwrite Overwrite existing translations
# --lang <code> Upload only a single language (e.g., --lang fr)
set -e
# Parse arguments
OVERWRITE=false
TRANSLATIONS_DIR="Assets/Translations"
SINGLE_LANG=""
for arg in "$@"; do
case $arg in
while [[ $# -gt 0 ]]; do
case $1 in
--overwrite)
OVERWRITE=true
shift
;;
--lang)
SINGLE_LANG="$2"
shift 2
;;
*)
TRANSLATIONS_DIR="$arg"
TRANSLATIONS_DIR="$1"
shift
;;
esac
done
@@ -50,18 +61,30 @@ echo "Target: $API_URL/api/projects/$PROJECT_SLUG/push"
# Build combined JSON object
COMBINED_JSON="{}"
for file in "$TRANSLATIONS_DIR"/*.json; do
if [ -f "$file" ]; then
# Extract locale from filename (e.g., en.json -> en)
filename=$(basename "$file")
locale="${filename%.json}"
echo " Loading: $locale ($filename)"
# Add this locale's translations to the combined object
COMBINED_JSON=$(echo "$COMBINED_JSON" | jq --arg locale "$locale" --slurpfile content "$file" '. + {($locale): $content[0]}')
if [ -n "$SINGLE_LANG" ]; then
# Single language mode
file="$TRANSLATIONS_DIR/$SINGLE_LANG.json"
if [ ! -f "$file" ]; then
echo "Error: Language file not found: $file"
exit 1
fi
done
echo " Loading: $SINGLE_LANG ($SINGLE_LANG.json)"
COMBINED_JSON=$(echo "$COMBINED_JSON" | jq --arg locale "$SINGLE_LANG" --slurpfile content "$file" '. + {($locale): $content[0]}')
else
# All languages mode
for file in "$TRANSLATIONS_DIR"/*.json; do
if [ -f "$file" ]; then
# Extract locale from filename (e.g., en.json -> en)
filename=$(basename "$file")
locale="${filename%.json}"
echo " Loading: $locale ($filename)"
# Add this locale's translations to the combined object
COMBINED_JSON=$(echo "$COMBINED_JSON" | jq --arg locale "$locale" --slurpfile content "$file" '. + {($locale): $content[0]}')
fi
done
fi
# Count locales
LOCALE_COUNT=$(echo "$COMBINED_JSON" | jq 'keys | length')
@@ -72,15 +95,19 @@ if [ "$LOCALE_COUNT" -eq 0 ]; then
exit 1
fi
# Check if English exists
if ! echo "$COMBINED_JSON" | jq -e '.en' > /dev/null 2>&1; then
# Check if English exists (only required when pushing all languages)
if [ -z "$SINGLE_LANG" ] && ! echo "$COMBINED_JSON" | jq -e '.en' > /dev/null 2>&1; then
echo "Error: English (en.json) is required"
exit 1
fi
# Confirmation
echo ""
read -p "Push $LOCALE_COUNT locale(s) to $API_URL? [y/N] " -n 1 -r
if [ -n "$SINGLE_LANG" ]; then
read -p "Push '$SINGLE_LANG' to $API_URL? [y/N] " -n 1 -r
else
read -p "Push $LOCALE_COUNT locale(s) to $API_URL? [y/N] " -n 1 -r
fi
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted."