translations_code_gen 1.1.1 copy "translations_code_gen: ^1.1.1" to clipboard
translations_code_gen: ^1.1.1 copied to clipboard

outdated

An application to generate translations keys and values for a dart/flutter application from .json file.

Translations code generator #

This is a simple tool to generate the translations code for the Dart/Flutter projects.

QuickstartPub.dev

Install #

1. Add the dependency #

dependencies:
  translations_code_gen: ^1.1.0

2. Run this commend #

flutter pub get

Usage #

1. Create a translations files in assets folder #

Create a folder called assets in the root of your project and create a file called en.json and ar.json and add the following content:

example: assets/translations/en.json

{
  "GENERAL": {
    "HELLO": "Hello",
    "WELCOME": "Welcome"
  },
  "HOME": {
    "TITLE": "Home"
  }
}

example: assets/translations/ar.json

{
  "GENERAL": {
    "HELLO": "مرحبا",
    "WELCOME": "أهلا بك"
  },
  "HOME": {
    "TITLE": "الرئيسية"
  }
}

2. Add the translations file paths to the pubspec.yaml or translations_code_gen.yaml file #

translations_code_gen:
  keys:
    input: 'assets/translations/en.json'
    output: 'lib/translations/keys.dart'
  values:
    input: 'assets/translations/'
    output: 'lib/translations/values/'

Run this command to generate the translations keys and values to the output:

flutter pub run translations_code_gen

This will generate the following keys to the lib/translations/keys.dart file:

// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: constant_identifier_names, camel_case_types

class GENERAL {
  static const String HELLO = "GENERAL.HELLO";
  static const String WELCOME = "GENERAL.WELCOME";
}

class HOME {
  static const String TITLE = "HOME.TITLE";
}

and the following values to the en.dart and ar.dart file to lib/translations/values/

example: lib/translations/values/en.dart

// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: constant_identifier_names

import '../keys.dart'; // sometimes you need to change this path to match your project structure

const Map<String, String> _general  = {
  GENERAL.HELLO: "Hello",
  GENERAL.WELCOME: "Welcome",
};

const Map<String, String> _home  = {
  HOME.TITLE: "Home",
};

final Map<String, String> enValues = {
  ..._general,
  ..._home,
};

example: lib/translations/values/ar.dart

// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: constant_identifier_names

import '../keys.dart'; // sometimes you need to change this path to match your project structure

const Map<String, String> _general  = {
  GENERAL.HELLO: "مرحبا",
  GENERAL.WELCOME: "أهلا بك",
};

const Map<String, String> _home  = {
  HOME.TITLE: "الرئيسية",
};

final Map<String, String> enValues = {
  ..._general,
  ..._home,
};

You might have to change the keys import path to match your project structure.

4. Use the generated code #

import 'package:flutter/material.dart';

import './translations/keys.dart';

// any translation package

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(HOME.TITLE.tr()),
      ),
      body: Center(
        child: Text(GENERAL.HELLO.tr()),
      ),
    );
  }
}
8
likes
0
points
47
downloads

Publisher

verified publisheryouhanasheriff.com

Weekly Downloads

An application to generate translations keys and values for a dart/flutter application from .json file.

Homepage
Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

settings_yaml

More

Packages that depend on translations_code_gen