From 03531c495b70bd66b8298c1baeca3c1913ecf8a1 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Thu, 2 Mar 2017 14:08:45 +0100 Subject: [PATCH] Add gradle tasks for l10n tasks. (#140) (Eventually this could be refactored to be a gradle plugin that we can use for future apps) --- .android2po | 2 +- .gitignore | 2 +- README.md | 36 +++++++++----- app/build.gradle | 82 ++++++++++++++++++++++++++++++++ tools/l10n/create_commits.sh | 25 ++++++++++ tools/l10n/fix_locale_folders.sh | 34 +++++++++++++ 6 files changed, 166 insertions(+), 15 deletions(-) create mode 100755 tools/l10n/create_commits.sh create mode 100755 tools/l10n/fix_locale_folders.sh diff --git a/.android2po b/.android2po index b7b565b4905..b9affb601c8 100644 --- a/.android2po +++ b/.android2po @@ -6,7 +6,7 @@ # That's where we store our exported po files. This directory is added to .gitignore and should not be # commited to the Focus repository. Usually this is a checkout of the l10n repository: # https://github.com/mozilla-l10n/focus-android-l10n ---gettext l10n/ +--gettext l10n-repo/ # This is following the layout that Pontoon requires: # https://developer.mozilla.org/en-US/docs/Mozilla/Implementing_Pontoon_in_a_Mozilla_website diff --git a/.gitignore b/.gitignore index c93080895c8..b0cc6e44ef0 100644 --- a/.gitignore +++ b/.gitignore @@ -40,5 +40,5 @@ captures/ *.jks # Local checkout of localization files -l10n/ +l10n-repo/ diff --git a/README.md b/README.md index 5571f8828ed..f07719ee88e 100644 --- a/README.md +++ b/README.md @@ -43,39 +43,49 @@ Focus for Android is getting localized on [Pontoon](https://pontoon.mozilla.org/ easy_install android2po ``` -1. Create a local checkout of the l10n repository into a folder called l10n: +1. Run the `stringsSetup` gradle tasks to create a local checkout of the L10N repository (in folder l10n-repo): ```shell - git clone https://github.com/mozilla-l10n/focus-android-l10n.git l10n + ./gradlew stringsSetup ``` + ### Export strings for translation -1. Run the export command of android2po to generate a new template: +1. Fetch the latest changes from the L10N repository and remove all local modifications with the `stringsCleanUpdate` gradle task: + + ```shell + ./gradlew stringsCleanUpdate + ``` + +1. Run the `stringsExport` gradle task to update the template and existing translations: ```shell - a2po export + ./gradlew stringsExport ``` -1. Go to the l10n folder, commit and push the updated template to the l10n repository. +1. Create separate commits for every locale using the `stringsCommit` gradle task: + ```shell + ./gradlew stringsCommit + ``` + +1. Go to the l10n-repo folder, verify the changes and push them to the L10N repository. ### Import translated strings -1. Go to the l10n folder and pull the latest changes: +1. Fetch the latest changes from the L10N repository and remove all local modifications with the `stringsCleanUpdate` gradle task: ```shell - cd l10n - git pull -r + ./gradlew stringsCleanUpdate ``` -1. Go back to the root folder and run the import command of android2po +1. Run the `stringsImport` gradle task to generate the Android XML files. ```shell - cd .. - a2po import + ./gradlew stringsImport ``` - -1. Commit and push the updated XML files to the app repository. + +1. Verify the changes and then commit and push the updated XML files to the app repository. diff --git a/app/build.gradle b/app/build.gradle index 30f60ae67c2..f4b0d66634a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -103,3 +103,85 @@ tasks.whenTaskAdded { task -> task.dependsOn buildBlocklists } } + + + +// ------------------------------------------------------------------------------------------------- +// L10N: Initialize Strings +// ------------------------------------------------------------------------------------------------- + +task stringsSetup(type:Exec) { + group = 'Localization' + description = 'Setup L10N repository for importing and exporting strings.' + + workingDir '..' + + commandLine 'git', 'clone', 'https://github.com/mozilla-l10n/focus-android-l10n.git', 'l10n-repo' +} + +// ------------------------------------------------------------------------------------------------- +// L10N: Export Strings +// ------------------------------------------------------------------------------------------------- + +task stringsExport(type:Exec) { + group = 'Localization' + description = 'Export strings to L10N repository.' + + workingDir '..' + + commandLine 'a2po', 'export' +} + +// ------------------------------------------------------------------------------------------------- +// L10N: Import Strings +// ------------------------------------------------------------------------------------------------- + +task stringsImport { + group = 'Localization' + description = 'Import strings from L10N repository.' + + doLast { + exec { + workingDir '..' + commandLine 'a2po', 'import' + } + exec { + workingDir '../tools/l10n/' + commandLine 'sh', 'fix_locale_folders.sh' + } + } +} + +// ------------------------------------------------------------------------------------------------- +// L10N: Create commits +// ------------------------------------------------------------------------------------------------- + +task stringsCommit(type:Exec) { + group = 'Localization' + description = 'Create commits for exported strings.' + + workingDir '../tools/l10n/' + + commandLine 'sh', 'create_commits.sh' +} + +// ------------------------------------------------------------------------------------------------- +// L10N: Clean and update +// ------------------------------------------------------------------------------------------------- + +task stringsCleanUpdate() { + group = 'Localization' + description = 'Fetch L10N changes and remove all local modifications.' + + doLast { + exec { + workingDir '../l10n-repo/' + commandLine 'git', 'fetch', 'origin' + + } + exec { + workingDir '../l10n-repo/' + commandLine 'git', 'reset', '--hard', 'origin/master' + } + } +} diff --git a/tools/l10n/create_commits.sh b/tools/l10n/create_commits.sh new file mode 100755 index 00000000000..521debec4fe --- /dev/null +++ b/tools/l10n/create_commits.sh @@ -0,0 +1,25 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# Create a separate commit for every locale. + +parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) +cd "$parent_path/../../l10n-repo" + +git add locales/templates/app.pot +git commit -m "template update: app.pot" + +cd locales + +locale_list=$(find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \) | sed 's|^\./||g' | sort) +for locale in ${locale_list}; +do + # Exclude templates + if [ "${locale}" != "templates" ] + then + git add ${locale}/app.po + git commit -m "${locale}: Update app.po" + fi +done + diff --git a/tools/l10n/fix_locale_folders.sh b/tools/l10n/fix_locale_folders.sh new file mode 100755 index 00000000000..0fbd8116352 --- /dev/null +++ b/tools/l10n/fix_locale_folders.sh @@ -0,0 +1,34 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# a2po has problems with the folder format of Pontoon and creates resource folders +# like values-es-MX. Android expects values-es-rMX. This script tries to find those +# folders and fixes them. + +parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) + +cd "$parent_path/../../app/src/main/res/" + + +folder_list=$(find . -maxdepth 1 -type d -iname "values-*-*") +for folder in ${folder_list}; +do + country=$(echo ${folder} | cut -d'-' -f3) + len=${#country} + + if [ "$len" -eq "2" ]; then + prefix=$(echo ${folder} | cut -d'-' -f1,2) + + fixed_folder="${prefix}-r${country}" + + echo "Fixing ${folder} -> ${fixed_folder}" + + if [ -f $fixed_folder ] ; then + rm -rf "${fixed_folder}" + fi + + mv "$folder" "${fixed_folder}" + fi +done +