软件包intl
存在用于定位颤振应用程序,但Intl.message
只返回String
如何实现丰富的内容本地化?
Hello $name!
,并且只将$name
加粗,考虑在不同的语言中hello和$name的顺序可以不同I read terms of services and accepted it
,只链接terms of services
部分in [TEXT_INPUT] days
, [TEXT_INPUT]文本和文本后,但在一些语言没有2文本,一个文本之前或之后进行,或days
后之前,in
[TEXT_INPUT]
看一下styled_text包。您可以将文本(内容)与格式分开指定,这允许您使用标准的i18n方法。
文本格式指定为标签,您可以全局定义(例如在资源类中),然后在任何地方使用。我觉得这是个好办法。
示例(来自Readme):
StyledText(
text: 'Test: <bold>bold</bold> text.',
tags: {
'bold': const StyledTextTag(style: const TextStyle(fontWeight: FontWeight.bold)),
},
)
将显示如下:
大胆测试:文本。
如果使用i18n包和资源类实例R
作为标记,则可能如下所示:
StyledText(
text: S.testBoldText,
tags: R.styledTextTags,
);
请记住,在翻译中需要保留标签。
俚语包具有内置的富文本支持。
它看起来像这样:
{
"myRichText(rich)": "Welcome {name}"
}
Widget a = Text.rich(t.myRichText(
// Show name in blue color
name: TextSpan(text: 'Tom', style: TextStyle(color: Colors.blue)),
));
下面是一个如何在翻译中注入变量的示例。
"hello_name": "Hello {name}",
"@hello_name": {
"placeholders": {
"name": {
"type": "String"
}
}
},
这将生成一个函数hello_name
给你传递name
没有样式支持,但是如果你需要它,那么就不要像上面的例子那样使用placeholders
,让你的文本分开,并使用RichText
来指定文本的哪些部分需要有什么样式。你不需要担心方向,因为它会根据你的本地为你处理。