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

Json to translation strings support #71

Open
Tkko opened this issue Dec 22, 2022 · 2 comments
Open

Json to translation strings support #71

Tkko opened this issue Dec 22, 2022 · 2 comments

Comments

@Tkko
Copy link

Tkko commented Dec 22, 2022

Is your feature request related to a problem? Please describe.
The ability to generate icon asset paths is fabulous but it would be great to also generate app_string.dart file from json
For example:

assets/translations/*

en.json
{
  "appTitle": "Title",
  "continueButton": "Continue"
}
ka.json
{
  "appTitle": "სათაური",
  "back": "უკან"
}

Output:

class Strings {
  const Strings._();

  static const appTitle = 'appTitle';
  static const continueButton = 'continueButton';
  static const back = 'back';

}

This is helpful if you are using json based localizations, checkout the article for reference.

In short, you have translation files, en.json, ka.json, ... and you are accessing the translated string by the key, and with this request keys will be extracted as constants.

Describe the solution you'd like
Scan the specific directory assets/translations/ merge all the json keys and export them as a class containing static const instances of retrieved keys.

Describe alternatives you've considered
I'm tried adding the feature to flutter_gen package but the author is holding back.

@BirjuVachhani
Copy link
Owner

BirjuVachhani commented Dec 23, 2022

@Tkko I don't know much about localization in Flutter but this seems like a job that can be handled better by json_serialization.

So instead of generating a Strings class for json keys and using that for retrieving value, you should generate a data class from the JSON and parse json with it. Store instance of it in your AppLocalizations class.

Your data class

You can generate this using any json to dart generator.

@JsonSerializable()
class AppStrings {
  final String appTitle;
  final String continueButton;
  final String back;

  const AppStrings({
    required this.appTitle,
    required this.continueButton,
    required this.back,
  });

  factory AppStrings.fromJson(Map<String, dynamic> json) => _$AppStringsFromJson(json);

  Map<String, dynamic> toJson() => _$AppStringsToJson(this);
}

Loading from JSON

AppStrings strings;

Future<bool> load() async {
  // Load the language JSON file from the "lang" folder
  String jsonString =
  await rootBundle.loadString('assets/i18n/${locale.languageCode}-${locale.countryCode}-${locale.countryCode}.json');
  Map<String, dynamic> jsonMap = json.decode(jsonString);
  strings = AppStrings.fromJson(jsonMap);
  return true;
}

How to Use

Before

AppLocalizations.of(context).translate('title');

After

AppLocalizations.of(context).strings.appTitle;

This way it achieves what you wanted to achieve with Spider and it is much cleaner too. I hope this is helpful.

@Tkko
Copy link
Author

Tkko commented Dec 23, 2022

That would work but everytime I add new translation I have to use external package or online tool to convert the Json to data class, wouldn't it be nicer if spider makes all of that?

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

2 participants