mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Consolidate translation script (#33911)
* Consolidate translation script * Remove commented code
This commit is contained in:
parent
9535dd87b0
commit
425c97626a
@ -54,9 +54,8 @@ jobs:
|
|||||||
- template: templates/azp-step-git-init.yaml@azure
|
- template: templates/azp-step-git-init.yaml@azure
|
||||||
- script: |
|
- script: |
|
||||||
export LOKALISE_TOKEN="$(lokaliseToken)"
|
export LOKALISE_TOKEN="$(lokaliseToken)"
|
||||||
export AZURE_BRANCH="$(Build.SourceBranchName)"
|
|
||||||
|
|
||||||
./script/translations_download
|
python3 -m script.translations download
|
||||||
displayName: 'Download Translation'
|
displayName: 'Download Translation'
|
||||||
- script: |
|
- script: |
|
||||||
git checkout dev
|
git checkout dev
|
||||||
|
1
script/translations/__init__.py
Normal file
1
script/translations/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
"""Translation helper scripts."""
|
40
script/translations/__main__.py
Normal file
40
script/translations/__main__.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
"""Validate manifests."""
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from . import download, error
|
||||||
|
|
||||||
|
|
||||||
|
def get_arguments() -> argparse.Namespace:
|
||||||
|
"""Get parsed passed in arguments."""
|
||||||
|
parser = argparse.ArgumentParser(description="Home Assistant Scaffolder")
|
||||||
|
parser.add_argument("action", type=str, choices=["download"])
|
||||||
|
parser.add_argument("--debug", action="store_true", help="Enable log output")
|
||||||
|
|
||||||
|
arguments = parser.parse_args()
|
||||||
|
|
||||||
|
return arguments
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Run a translation script."""
|
||||||
|
if not Path("requirements_all.txt").is_file():
|
||||||
|
print("Run from project root")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
args = get_arguments()
|
||||||
|
|
||||||
|
if args.action == "download":
|
||||||
|
download.run(args)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
sys.exit(main())
|
||||||
|
except error.ExitApp as err:
|
||||||
|
print()
|
||||||
|
print(f"Fatal Error: {err.reason}")
|
||||||
|
sys.exit(err.exit_code)
|
4
script/translations/const.py
Normal file
4
script/translations/const.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
"""Translation constants."""
|
||||||
|
|
||||||
|
PROJECT_ID = "130246255a974bd3b5e8a1.51616605"
|
||||||
|
DOCKER_IMAGE = "b8329d20280263cad04f65b843e54b9e8e6909a348a678eac959550b5ef5c75f"
|
@ -3,10 +3,51 @@
|
|||||||
import glob
|
import glob
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
from typing import Dict, List, Union
|
from typing import Dict, List, Union
|
||||||
|
|
||||||
|
from .const import DOCKER_IMAGE, PROJECT_ID
|
||||||
|
from .error import ExitApp
|
||||||
|
from .util import get_lokalise_token
|
||||||
|
|
||||||
FILENAME_FORMAT = re.compile(r"strings\.(?P<suffix>\w+)\.json")
|
FILENAME_FORMAT = re.compile(r"strings\.(?P<suffix>\w+)\.json")
|
||||||
|
LOCAL_DIR = pathlib.Path("build/translations-download").absolute()
|
||||||
|
|
||||||
|
|
||||||
|
def run_download_docker(args):
|
||||||
|
"""Run the Docker image to download the translations."""
|
||||||
|
pipe_null = {} if args.debug else {"stdout": subprocess.DEVNULL}
|
||||||
|
|
||||||
|
print("Running Docker to download latest translations.")
|
||||||
|
run = subprocess.run(
|
||||||
|
[
|
||||||
|
"docker",
|
||||||
|
"run",
|
||||||
|
"-v",
|
||||||
|
f"{LOCAL_DIR}:/opt/dest/locale",
|
||||||
|
"--rm",
|
||||||
|
f"lokalise/lokalise-cli@sha256:{DOCKER_IMAGE}",
|
||||||
|
# Lokalise command
|
||||||
|
"lokalise",
|
||||||
|
"export",
|
||||||
|
PROJECT_ID,
|
||||||
|
"--token",
|
||||||
|
get_lokalise_token(),
|
||||||
|
"--export_empty",
|
||||||
|
"skip",
|
||||||
|
"--type",
|
||||||
|
"json",
|
||||||
|
"--unzip_to",
|
||||||
|
"/opt/dest",
|
||||||
|
],
|
||||||
|
**pipe_null,
|
||||||
|
)
|
||||||
|
print()
|
||||||
|
|
||||||
|
if run.returncode != 0:
|
||||||
|
raise ExitApp("Failed to download translations")
|
||||||
|
|
||||||
|
|
||||||
def load_json(filename: str) -> Union[List, Dict]:
|
def load_json(filename: str) -> Union[List, Dict]:
|
||||||
@ -95,18 +136,14 @@ def save_language_translations(lang, translations):
|
|||||||
save_json(path, platform_translations)
|
save_json(path, platform_translations)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def run(args):
|
||||||
"""Run the script."""
|
"""Run the script."""
|
||||||
if not os.path.isfile("requirements_all.txt"):
|
LOCAL_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
print("Run this from HA root dir")
|
|
||||||
return
|
run_download_docker(args)
|
||||||
|
|
||||||
paths = glob.iglob("build/translations-download/*.json")
|
paths = glob.iglob("build/translations-download/*.json")
|
||||||
for path in paths:
|
for path in paths:
|
||||||
lang = get_language(path)
|
lang = get_language(path)
|
||||||
translations = load_json(path)
|
translations = load_json(path)
|
||||||
save_language_translations(lang, translations)
|
save_language_translations(lang, translations)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
10
script/translations/error.py
Normal file
10
script/translations/error.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
"""Errors for translations."""
|
||||||
|
|
||||||
|
|
||||||
|
class ExitApp(Exception):
|
||||||
|
"""Exception to indicate app should exit."""
|
||||||
|
|
||||||
|
def __init__(self, reason, exit_code=1):
|
||||||
|
"""Initialize the exit app exception."""
|
||||||
|
self.reason = reason
|
||||||
|
self.exit_code = exit_code
|
22
script/translations/util.py
Normal file
22
script/translations/util.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"""Translation utils."""
|
||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
from .error import ExitApp
|
||||||
|
|
||||||
|
|
||||||
|
def get_lokalise_token():
|
||||||
|
"""Get lokalise token."""
|
||||||
|
token = os.environ.get("LOKALISE_TOKEN")
|
||||||
|
|
||||||
|
if token is not None:
|
||||||
|
return token
|
||||||
|
|
||||||
|
token_file = pathlib.Path(".lokalise_token")
|
||||||
|
|
||||||
|
if not token_file.is_file():
|
||||||
|
raise ExitApp(
|
||||||
|
"Lokalise token not found in env LOKALISE_TOKEN or file .lokalise_token"
|
||||||
|
)
|
||||||
|
|
||||||
|
return token_file.read_text().strip()
|
@ -1,39 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# Safe bash settings
|
|
||||||
# -e Exit on command fail
|
|
||||||
# -u Exit on unset variable
|
|
||||||
# -o pipefail Exit if piped command has error code
|
|
||||||
set -eu -o pipefail
|
|
||||||
|
|
||||||
cd "$(dirname "$0")/.."
|
|
||||||
|
|
||||||
if [ -z "${LOKALISE_TOKEN-}" ] && [ ! -f .lokalise_token ] ; then
|
|
||||||
echo "Lokalise API token is required to download the latest set of" \
|
|
||||||
"translations. Please create an account by using the following link:" \
|
|
||||||
"https://lokalise.co/signup/130246255a974bd3b5e8a1.51616605/all/" \
|
|
||||||
"Place your token in a new file \".lokalise_token\" in the repo" \
|
|
||||||
"root directory."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Load token from file if not already in the environment
|
|
||||||
[ -z "${LOKALISE_TOKEN-}" ] && LOKALISE_TOKEN="$(<.lokalise_token)"
|
|
||||||
|
|
||||||
PROJECT_ID="130246255a974bd3b5e8a1.51616605"
|
|
||||||
LOCAL_DIR="$(pwd)/build/translations-download"
|
|
||||||
FILE_FORMAT=json
|
|
||||||
|
|
||||||
mkdir -p ${LOCAL_DIR}
|
|
||||||
|
|
||||||
docker run \
|
|
||||||
-v ${LOCAL_DIR}:/opt/dest/locale \
|
|
||||||
--rm \
|
|
||||||
lokalise/lokalise-cli@sha256:b8329d20280263cad04f65b843e54b9e8e6909a348a678eac959550b5ef5c75f lokalise \
|
|
||||||
--token ${LOKALISE_TOKEN} \
|
|
||||||
export ${PROJECT_ID} \
|
|
||||||
--export_empty skip \
|
|
||||||
--type json \
|
|
||||||
--unzip_to /opt/dest
|
|
||||||
|
|
||||||
script/translations_download_split.py
|
|
Loading…
x
Reference in New Issue
Block a user