Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization generator request #6

Closed
alexkharech opened this issue Sep 3, 2020 · 13 comments
Closed

Localization generator request #6

alexkharech opened this issue Sep 3, 2020 · 13 comments

Comments

@alexkharech
Copy link
Contributor

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)
@alexkharech
Copy link
Contributor Author

alexkharech commented Sep 3, 2020

can be a simple key / value map

  static const Map<String, dynamic> en_US = {
    "menu.logoutBtn": "Log out",
    "menu.items.settings": "Settings",
  };

@roipeker
Copy link
Contributor

roipeker commented Sep 3, 2020

Agree 💯 , super useful feature for code generation.

@alexkharech
Copy link
Contributor Author

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

@CpdnCristiano
Copy link
Collaborator

What a fantastic idea, I really liked it

@alexkharech
Copy link
Contributor Author

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': 'Кошелёк',
  };

}

@CpdnCristiano
Copy link
Collaborator

congratulations!! you did an excellent job

@CpdnCristiano
Copy link
Collaborator

I will close this issue

@jnickzlim
Copy link

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.

@CpdnCristiano
Copy link
Collaborator

CpdnCristiano commented Jan 10, 2021

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

@jnickzlim
Copy link

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

@jnickzlim
Copy link

@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

@CpdnCristiano
Copy link
Collaborator

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

@jnickzlim
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants