Is possible to use %s string format in Flutter?

216

sprintf: ^6.0.0 Use this package

  var me = {
    "ocrMessage":
        "User with name %s found in the database. Would you like to select and edit them?"
  };
  print(sprintf(me["ocrMessage"].toString(), [100]));

Output:

User with name 100 found in the database. Would you like to select and edit them?
Share:
216
Darko Milošević
Author by

Darko Milošević

Updated on January 03, 2023

Comments

  • Darko Milošević
    Darko Milošević over 1 year

    I need to work with .arb files for internalization in Flutter, and I am interesting is possible to use string format with %s, and integer format with %d in Flutter, such in Java, C#, Python, etc. I want for example this string in app_en.arb

    {
    "ocrMessage": "User with name %s found in the database. Would you like to select and edit them?"
    }
    

    I was searching Flutter documentation and Google, but I did not find anything about formatting strings in Flutter, only string interpolation, eg:

    var myName = "Darko";
    Print("Your name is $myName");
    

    Thank you very much.

    Best regards,

    Darko

  • Darko Milošević
    Darko Milošević about 2 years
    Do I need to add placeholders in each .arb file where I want interpolation, or only in app_en.arb, because I am translating the app from English to several languages.
  • b4sti
    b4sti about 2 years
    As far as I know, you only need to specify the placeholders in your main arb (like the English one). Then you can use it in other files too without redefining it as long as you are using the same name for it (like firstName in my example). But, it will also work if you add your placeholder in other arb files again. Sometimes this can make sense if you'd like to have a translated placeholder name.