Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter translation sheet generator [fts]

command line application to make your l10n super fast. compose your strings in yaml/json format and use googlesheet for auto translate.

wiki and setup

follow the wiki pages for detailed instructions on the usage and project examples.

�� install:

you need to have flutter or dart sdk in your system path.

flutter pub global activate flutter_translation_sheet

now just run fts in any folder to create a template configuration file.

check --help on any sub-command of fts:

  • fts run
  • fts fetch
  • fts extract
  • fts upgrade
  • fts --version

⚙️ usage:

go with your terminal in any folder (or flutter project folder), and run fts run.

first time will create a template for you, and you will have to get your google credentials json.

once you get the json, go to trconfig.yaml and in the gsheet: there are two ways to fill the credentials (you only need to use one):

  1. add credentials_path: followed by the path of your json. you can copy the json file to the root folder of your project. the path can be absolute or relative.

example:

gsheets:
  credentials_path: c:/my_project/credentials.json or ./credentials.json

⚠️ note to windows users: paths should be either in the form “c:\users\etc”, with two backslash characters, or using forwardslash characters instead such as “c:/users/etc”.

  1. add credentials: followed by the whole credentials json content

example:

gsheets:
  credentials: {
    "type": "service_account",
    "project_id": "project-id",
    "private_key_id": "",
    "private_key": "-----begin private key-----nmiievaibadanbgkqhkig9w0baqefaascb-----end private key-----n",
    "client_email": "[email protected]",
    "client_id": "123456",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gsheets%40evolution-cp-calculator.iam.gserviceaccount.com"
  }

you can find more information in the comments in trconfig.yaml and fill the gsheet: section, and change the output folder as needed.

once you have your configuration file ready, run fts to generate your sample google sheets.

take the sample data input as reference, and use it in your own project.

fts will try to keep the local input and the remote sheet in sync, and automatically generate the locales for you every time you run it.

you can leave a terminal open and use the run command while listening for file changes in your master strings folder, or your trconfig.yaml:
fts run --watch.
you can exit the watch with q and then press enter.

⚠️ warning:

watch out how often you modify the files and save them. remember that your google service account has usage limits.

after a while of not using it, google sheet performance slow down on every request, so it might take a little longer to get the output generated.
once it warms up (run 1 time) the sync performance is pretty solid.

fts fetch

unlike fts run, fetch doesn’t sync, nor validates the data structure.

uses the local strings as entry map, downloads the latest data from googlesheet and generates the files accordingly.
is a much faster process. very useful when you made manual corrections in your sheets for the auto-translated locales.

do not manually modify the master language column on your google sheet, change the data in the string source file
and let fts do the upload.

if there are differences of master lang strings between local and remote, the entire row will be cleared and regenerated with auto translation
using the latest strings, and manual changes will get lost.

currently you have to be careful, and keep your manual translations backed up just in case you modify the master language string.

variables:

to store “variables” or placeholders in your strings to be replaced later in your code, use the follow notation:

"welcome back {{user}}, today is {{date}}."

it will store the values in the sheet as {{0}} {{1}} and so on, to avoid complications with googletranslate (although in rare cases gtranslate will truncate the {{}} somehow, don’t worry), and it will
generate a vars.lock file in the directory where you point your “entry_file” in config.

so you can define your own pattern for the code/json generation:

## pattern to applies final variables in the generated json/dart strings.
## enclose * in the pattern you need.
## {*} = {{name}} becomes {name}
## %* = {{name}} becomes %name
## (*) = {{name}} becomes (name)
## - special case when you need * as prefix or suffix, use *? as splitter
## ***?** = {{name}} becomes **name**
param_output_pattern: "{{*}}"

⚠️ warning:

do not confuse the data source placeholder format with param_output_pattern configuration.
data-source (your yaml strings) must have this form {{variable}} to be interpreted as variables.
the generated output strings uses param_output_pattern configuration to render the variables as you please.

utilities:

  • fts extract [--path] [--output]: this tiny utility command performs a shallow search (no syslinks) of your dart classes and uses a basic pattern matching to capture your code’s strings.
    might come in handy when you wanna localize an app with hardcoded texts. it only process ‘.dart’ files, and the string matching isn’t very permissive (single words strings are skipped).
    pass the folder to analyse in --path and the folder (or json file) path to save in --output.
    it will output a single json file cloning the structure of the source code folder tree for easy manual search.
    it’s up to you to clean it up, adjust keys, split it up in other data source files, and use it with “flutter translate sheet”.
    this command tool is in alpha state, but don’t worry, as it doesn’t touch any of analyzed files, so is safe.

