Flutter 与动态字符串一起使用的颤振Intl插件

Flutter 与动态字符串一起使用的颤振Intl插件,flutter,flutter-intl,Flutter,Flutter Intl,我正在使用Localizely提供的Flitter Intl插件对我的应用程序进行本地化。 我为我想要的语言生成了arb文件,并开始介绍翻译 例如: { "documentsSection": "All the documents", "favouritesSection": "Favourites", "newsSection": "News", "settingsSection": "Settings" } 每次我想本地化我使用的文本时: S.of(context).fav

我正在使用Localizely提供的Flitter Intl插件对我的应用程序进行本地化。 我为我想要的语言生成了arb文件,并开始介绍翻译

例如:

{
  "documentsSection": "All the documents",
  "favouritesSection": "Favourites",
  "newsSection": "News",
  "settingsSection": "Settings"
}
每次我想本地化我使用的文本时:

S.of(context).favouritesSection;
而且它工作得很好

但是,当我有这样一个列表时:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
显然这不起作用,因为sectionName不是arb文件中的键。但我认为代码表达了我想要实现的目标。 也许有人能帮我。
提前谢谢。

您是否尝试过使用上下文S.ofcontext[sectionName]来代替

您是否尝试过使用S.ofcontext[sectionName]来代替

我认为有两种方法可以实现你想要的

第一种方法是创建一个函数,将sectionTitles映射到intl字符串,如下所示:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
字符串getSectionTitleBuildContext,字符串标题{ 如果title==documents节{ 返回S.ofcontext.documents节; }如果title==FavoriteSection,则为else{ 返回S.ofcontext.FavoriteSection; }如果标题==新闻节,则为else{ 返回S.ofcontext.newsSection; }如果标题==设置选项,则为else{ 返回S.ofcontext.settingsSection; } } 然后像这样使用:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
... itemBuilder:上下文、索引{ 返回文本 getSectionTitlecontext,sectionTitles[索引], ; }, ... 第二种方法是使用intl字符串生成数组:

列表部分标题=[ S.ofcontext.Documents节, 上下文S.F.FavoriteSection, S.ofcontext.newsSection, S.ofcontext.Settings节, ]; 但是,您需要在构建函数中创建它,因为您需要一个上下文:

@凌驾 小部件构建上下文上下文{ 列表部分标题=[ S.ofcontext.Documents节, 上下文S.F.FavoriteSection, S.ofcontext.newsSection, S.ofcontext.Settings节, ]; 回来 itemBuilder:上下文、索引{ 返回文本 章节名称[索引], ; }, ... } 另一种不使用构建函数中的上下文的方法是使用StatefulWidgets上可用的didChangeDependencies方法,如下所示:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
列出部门名称; @凌驾 无效更改依赖项{ super.didchangependencies; 章节标题[ S.ofcontext.Documents节, 上下文S.F.FavoriteSection, S.ofcontext.newsSection, S.ofcontext.Settings节, ]; } @凌驾 小部件构建上下文上下文{ 回来 itemBuilder:上下文、索引{ 返回文本 章节名称[索引], ; }, ... } 请注意,在这种情况下,您不能使用initState,因为它不会提供一个已经存在intl字符串的上下文,所以我们使用didChangeDependencies


如果您想知道???=的作用是什么,它只会检查一个变量在本例中是否为null,如果为null,它会给它赋值。我们在这里使用它是为了避免每次都重新定义章节标题。

我认为有两种方法可以实现您想要的

第一种方法是创建一个函数,将sectionTitles映射到intl字符串,如下所示:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
字符串getSectionTitleBuildContext,字符串标题{ 如果title==documents节{ 返回S.ofcontext.documents节; }如果title==FavoriteSection,则为else{ 返回S.ofcontext.FavoriteSection; }如果标题==新闻节,则为else{ 返回S.ofcontext.newsSection; }如果标题==设置选项,则为else{ 返回S.ofcontext.settingsSection; } } 然后像这样使用:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
... itemBuilder:上下文、索引{ 返回文本 getSectionTitlecontext,sectionTitles[索引], ; }, ... 第二种方法是使用intl字符串生成数组:

列表部分标题=[ S.ofcontext.Documents节, 上下文S.F.FavoriteSection, S.ofcontext.newsSection, S.ofcontext.Settings节, ]; 但是,您需要在构建函数中创建它,因为您需要一个上下文:

@凌驾 小部件构建上下文上下文{ 列表部分标题=[ S.ofcontext.Documents节, 上下文S.F.FavoriteSection, S.ofcontext.newsSection, S.ofcontext.Settings节, ]; 回来 itemBuilder:上下文、索引{ 返回文本 章节名称[索引], ; }, ... } 另一种不使用构建函数中的上下文的方法是使用StatefulWidgets上可用的didChangeDependencies方法,如下所示:

List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
列出部门名称; @凌驾 无效更改依赖项{ super.didchangependencies; 章节标题[ S.ofcontext.Documents节, 上下文S.F.FavoriteSection, S.ofcontext.newsSection, S.ofcontext.Settings节, ]; } @凌驾 小部件构建 语境{ 回来 itemBuilder:上下文、索引{ 返回文本 章节名称[索引], ; }, ... } 请注意,在这种情况下,您不能使用initState,因为它不会提供一个已经存在intl字符串的上下文,所以我们使用didChangeDependencies


如果您想知道???=的作用是什么,它只会检查一个变量在本例中是否为null,如果为null,它会给它赋值。我们在这里使用它是为了避免每次都重新定义章节标题。

另一种解决方法是使用Select ICU格式的消息。 注意,此解决方案可能会引入一些约束,但另一方面,它更优雅

字符串键声明:

"sectionTitles": "{section, select, documentsSection {Documents section} favouritesSection {Favourites section} newsSection {News section} settingsSection {Settings section} other {Section}}",
"@sectionTitles": {
  "placeholders": {
    "section": {}
  }
}
字符串键用法:

itemBuilder:上下文、索引{ 字符串sectionName=sectionTitles[索引]; 返回文本 S.ofcontext.sectionTitlessectionName, , },
另一种解决方法是使用Select ICU格式的消息。 注意,此解决方案可能会引入一些约束,但另一方面,它更优雅

字符串键声明:

"sectionTitles": "{section, select, documentsSection {Documents section} favouritesSection {Favourites section} newsSection {News section} settingsSection {Settings section} other {Section}}",
"@sectionTitles": {
  "placeholders": {
    "section": {}
  }
}
字符串键用法:

itemBuilder:上下文、索引{ 字符串sectionName=sectionTitles[索引]; 返回文本 S.ofcontext.sectionTitlessectionName, , },
它给出了一个错误:没有为类型“S”定义运算符“[]”。它给出了一个错误:没有为类型“S”定义运算符“[]”。这就是我要找的!太好了,我很高兴它有帮助!这就是我要找的!太好了,我很高兴它有帮助!