Search code examples
flutterflutter-getx

how to make Localization with Getx if there is parameter in the text


if the text like this:

           Text(
              'Put something ${widget.profileA} might like'.tr,        
            ),

Here is the code for the translation example:

class Language extends Translations {
  @override
  Map<String, Map<String, String>> get keys => {
        'en_US': {
          'Put something ${widget.profileA} might like': 'some translation', 
        };
      };
}

My question is since there is a parameter ${widget.profileA} in the text, is there a good solution to translate whole sentence with this parameter? thank you!


Solution

  • The documentation of GetX explains well how you can do that here: https://pub.dev/packages/get#internationalization

    Using translation with parameters

    
    
    Map<String, Map<String, String>> get keys => {
        'en_US': {
            'logged_in': 'logged in as @name with email @email',
        },
        'es_ES': {
           'logged_in': 'iniciado sesión como @name con e-mail @email',
        } };
    
    Text('logged_in'.trParams({   
      'name': 'Jhon',
      'email':'jhon@example.com'
    }));
    

    So for your example:

    class Language extends Translations {
      @override
      Map<String, Map<String, String>> get keys => {
            'en_US': {
              'Put something @profile might like': 'some translation with @profile', 
            };
          };
    }
    

    and then

    Text('Put something @profile might like'.trParams({
      'profile': widget.profileA,
    }));