Flutter国际化-动态字符串 [英] Flutter internationalization - Dynamic strings
问题描述
我正在使用intl包将我的应用翻译成西班牙语.
I'm translating my app to spanish using the intl package.
locales.dart
locales.dart
class AppLocale {
...
String get folder => Intl.message("Folder", name: 'folder');
...
}
messages_es.dart
messages_es.dart
class MessageLookup extends MessageLookupByLibrary {
get localeName => 'es';
final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => <String, Function> {
"folder": MessageLookupByLibrary.simpleMessage("Carpeta"),
};
}
我用以下代码称呼它:
AppLocale.of(context).folder
工作正常.
但是,我需要创建动态"字符串.例如:
However, I need to create "dynamic" strings. For example:
{$ name}"
然后,我将调用此字符串,并将此名称"作为参数或类似的名称传递.西班牙语将其翻译为"Hola,{$ name}".
Then I would call this string, passing this "name" as parameter, or something like this. It would be translate as "Hola, {$name}" in spanish.
可以使用此intl软件包吗?
It is possible using this intl package?
推荐答案
intl
程序包的自述文件说明了该示例
https://github.com/dart-lang/intl
The README of the intl
package explains that example
https://github.com/dart-lang/intl
将消息包装在函数中的目的是允许它 具有可以在结果中使用的参数.消息字符串是 允许使用Dart字符串插值的受限形式,其中 只能使用函数的参数,并且只能简单使用 表达式.不能使用局部变量,也不能使用 大括号的表达式.只有消息字符串可以具有 插值.名称,desc,args和示例必须是文字和 不包含插值.只有args参数可以引用 变量,并且应准确列出功能参数.如果你 正在传递数字或日期,并且您要格式化它们,则必须 函数外部的格式,并将格式化后的字符串传递到 消息.
The purpose of wrapping the message in a function is to allow it to have parameters which can be used in the result. The message string is allowed to use a restricted form of Dart string interpolation, where only the function's parameters can be used, and only in simple expressions. Local variables cannot be used, and neither can expressions with curly braces. Only the message string can have interpolation. The name, desc, args, and examples must be literals and not contain interpolations. Only the args parameter can refer to variables, and it should list exactly the function parameters. If you are passing numbers or dates and you want them formatted, you must do the formatting outside the function and pass the formatted string into the message.
greetingMessage(name) => Intl.message(
"Hello $name!",
name: "greetingMessage",
args: [name],
desc: "Greet the user as they first open the application",
examples: const {'name': "Emily"});
print(greetingMessage('Dan'));
在本节下面,有更复杂的示例被解释,它们也处理复数形式和性别.
Below this section there are more complex examples explained that also deal with plurals and genders.
这篇关于Flutter国际化-动态字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!