TeamWanari / rosetta

Flutter Localization tool with code generation tool.
MIT License
35 stars 7 forks source link

Support Interceptors #14

Closed agta1991 closed 5 years ago

agta1991 commented 5 years ago

Is your feature request related to a problem? Please describe. In most cases we will end up with some kind of parametrization in some of our resouces, like displaying currencies, amount, etc...

Describe the solution you'd like We should provide a new annotation (for example @Intercept), which can intercept the result of currently generated getters. Also, the annotation could include a regular expression string, to filter translations which should be intercepted. For parametrization, we can provide additional logic for the generator to include the same parameters on the getters as described on the interceptor function.

Describe alternatives you've considered We could include sprintf or any other library in the generator, but that would result in a strong dependency and a more abstract solution is always better. :)

Additional context Simple example:

@Intercept
String simpleIntercept(String translation) => ">>> $translation";

this should generate getters like:

String get helloLabel => simpleIntercept(_translate(_$Keys.helloLabel));

Parametrized example:

@Intercept(filter: r'%(?:(\d+)\$)?([\+\-\#0 ]*)(\d+|\*)?(?:\.(\d+|\*))?([a-z%])')
String paramIntercept(String translation, var args) => sprintf(translation, args);

this should generate getters like:

String helloLabel(var args) => paramIntercept(_translate(_$Keys.helloLabel), args);