Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): Add a script to push localization resource file to Crowdin #27230

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ios/brave-ios/App/l10n/tools/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# How to contribute translations
# How to push strings to Crowdin
Once your english strings are all up to date. These changes can be pushed to Crowdin for translation by running the following from Terminal:

```
cd l10n/
TOKEN=<token> ./push-strings-to-crowdin.sh
```

If there any issues pushing strings to Transifex then these issues are logged to ```output.log```

### The API token can be created from Crowdin Dev Portal

# How to contribute translations via Transifex

There are several things you can do to help us internationalize Brave and provide a great experience for everybody

Expand Down Expand Up @@ -91,4 +103,5 @@ If there any issues pulling translations into the Xcode project then these issue

* When importing translations the ```xcodebuild``` command-line tool:
* does __not__ allow us to specify where localizations are imported into the project folder structure
* adds duplicate .strings entries to the ```Copy Bundle Resources``` section of ```Build Phases```, which need to be manually removed
* adds duplicate .strings entries to the ```Copy Bundle Resources``` section of ```Build Phases```, which need to be manually removed

171 changes: 171 additions & 0 deletions ios/brave-ios/App/l10n/tools/push-strings-to-crowdin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/bin/bash
# Copyright (c) 2025 The Brave Authors. All rights reserved.
# 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 https://mozilla.org/MPL/2.0/.

# Push string changes to Transifex
#
# Error Codes:
# 0 = Success
# 1 = API Token must be set
# 2 = Unauthorized access
# 3 = Failed to export strings from Xcode project
# 4 = Failed to cleanup strings
# 5 = Failed to push string changes to Crowdin

report_error()
{
echo $2
echo $2 >>output.log 2>&1
cleanup
exit $1
}

cleanup()
{
if [ -e transifex.log ] ; then
cat transifex.log >> output.log
echo >> output.log
rm transifex.log
fi

if [ -e ../../Client/en.xcloc ]; then
rm -R ../../Client/en.xcloc
fi

if [ -e en.xliff ] ; then
rm en.xliff
fi
}

if [ "${TOKEN}" = "" ] ; then
report_error 1 "TOKEN environment variable must be set to \"api\""
fi

cd $(dirname "$0")

echo "Exporting strings from Xcode project..."
(cd ../../ && xcodebuild -exportLocalizations SWIFT_EMIT_LOC_STRINGS=NO) >>output.log 2>&1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only ever push up the en.xliff why dont we just only export english here? (-exportLanguage en)

if [ $? != 0 ] ; then
report_error 4 "ERROR: Failed to export strings from Xcode project, please see output.log"
fi

mv -f ../../Client/en.xcloc/Localized\ Contents/en.xliff . >>output.log 2>&1

if [ $? != 0 ] ; then
report_error 3 "ERROR: Failed to export strings from Xcode project, please see output.log"
fi

echo "Cleaning up strings..."
./xliff-cleanup.py en.xliff
if [ $? != 0 ] ; then
report_error 4 "ERROR: Failed to cleanup strings, please see output.log"
fi

sed -i '' 's/Shared\/Supporting Files/brave/' en.xliff >>output.log 2>&1
if [ $? != 0 ] ; then
report_error 4 "ERROR: Failed to cleanup strings, please see output.log"
fi

crowdin_project_id=33
crowdin_storage_id=null

add_storage_id()
{
FILE_PATH="en.xliff"
result=$(curl --silent \
-X "POST" "https://brave-software.crowdin.com/api/v2/storages" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/octet-stream' \
-H "Crowdin-API-FileName: en.xliff" \
--data-binary @"$FILE_PATH"
)
storage_id=$(echo "$result" | jq '.data.id')
echo $storage_id
}

delete_storage_id()
{
http_status_code=$(curl --silent -o /dev/null -w "%{http_code}" \
-X "DELETE" "https://brave-software.crowdin.com/api/v2/storages/$1" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json"
)
echo $http_status_code
}

add_file()
{
http_status_code=$(curl --silent -o /dev/null -w "%{http_code}" \
-X "POST" "https://brave-software.crowdin.com/api/v2/projects/$crowdin_project_id/files" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
--data @- << EOF
{
"storageId": $crowdin_storage_id,
"name": "en.xliff",
"type": "xliff"
}
EOF
)
echo $http_status_code
}

delete_file()
{
http_status_code=$(curl --silent -o /dev/null -w "%{http_code}" \
-X "DELETE" "https://brave-software.crowdin.com/api/v2/projects/$crowdin_project_id/files/$1" \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
)
echo $http_status_code
}

echo "Creating a storage id in Crowdin..."
crowdin_storage_id=$(add_storage_id)
if [ $crowdin_storage_id == null ]
then
report_error 5 "ERROR: Failed to create a storage id in Crowdin"
else
echo "Crowdin Storage ID: $crowdin_storage_id"
fi

echo "Checking if there is already a file id for our project in Crowdin..."
file_result=$(curl --silent \
-X "GET" "https://brave-software.crowdin.com/api/v2/projects/$crowdin_project_id/files" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json"
)
file_id=$(echo "$file_result" | jq '.data.[0].data.id')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think jq is a standard bash utility even on macOS, may want to add its requirement to the README

if [ $file_id == null ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this checking for the string literal "null"?

then
echo "No, there is no file. Adding a new file..."
add_file_http_code=$(add_file)
if [ $add_file_http_code != 201 ]
then
report_error 5 "ERROR: Failed to add a file to the project in Crowdin"
else
echo "The new source file has been added to the project in Crowdin"
fi
else
echo "Yes, there is a file id: $file_id. Deleting this file ..."
delete_file_http_code=$(delete_file $file_id)
if [ $delete_file_http_code != 204 ]
then
report_error 5 "ERROR: Failed to delete the existed file in Crowdin"
else
echo "Now, adding a new file ..."
add_file_http_code=$(add_file)
if [ $add_file_http_code != 201 ]
then
report_error 5 "ERROR: Failed to add a file to the project in Crowdin"
else
echo "The new source file has added to the project in Crowdin"
fi
fi
fi

cleanup

echo "Successfully pushed string changes to Crowdin"
Loading