Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter rich content localization

The package intl exists to localize the flutter application but the Intl.message only returns String

How can I have a rich content localization?

  1. Hello $name! , and make only $name bold, Consider that the order of hello and $name can be different in different languages
  2. I read terms of services and accepted it , and link only terms of services part
  3. in [TEXT_INPUT] days , the [TEXT_INPUT] has a text after and a text before, but in some languages there is no 2 text, just one text after or before, or days is before and in is after [TEXT_INPUT]
like image 260
WebMaster Avatar asked Jun 18 '26 11:06

WebMaster


2 Answers

Have a look at the styled_text package. You get to specify the text (content) separately from the formatting, witch allows you to use standard i18n approaches.

Text format is specified as tags wich you can define globally (e.g. in a resource class) and then use everywhere. Sounds like a good solution to me.

Example (from the Readme):

StyledText(
  text: 'Test: <bold>bold</bold> text.',
  tags: {
    'bold': const StyledTextTag(style: const TextStyle(fontWeight: FontWeight.bold)),
  },
)

will display like:

Test: bold text.

If you use the i18n package and a resource class instance R for the tags, this might look like:

StyledText(
  text: S.testBoldText,
  tags: R.styledTextTags,
);

Just keep in mind that the tags need to be preserved in translation.

like image 72
Ber Avatar answered Jun 21 '26 04:06

Ber


The slang package has built-in rich text support.

It will look like this:

{
  "myRichText(rich)": "Welcome {name}"
}
Widget a = Text.rich(t.myRichText(
  // Show name in blue color
  name: TextSpan(text: 'Tom', style: TextStyle(color: Colors.blue)),
));
like image 25
Tien Do Nam Avatar answered Jun 21 '26 04:06

Tien Do Nam