owoeo / dart_i18n_generator

Alternative i18n code generator for vscode-flutter-i18n-json

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

i18n_json

Introduction

An alternative dart code generator for the vs-code plugin "vscode-flutter-i18n-json", written in in dart. This project cannot yet completely replace the specified plugin, but is a workaround to deal with code generation bugs in original plugin. This project in not a vs-code plugin, rather it is a simple cli to convert the json files into one dart class. In order to use it, add it to dev dependencies in pubspec.yaml:

dev_dependencies:
  i18n_json:
    git: https://github.com/gnudles/dart_i18n_generator

and generate i18n.dart by running the command

flutter pub run i18n_json

Features

  • Full compatibility with the vscode plugin.
  • Auto-detection of "Sound Null Safety"
  • Comments (through YAML, see notes below)
  • Experimental Gender & Plural (see notes below)

Notes

This cli support both YAML and JSON locale files. This was made in effort to allow adding comments in your locale files, since JSON doesn't support comments but YAML does. In order to use comments in locale files, rename the file endings to ".yaml", so this tool could detect it. If you have both endings (.yaml & .json), the .yaml will be loaded.

To add Plural or Gender, make it like:

    "sentItems":
        {
         "__gender":{
            "male": {"__plural":
                {
                    "zero": "he sent you no {item}s",
                    "one": "he sent you one {item}",
                    "other": "he sent you {count} {item}s"
                }},
            "female": {"__plural":
                {
                    "zero": "she sent you no {item}s",
                    "one": "she sent you one {item}",
                    "other": "she sent you {count} {item}s"

                }},
            "other": {"__plural":
                {
                    "zero": "they sent you no {item}s",
                    "one": "they sent you one {item}",
                    "other": "they sent you {count} {item}s"

                }},
            },
        }

This will generate the following code:

String sentItems(int count, String gender, String item){if (gender == 'male'){if (count == 0){return "he sent you no ${item}s";} else if (count == 1){return "he sent you one ${item}";} else {return "he sent you ${count} ${item}s";}} else if (gender == 'female'){if (count == 0){return "she sent you no ${item}s";} else if (count == 1){return "she sent you one ${item}";} else {return "she sent you ${count} ${item}s";}} else {if (count == 0){return "they sent you no ${item}s";} else if (count == 1){return "they sent you one ${item}";} else {return "they sent you ${count} ${item}s";}}}

Note that "count" variable is automatically added in Plural, and "gender" variable is automatically added in Gender.

About

Alternative i18n code generator for vscode-flutter-i18n-json


Languages

Language:Dart 100.0%