it captures interpolated strings in your dart code, variables like $name, or ${obj.friends.length}, and use them as placeholders:
$name becomes {{name}} and ${obj.friends.length} becomes {{length}}.

  • new options added: [-s] captures all strings (event without spaces),
    and [-e] allows you to define a comma separated list of file extensions to search for strings, like -e dart,java,kt,arb

also… when you specify an –output that ends with .yaml, you will have a pretty cool template to plug into fts run 🙂

  • if you run the cli on macos, fts keeps your ios app bundle synced automatically with the locales! one thing less to worry about.

arb and intl:

we provide an experimental support for arb generation. in trconfig.yaml just set:

output_arb_template: lib/l10n/app_*.arb

fts has support for .arb readable metadata:

  today: "today is {{date}}, and is hot."
  "@today":
    description: show today's message with temperature.
    placeholders:
      date:
        type: datetime
        format: ymmmed

as a shortcut, placeholders supports the type and format in arb generation:

today: "today is {{date:datetime:ymmmed()}}, and is hot."

for plurals, we have a custom way of writing the dictionary. just use plural:variablename: so fts knows how to generate the string.
remember that other is mandatory (the default value) when you use plurals.

raw way of adding the metadata:

  ### not required, but you will be a much cooler dev if you provide context :)
  "@messagecount":
    {
      "description": "new messages count on the home screen",
      "placeholders": { "count": {} },
    }

  messagecount:
    plural:count:
      =0: no new messages
      =1: you have 1 new message
      =2: you have a couple of messages
      other: you have {{count}} new messages

previous yaml will output :
"messagecount": "{count,plural, =0{no new messages}=1{you have 1 new message}=2{you have a couple of messages}other{you have {count} new messages}}",

now you can also capture internal variables in the plural/selector modifiers, and add the type and parsing information into it!

messagecount:
    plural:count:
      =0: no new messages
      =1: you have 1 new message. you won {{money:int:compactcurrency(decimaldigits:2,name:"euro",symbol:"€")}}, congratulations!
      =2: you have a couple of messages
      other: you have {{count:int}} new messages

all {{variables}} supports this special way to define name, type, format, arguments.
useful when you don’t want to use the @meta arb approach.

the “format” part applies to numberformatter and dateformat constructors.
{{variable:type:format(optionalnamedarguments)}}

selectors (like “gender”), are also included for the arb generation, although not yet supported on flutter’s intl for code generation:

rolewelcome:
  selector:role:
    admin: hi admin!
    manager: hi manager!
    other: hi visitor.

output arb:

"mainrolewelcome": "{role, select, admin {hi admin!} manager {hi manager!} other {hi visitor.} }",
"@mainrolewelcome": {
    "description": "auto-generated for mainrolewelcome",
    "placeholders": {
        "role": {
            "type": "string"
        }
    }
}

�� utilities:

you can use simplelangpicker() widget when you generate the dart code (included by default in `tdata class]).
is meant to be a quick tester to change languages. for example, if you use getx for translations:

return scaffold(
  appbar: appbar(
    title: text(widget.title),
    actions: [
      simplelangpicker(
        onselected: get.updatelocale,
        selected: get.locale,
      ),
    ],
  ),
  ...

we will try to provide a richer experience integrating more libraries outputs in the future.

�� considerations:

  • is preferable to keep the trconfig.yaml in the root of your project, some commands assumes that location (like arb generation).
  • when using arb output, make sure you have l10n.yaml next to the trconfig.yaml at the root of your project.
  • in your spreadsheet, the first column will always be your “keys”, don’t change that, don’t move the column.
  • in your trconfig.yaml, the first locale you define is your master language:
locales:
  - es ## master language used in `entry_file`
  - en ## language list to translate.
  - ko
  • the tool only analyzes the locales in your config… will not keep updated any other sheet column with other locales.
  • you can move the columns, or add new columns, around as long as the keys is always the first (a1).
  • the 1st row in your sheet are the “headers”, don’t change the autogenerated names.
  • if fts finds the 2nd row empty in any column, it will take the data corrupted, and will re-upload for translation.
  • if the row count from keys is different from the master language, it will invalidate the entire sheet.

thanks for passing by!


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.