From 9fe372f58f4eab6da39c103ca7476e3622836a32 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 15 Jan 2023 12:07:51 -0500 Subject: [PATCH 1/2] Match target translation to site's locale --- README.md | 4 +-- libretranslate/app.py | 44 ++++++++++++++++++++++---------- libretranslate/default_values.py | 4 +-- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b512ea7..51f7352 100644 --- a/README.md +++ b/README.md @@ -183,8 +183,8 @@ docker-compose -f docker-compose.cuda.yml up -d --build | --ga-id | Enable Google Analytics on the API client page by providing an ID | `No tracking` | LT_GA_ID | | --debug | Enable debug environment | `False` | LT_DEBUG | | --ssl | Whether to enable SSL | `False` | LT_SSL | -| --frontend-language-source | Set frontend default language - source | `en` | LT_FRONTEND_LANGUAGE_SOURCE | -| --frontend-language-target | Set frontend default language - target | `es` | LT_FRONTEND_LANGUAGE_TARGET | +| --frontend-language-source | Set frontend default language - source | `auto` | LT_FRONTEND_LANGUAGE_SOURCE | +| --frontend-language-target | Set frontend default language - target | `locale` (match site's locale) | LT_FRONTEND_LANGUAGE_TARGET | | --frontend-timeout | Set frontend translation timeout | `500` | LT_FRONTEND_TIMEOUT | | --api-keys | Enable API keys database for per-user rate limits lookup | `Don't use API keys` | LT_API_KEYS | | --api-keys-db-path | Use a specific path inside the container for the local database. Can be absolute or relative | `db/api_keys.db` | LT_API_KEYS_DB_PATH | diff --git a/libretranslate/app.py b/libretranslate/app.py index 662351d..b29cce5 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -132,10 +132,33 @@ def create_app(args): iter([l for l in languages if l.code == args.frontend_language_source]), None, ) + if frontend_argos_language_source is None: + frontend_argos_language_source = languages[0] - frontend_argos_language_target = next( - iter([l for l in languages if l.code == args.frontend_language_target]), None - ) + + if len(languages) >= 2: + language_target_fallback = languages[1] + else: + language_target_fallback = languages[0] + + if args.frontend_language_target == "locale": + def resolve_language_locale(): + loc = get_locale() + language_target = next( + iter([l for l in languages if l.code == loc]), None + ) + if language_target is None: + language_target = language_target_fallback + return language_target + + frontend_argos_language_target = resolve_language_locale + else: + language_target = next( + iter([l for l in languages if l.code == args.frontend_language_target]), None + ) + if language_target is None: + language_target = language_target_fallback + frontend_argos_language_target = lambda: language_target frontend_argos_supported_files_format = [] @@ -143,15 +166,6 @@ def create_app(args): for ff in file_format.supported_file_extensions: frontend_argos_supported_files_format.append(ff) - # Raise AttributeError to prevent app startup if user input is not valid. - if frontend_argos_language_source is None: - frontend_argos_language_source = languages[0] - if frontend_argos_language_target is None: - if len(languages) >= 2: - frontend_argos_language_target = languages[1] - else: - frontend_argos_language_target = languages[0] - api_keys_db = None if args.req_limit > 0 or args.api_keys or args.daily_req_limit > 0: @@ -896,6 +910,8 @@ def create_app(args): type: string description: Human-readable language name (in English) """ + target_lang = frontend_argos_language_target() + return jsonify( { "charLimit": args.char_limit, @@ -911,8 +927,8 @@ def create_app(args): "name": _lazy(frontend_argos_language_source.name), }, "target": { - "code": frontend_argos_language_target.code, - "name": _lazy(frontend_argos_language_target.name), + "code": target_lang.code, + "name": _lazy(target_lang.name), }, }, } diff --git a/libretranslate/default_values.py b/libretranslate/default_values.py index c066cbb..e3e9113 100644 --- a/libretranslate/default_values.py +++ b/libretranslate/default_values.py @@ -93,12 +93,12 @@ _default_options_objects = [ }, { 'name': 'FRONTEND_LANGUAGE_SOURCE', - 'default_value': 'en', + 'default_value': 'auto', 'value_type': 'str' }, { 'name': 'FRONTEND_LANGUAGE_TARGET', - 'default_value': 'es', + 'default_value': 'locale', 'value_type': 'str' }, { From 573b449c81ee8f78bedaf62946cf345f02931817 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 15 Jan 2023 12:14:04 -0500 Subject: [PATCH 2/2] Fix build --- .github/workflows/publish-docker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml index 3fcc54e..d4501a2 100644 --- a/.github/workflows/publish-docker.yml +++ b/.github/workflows/publish-docker.yml @@ -42,6 +42,7 @@ jobs: - name: Build and push Image uses: docker/build-push-action@v2 with: + file: ./docker/Dockerfile platforms: linux/amd64,linux/arm64 tags: | ${{ steps.get-variables.outputs.gh-username-lower }}/libretranslate:${{ env.TAG }},