- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在我的 WPF 应用程序中使用 MVVM 模式,并将具有本地化功能的 DisplayAttribute 应用于我的模型属性:
class MyModel
{
[Display(
ResourceType = typeof(Resources.Strings),
Name = "MyPropertyName",
Description = "MyPropertyDescription")]
public double MyProperty { get; set; }
}
我想显示模型属性的本地化属性。必要的资源文件位于适当的位置,必要的文化设置。我还将构建操作设置为嵌入式资源,并将自定义工具设置为 PublicResXFileCodeGenerator - 根据 DisplayAttribute 的要求。但是 DisplayAttribute 为其 Name 参数返回“MyPropertyName”而不是本地化字符串。
我使用 DisplayProperty 的场景如下所示。为了显示模型属性的属性属性,我使用了一个带有辅助类的转换器,如下所示:
namespace MyProject.Converters
{
// helper class
public class MetadataParameters
{
public Type ModelType { get; set; }
public string ModelProperty { get; set; }
public Type AttributeType { get; set; }
public string AttributeProperty { get; set; }
}
public class MetadataConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mp = parameter as MetadataParameters;
var modelPropertyInfo = mp.ModelType.GetProperty(mp.ModelProperty);
var attribute = modelPropertyInfo
.GetCustomAttributes(true)
.Cast<Attribute>()
.FirstOrDefault(memberInfo => memberInfo.GetType() == mp.AttributeType);
var attributeProperty = attribute.GetType().GetProperty(mp.AttributeProperty);
return attributeProperty.GetValue(attribute, null);
}
}
}
XAML 资源:
xmlns:converters="clr-namespace:MyProject.Converters"
<converters:MetadataConverter x:Key="metadataConverter" />
XAML:
xmlns:converters="clr-namespace:MyProject.Converters"
xmlns:DataAnnotations="clr-namespace:System.ComponentModel.DataAnnotations;assembly=System.ComponentModel.DataAnnotations"
xmlns:Models="clr-namespace:MyProject.Models"
<TextBlock
<TextBlock.Text>
<Binding
Mode="OneWay"
Converter="{StaticResource metadataConverter}">
<Binding.ConverterParameter>
<converters:MetadataParameters
ModelType="{x:Type Models:Model}"
ModelProperty="ModelProperty"
AttributeType="{x:Type DataAnnotations:DisplayAttribute}"
AttributeProperty="Name" />
</Binding.ConverterParameter>
</Binding>
</TextBlock.Text>
</TextBlock>
我编写了自己的简单属性,它返回本地化的字符串,而不仅仅是属性名称的值(如 DisplayAttribute 所做的那样):
public class LocalizedDisplayAttribute : Attribute
{
private readonly string name;
public virtual string Name
{
get
{
var rm = new System.Resources.ResourceManager(typeof(Resources.Strings));
return rm.GetString(this.name);
}
}
public LocalizedDisplayAttribute(string name)
: base()
{
this.name = name;
}
}
class MyModel
{
[LocalizedDisplay("MyPropertyName")]
public double MyProperty { get; set; }
}
那么为什么 DisplayAttribute 不本地化它的属性呢? MSDN 说这个属性是为本地化而设计的。在我的 ASP.NET MVC 项目中,DisplayAttribute 工作正常。
最佳答案
DisplayAttribute 上有 Get 方法和您需要调用的其他一些方法,它们将为您提供您正在寻找的本地化字符串。您在这里所做的是获取 DisplayAttribute.Name 属性的值,即“MyPropertyName”。
如果您调用 DisplayAttribute.GetName(),它将查找由 ResourceType 属性指定的资源类,然后它将使用 Name 属性来反射(reflect)资源类型上的属性。
我更改了您的代码,假设您请求的 AttributeProperty 将获得一个 GetXXX() 方法。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mp = parameter as MetadataParameters;
var modelPropertyInfo = mp.ModelType.GetProperty(mp.ModelProperty);
var attribute = modelPropertyInfo
.GetCustomAttributes(true)
.Cast<Attribute>()
.FirstOrDefault(memberInfo => memberInfo.GetType() == mp.AttributeType);
// We have to call the GetXXX methods on the attribute to get a localised result.
//return ((DisplayAttribute)attribute).GetName();
var result = attribute
.GetType()
.InvokeMember(
"Get" + mp.AttributeProperty,
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null,
attribute,
new object[0]);
return result;
}
关于c# - DisplayAttribute 未在模型中本地化其属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14573757/
翻译 silverlight 5 应用程序 (Prism + MEF) 的可用选项是什么? 如果可能的话,我想: 没有 resx 文件(我不喜欢它们的管理方式) 而是提取并翻译 xaml 字符串(我认
我可以同时使用这两种方法来本地化 $| 还是我应该使用其中一种来支持另一种? 方法一:备份“_init_scr”中$|的旧值,并在调用“_end_win”时将$|设置回旧值。方式 2:调用 local
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C# localization , really confusing me 有人可以分享他们对大型 C# 应
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我正在使用 Phoenix 框架开发多语言应用程序 到目前为止,路由器看起来像这样: scope "/:locale", App do pipe_through [:browser, :bro
我想为我的域对象添加本地化支持。我有以下几点: class Person { int Id; City city; } class City { int Id; str
我需要 BlackBerry 本地化方面的帮助。我在 http://na.blackberry.com/eng/developers/resources/developer_labs.jsp#tab_
我正在尝试通过关注 documentation 来本地化我的 Flutter 应用程序. 我想要实现的是,在动态构建小部件的同时,我想转换来自我的模型的数据。这是我迄今为止尝试过的 List.gene
如何更改 EKEventEditViewController 中的默认语言,即使我手动设置 AppleLanguages 对象,它也始终是英语 最佳答案 我通过向我的应用程序 plist 添加一个 L
我在 iphone 日期选择器中遇到本地化问题。我希望日期选择器仅以英语显示日期,但现在它需要在 iphone 设置中设置为区域的语言。我尝试了各种没有用的方法像下面这样。 在 uidatepicke
我的应用仅支持荷兰语和法语。英语不是此应用程序的可用语言。如果我想使用可本地化的字符串,则默认值始终设置为英语。我希望这是荷兰语。所以我所做的就是使用英语可本地化字符串文件并用荷兰语单词填充它。我遇到
我即将本地化 iPhone 应用程序。当用户的语言(iOS 系统语言)是德语时,我想使用不同的 URL。 我想知道这是否是正确的方法: NSURL *url = [NSURL URLWithStrin
我正在将我的 iPhone 应用程序本地化为多种语言,除了更改一些字符串之外,我还需要更改一些背景。是否可以查询iPhone并获取用户的语言代码? 谢谢! 最佳答案 看一下 NSLocale: NSS
在本地化的 Iphone(语言设置为希伯来语)上,当我们使用 Safari 查看网页并点击输入字段时,我们会在键盘上显示希伯来语的“下一个/上一个/完成”按钮。 当我们使用应用程序中嵌入的 UIWeb
是否有更好的方法来存储大量文本以在 laravel 中进行本地化?如果我的整个页面只是纯文本,那会很容易,但是我的几个页面具有复杂的布局,我需要添加多个字符串来将文本包裹在图像/链接/媒体等内容周围。
我正在尝试将我的应用程序本地化。我为支持的语言创建了所需的 string.arb 文件。 为什么AppLocalizations.of(context)需要上下文? 我只是想访问文件/语言环境文件/类
绑定(bind)到 double可能会产生以下验证错误 Value ... could not be converted. 使用 ExceptionValidationRule 时错误更健谈: Inp
我有一些 Delphi 经验,并且正在尝试使用 Lazarus 构建一个项目,这对我来说是全新的。 我想,我已经阅读了有关 Lazarus、翻译/国际化/本地化的所有可用信息,但我无法找到我真正想要的
我一直在尝试更新网站的语言。 Controller public function getUpdateLanguage(Request $request) { $request_dat
我正在为我们正在启动的一个项目寻找框架和 CMS,该项目是一个需要支持本地化内容的网站。 Laravel 看起来不错,并且当然允许您本地化 UI 内容,但它似乎本身并不支持存储在数据库中的内容的本地化
我是一名优秀的程序员,十分优秀!