我需要使用存储在 .resx 文件中的预翻译词来更新一个法语应用程序,只需按一下按钮

I need to update an application to be available in French using pre-translated words stored in a .resx file at the push of a button

我得到了两个 .resx 文件,其中提供了我需要在英语和法语中使用的所有单词。现在,该应用程序仅提供英文版本,我需要创建一个按钮来更改标签的内容以指向法语 .resx 而不是英语,反之亦然。如果当前语言是法语,按钮将显示英语;如果当前语言是英语,按钮将显示法语。

当前编写程序的方式,语言是硬编码的,因此标签仅查看英语 .resx,并且根据所选语言不流畅。我是这种编程的新手,我很困惑。

<Label Margin="12,2.222,0,0" Content="{Resx ResxName=LegalServicesTimesheets.Labels, Key=ApplicationTitleDASH}"/>

''Both .resx files have words associated with the key ApplicationTitleDash
''The French .resx file is called Labels.fr-CA.resx

如您所见,英语设置目前是硬编码的,我需要进行设置,以便 .resx 文件取决于当前使用的语言。

{Resx ...} 是标记扩展。看起来您正在使用 Grant Frisken 的 ResxExtension

<TextBlock Text="{Resx ResxName=MyApp.TestWindow, Key=MyText}"/>

您的 XAML 标记没有任何要更改的内容。如果你愿意,你可以指定一个 Language 属性并绑定到你的 ViewModel 上的一些 UICulture 属性(一个 CultureInfo 对象):

<Window ResxExtension.DefaultResxName="WpfApp.MainWindow" Language="{UICulture}"> 

但除此之外,.resx 的工作方式是从与资源对象的 Culture 最匹配的任何资源文件中提取,即在您的情况下:

LegalServicesTimesheets.Culture = CultureInfo.GetCultureInfo("fr-CA")

如果您将文化设置为没有 .resx 文件的文化,它应该回退到 "neutral" .resx 内容。由于您 有一个 .fr-CA.resx 文件,从 LegalServicesTimeSheets 提取的所有资源现在将从 .fr-CA.resx 文件提取。

在 code-behind 中,您可以通过 ResourceManager 检索资源字符串 ad-hoc,方法是为 GeteString 方法指定一个 CultureInfo 参数:

foo = LegalServicesTimesheets.ResourceManager.GetString("Labels", CultureInfo.CurrentUICulture)