Skip to content

devstory-co-kr/flutter-translator

Repository files navigation

Flutter Translator

VSCode Badge
Supports internationalization of Flutter applications using Google Translate.

Table of Contents

Introduction

This is a extension created based on an environment using Flutter and Fastlane.

Features

Usage

Text Translate

  1. Select the text you want to translate.
  2. Run Flutter Translator: Text - Translate

ARB

  1. Configure the Flutter project localizations environment by referring to the documentation.
  2. Run Flutter Translator: ARB - Translate

Metadata

  1. (If metadata is not set) Run Flutter Translator: Metadata - Create
  2. Run Flutter Translator: Metadata - Translate
  3. The folder structure below is automatically created when the command is executed. (Based on fastlane upload_to_play_store & deliver)
    ├── android
    │    └── fastlane
    │        └── metadata
    │            └── android
    │                ├── en-US
    │                │   └── changelogs
    │                │       ├── full_description.txt
    │                │       ├── short_description.txt
    │                │       ├── title.txt
    │                │       └── video.txt
    │                └── ko-KR
    │                        ├── full_description.txt
    │                        ├── short_description.txt
    │                        ├── title.txt
    │                        └── video.txt
    └── ios
        └── fastlane
            └── metadata
                ├── en-US
                │   ├── description.txt
                │   ├── keywords.txt
                │   ├── marketing_url.txt
                │   ├── name.txt
                │   ├── privacy_url.txt
                │   ├── subtitle.txt
                │   └── support_url.txt
                └── ko
                    ├── description.txt
                    ├── keywords.txt
                    ├── marketing_url.txt
                    ├── name.txt
                    ├── privacy_url.txt
                    ├── subtitle.txt
                    └── support_url.txt
    

Changelog

  1. Run Flutter Translator: Metadata - Create and select languages you want to add.
  2. Run Flutter Translator: Changelog - Translate
  3. The folder structure below is automatically created when the command is executed. (Based on fastlane upload_to_play_store & deliver)
    ├── android
    │    └── fastlane
    │        └── metadata
    │            └── android
    │                ├── en-US
    │                │   └── changelogs
    │                │       ├── default.txt
    │                │       ├── 1.txt
    │                │       └── 2.txt
    │                └── ko-KR
    │                    └── changelogs
    │                        ├── default.txt
    │                        ├── 1.txt
    │                        └── 2.txt
    └── ios
        └── fastlane
            └── metadata
                ├── en-US
                │   └── release_notes.txt
                └── ko
                    └── release_notes.txt
    

Xcode Strings

  1. Refer to the link and complete localization settings in xcode and then add the strings file.
  2. Run Flutter Translator: Xcode Strings - Translate

Configuration

It is recommended to set the configuration in the project workspace(.vscode/settings.json).

{
  "flutterTranslator.config": {
    "arbConfig": {
      "sourcePath": "ABSOLUTE_PATH/intl_en.arb",
      "exclude": ["ko", "zh_CN", "fr"],
      "prefix": "intl_",
      "custom": {
        "zh_CN": "intl_zh_Hant"
      }
    }
    "googleAuthConfig": {
      "apiKey": "YOUR_GOOGLE_API_KEY",
      "credential": "YOUR_CREDENTIAL_JSON_FILE_ABSOLUTE_PATH",
    }
    "googleSheetConfig": {
      "id": "YOUR_GOOGLE_SHEET_ID",
      "name": "YOUR_GOOGLE_SHEET_NAME",
      "exclude": ["ko", "zh_CN", "fr"]
    },
    "metadataConfig": {
      "exclude": ["ko-KR", "zh-CN"]
    },
    "changelogConfig": {
      "exclude": ["ko-KR", "zh-CN"]
    },
    "xcodeConfig": {
      "projectLanguageCode": {
        "fil.lproj": "tl"
      }
    },
    "translationConfig": {
      "useCache": true,
      "exclude": ["BRAND_NAME", "APPLICATION_NAME"],
    }
  }
}
  • arbConfig
    • sourcePath : Absolute path to the source ARB file you want to translate.
    • exclude : List of ARB language codes that you do not want to translate.
    • prefix : Arb common string to prepend to file name. (e.g. intl_ : intl_ko.arb, intl_hi.arb, intl_fr.arb)
    • custom : You can customize the ARB file name for languageCode in the format {LanguageCode: CUSTOM_NAME} and arbFilePrefix is not applied.
  • googleAuthConfig
    • credential : Absolute path to JSON key files created after creating a service account on the Google Cloud console.
  • googleSheetConfig
    • id : You can find the spreadsheet ID in a Google Sheets URL: https://docs.google.com/spreadsheets/d/<GOOGLE_SHEET_ID>/edit#gid=0
    • name : Sheet name at the bottom of google sheet.
    • exclude : List of ARB language codes that you do not want to upload.
  • metadataConfig
  • changelogConfig
  • xcodeConfig
    • projectLanguageCode: Set the language to translate locale.lproj into. Enter the folder name ending with .lproj and the ARB language code name.
  • translationConfig
    • useCache : whether to use cache When translating.
    • exclude : List of keywords you do not want translated (ignoring case).

Translate

  • Translate with free Google Translate API.
  • Translation Rules
    • if the key contains @, it will not be translated.
    • If the key does not exist in the targetARB file, preceed with translation.
    • If the values retrieved from the history and sourceARB files using the key are different, it is determined that there has been a change and translation is performed.
    • If the value in the history file and the value in sourceARB are different, replaces the value of the entire targetARB with the translation result.
      • .vscode/flutter-translator/history.json : A history of the last translated sourceARB to track changes to the sourceARB file.
        {
          "data": {
            "@@locale": "en",
            "helloWorld": "hello world"
            "happyNewYear": "Happy new year",
          }
        }
        
    • Google Translator's results are stored in a cache file, and the cache is returned when the same request comes in.
      • You can turn the cache on and off using Translation - Use Cache command.
      • .vscode/flutter-translator/cache.json : This is a file that caches Google Translate results.
        {
          "source languageCode": {
            "target languageCode": {
              "SHA-1 of source value" : "translated text"
            }
          }
        }
        
  • Exclude Translation
    • If there are changes in the sourceARB file but you do not want to translate them, run the Flutter Translator: ARB - Exclude Translation command.
    • Overwrites the changed value of sourceARB with history so that the value is not translated.
    • However, if the key does not exist in the targetARB file, translation is performed.
  • It has a built-in algorithm that evaluates translation results and selects a better translation.
    • Translation evaluation parameters
      • ARB Parameter Count
      • Number of parentheses
      • Number of line breaks
      • Number of keywords excluded from translation

License

MIT License

Copyright (c) 2023 DevStory

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.