例如,我有一系列部门
ObservableCollection<Department> Departments ...
XAML
<ComboBox ItemsSource="{Binding Departments}"
SelectedValue="{Binding SelectedDepartmentId}"
SelectedValuePath="Id"
DisplayMemberPath="Name" />
如您所见,DisplayMemberPath设置为“Name”,名称为英文。
由于这是 DATA 和 NOT UI ,我认为我不应该使用resx资源,因为如果添加新部门及其翻译,则需要如果使用resx,则重新编译并释放。
如何本地化数据?
答案 0 :(得分:1)
我个人使用自定义加分扩展程序there's a good article about it here。我还给它一个custom namespace,它允许我像这样编写XAML:
<Label Content="{Translate 'First Name:'}" />
除此之外,我还写了两个实用程序。第一个搜索我的所有XAML文件以查找&#34; {Translate ...}&#34;标记并将密钥(例如&#39;名字:&#39;)保存在CSV文件中,然后可以将其导入到翻译人员的电子表格中或粘贴到Google翻译等进行测试。第二个实用程序然后从电子表格中获取值并将它们转换为XML,然后将其加载到我的本地化管理器的相应字典中。英语词典留空,只要在字典中找不到字符串,它就会回落到键本身。这个系统意味着我可以在编写XAML时使用英语,但导出和导入键的过程是完全自动化的,我可以在运行时即时更改应用程序中所有字符串的语言。
答案 1 :(得分:0)
所以这是 MY 方法:
在数据库中创建一个具有本地化值
的表Table Departments_Localized_Names
IdDepartment -> Foreign key to the original department
culture -> Relate to the culture/language
LocalizedDisplayName -> Translated department name
使用部分实体类甚至更好的数据传输对象,添加“LocalizedDisplayName”属性,此属性将仅在程序加载时设置,并且该值将取决于当前文化
public partial class Department
{
//Set it at runtime according to the current culture
public string LocalizedDisplayName{get;set;}
}
所以在显示数据时,我从Departments_Localized_Names中提取LocalizedDisplayName值,具体取决于应用程序当前文化和部门ID,并在对象上设置LocalizedDisplayName,这样我就可以执行以下操作:
<ComboBox ItemsSource="{Binding Departments}"
SelectedValue="{Binding SelectedDepartmentId}"
SelectedValuePath="Id"
DisplayMemberPath="LocalizedDisplayName" />
插入新部门时,我只需确保将已翻译的值添加到Departments_Localized_Names中,并且无需新版本即可使用。