Skip to content

Localization generator request #6

Closed

Description

@alexkharech

It would be great to add a localization generator from json files to get_cli, while generating an abstract class with keys and translation maps, just like in the easy_localization package.

input directory

en_EN.json
ru_RU.json
{
  "menu": {
    "logoutBtn": "Log out",
    "items": {
      "settings": "Settings",
      "notifications": "Notifications",
      "wallet": "Wallet"
    }
  }
}
{
  "menu": {
    "logoutBtn": "Выход",
    "items": {
      "settings": "Настройки",
        "notifications": "Оповещения",
        "wallet": "Кошелёк"
    }
  }
}

generated keys

abstract class LocaleKeys {
  static const menu_logoutBtn = 'menu.logoutBtn';
  static const menu_items_settings = 'menu.items.settings';
  static const menu_items_notifications = 'menu.items.notifications';
  static const menu_items_wallet = 'menu.items.wallet';
}

generated localization maps

abstract class Locales {
  static const Map<String, dynamic> en_US = {
    "menu": {
      "logoutBtn": "Log out",
      "items": {
        "settings": "Settings",
        "notifications": "Notifications",
        "wallet": "Wallet"
      },
    },
  };

  static const Map<String, dynamic> ru_RU = {
    "menu": {
      "logoutBtn": "Выход",
      "items": {
        "settings": "Настройки",
        "notifications": "Оповещения",
        "wallet": "Кошелёк"
      },
    },
  };
}

using

Text(LocaleKeys.menu_logoutBtn.tr)

Activity

  1. alexkharech commented on Sep 3, 2020

    @alexkharech
    ContributorAuthor

    can be a simple key / value map

      static const Map<String, dynamic> en_US = {
        "menu.logoutBtn": "Log out",
        "menu.items.settings": "Settings",
      };
  2. roipeker commented on Sep 3, 2020

    @roipeker
    Contributor

    Agree 💯 , super useful feature for code generation.

  3. alexkharech commented on Sep 3, 2020

    @alexkharech
    ContributorAuthor

    I have implemented this, now I will post a pull request

  4. CpdnCristiano commented on Sep 3, 2020

    @CpdnCristiano
    Collaborator

    What a fantastic idea, I really liked it

  5. alexkharech commented on Sep 3, 2020

    @alexkharech
    ContributorAuthor

    PR #7

    assets/locales - input directory with json files

    get generate locales assets/locales

    output file lib/generated/locales.g.dart

    // DO NOT EDIT. This is code generated via package:get_cli/get_cli.dart
    
    abstract class LocaleKeys {
        static const simple = 'simple';
      static const menu_logoutBtn = 'menu_logoutBtn';
      static const menu_items_settings = 'menu_items_settings';
      static const menu_items_notifications = 'menu_items_notifications';
      static const menu_items_wallet = 'menu_items_wallet';
    }
    
    abstract class Locales {
      
      static const en_US = {
       'simple': 'The Simple',
       'menu_logoutBtn': 'Log out',
       'menu_items_settings': 'Settings',
       'menu_items_notifications': 'Notifications',
       'menu_items_wallet': 'Wallet',
      };
      static const ru_RU = {
       'menu_logoutBtn': 'Выход',
       'menu_items_settings': 'Настройки',
       'menu_items_notifications': 'Оповещения',
       'menu_items_wallet': 'Кошелёк',
      };
    
    }
  6. CpdnCristiano commented on Sep 3, 2020

    @CpdnCristiano
    Collaborator

    congratulations!! you did an excellent job

  7. CpdnCristiano commented on Sep 3, 2020

    @CpdnCristiano
    Collaborator

    I will close this issue

  8. jnickzlim commented on Jan 10, 2021

    @jnickzlim

    Sorry for the dump question, but i could not figure out how to use the generated locales.

    main.dart

    void main() {
      runApp(
        GetMaterialApp(
          initialRoute: AppPages.INITIAL,
          getPages: AppPages.routes,
          translationsKeys: AppTranslation.translations,
          locale: Locale('en', 'US'),
          fallbackLocale: Locale('en', 'US'),
          supportedLocales: <Locale>[Locale('en', 'US'), Locale('zh', 'CN')],
          debugShowCheckedModeBanner: false,
        ),
      );
    }

    i have tried:

        Text(LocaleKeys.titles_home.tr) // not working
        Text(LocaleKeys.titles_home) // not working
        Text('titles_home'.tr) // not working

    Thank you for the help in advance.

  9. CpdnCristiano commented on Jan 10, 2021

    @CpdnCristiano
    Collaborator

    is the text output in English or it's empty?

  10. jnickzlim commented on Jan 10, 2021

    @jnickzlim

    The out put is titles_home instead of it's content.

  11. jnickzlim commented on Jan 11, 2021

    @jnickzlim

    @CpdnCristiano or any one please help?

    The generated locales.g.dart:

        // DO NOT EDIT. This is code generated via package:get_cli/get_cli.dart
    
    abstract class AppTranslation {
      static Map<String, Map<String, String>> translations = {
        'zh_CN': Locales.zh_CN,
        'en_EN': Locales.en_EN,
      };
    }
    
    abstract class LocaleKeys {
      static const titles_home = 'titles_home';
      static const buttons_sign_up = 'buttons_sign_up';
      static const buttons_sign_in = 'buttons_sign_in';
      static const buttons_logout = 'buttons_logout';
    }
    
    abstract class Locales {
      static const zh_CN = {
        'titles_home': '首页',
        'buttons_sign_up': '注册',
        'buttons_sign_in': '登入',
        'buttons_logout': '退出',
      };
      static const en_EN = {
        'titles_home': 'Home',
        'buttons_sign_up': 'Sign Up',
        'buttons_sign_in': 'Sign in',
        'buttons_logout': 'Logout',
      };
    }

    In the app it shows the titles_home instead of Home or 首页

    Simulator Screen Shot - iPhone 12 Pro - 2021-01-11 at 10 40 17

  12. CpdnCristiano commented on Jan 11, 2021

    @CpdnCristiano
    Collaborator

    you generated for the locale en_EN and are sitting on the app en_US

  13. jnickzlim commented on Jan 11, 2021

    @jnickzlim

    Thank you for the shape eye, didn't notice the example was using EN instead of US
    Cheers~!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions