Silverlight-国际化的参数化资源价值 [英] Silverlight - Paramterised resource values for Internationalisation

查看:69
本文介绍了Silverlight-国际化的参数化资源价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,似乎没有我满意的明智/易处理的解决方案. Silverlight和国际化-不断逼近.

I'm running into a problem that seems to have no sensible / tractable solution that I am happy with. Silverlight and Internationalisation - a quagmire in the making.

我有一组包含翻译后的字符串的资源文件.

I have a set of resource files that contain translated strings.

在简单的情况下,我可以声明语言资源等,并将值的内容/文本绑定到强类型资源中.很好-周围有很多例子.

In the simple case, I can declare the language resource etc and bind the content / text of a value within the strongly typed resource. Thats fine - plenty of examples of that posted around.

一个不太简单的情况是字符串可以有一个参数,因此我需要在字符串中注入一个值以显示它,它本身可以是数据绑定.

A less simple case is that the strings can have a parameter, so I need to inject a value into the string for the display of it, which in itself can be a data binding.

更复杂的情况是带有多个参数的字符串.

The more complex scenario is a string with multiple parameters.

您可以争辩说VM应该提供此功能,但我对此感到不满意,因为它打破了UX设计器与开发人员之间的鸿沟,要求开发人员在VM上实现属性/方法以支持每个字符串.用户界面要求.

You can argue that the VM should provide this ability, but I am unhappy at that since it breaks the divide between the UX Designer and the Developer, requiring the developer to implement a property / method on the VM to support every string the UI requires.

例如:设计人员在用户反馈后决定添加带有更多信息的自定义工具提示.工具提示是声明式的,并结合了更好的解释和数据上下文中的值.解释存储在资源中,使用的值来自现有数据上下文或元素到元素的绑定.

For example : The designer decides after user feedback to add a custom tooltip with more information on it. The tooltip is done declaratively and combines a better explanation with values from the datacontext. The explanation is stored in the resources and the values used come from either an existing data context or an element to element binding.

如果必须通过VM运行每个字符串,那么添加类似内容需要更改VM.同样,如果参数的来源来自其他元素,则VM不一定就可以提供格式化的字符串.

If I have to run every string through the VM then the addition of something like this requires VM changes. Equally if the source of the parameters will be from other elements, then the VM is not necessarily in the position to provide the formatted string.

人们如何解决这个问题?

How have people got round / approached this issue?

A.

推荐答案

我不确定您要做什么,但是您有几种解决方案.

I don't know exactly what you want to do but you've got several solutions.

  • 在您的VM中创建一个新字符串,并将其设置为INotifyPropertyChanged-able

  • Create a new string in your VM and make it INotifyPropertyChanged-able

public string MyTranslatedString 
{ 
   get 
     { 
        return string.Format("{0} Someone", LocalizedResource.Hello;
     }
 };

然后(从您的应用程序中)监听本地化更改事件

And then listen for localization change events (from your application)

  • 创建几个文本块并绑定本地化的项目:

  • Create several text blocks and bind the localized items:

<TextBlock HorizontalAlignment="Stretch" Foreground="Black" Text="{Binding Path=Resource.CurrentlyLoggedInAs, Source={StaticResource LocalizedStrings }}" VerticalAlignment="Center" Margin="0,0,5,0" />
<TextBlock HorizontalAlignment="Stretch" Foreground="Black" Text="{Binding Path=Username}" VerticalAlignment="Center" Margin="0,0,5,0" />
<Button Commands:Click.Command="{Binding Path=LogOutCommand}" Commands:Click.CommandParameter="" Content="{Binding Path=Resource.LogOut, Source={StaticResource LocalizedStrings }}" />

您需要在主应用中添加:

You need to add, in your main app:

<Application.Resources>
    <local:LocalizedStrings 
        xmlns:local="clr-namespace:Localization.Silverlight;assembly=Localization.Silverlight"
        x:Key="LocalizedStrings" />
</Application.Resources>

这些是我想到的最简单的方法,但是我很高兴能为我提供一些更简单的方法.

These are the easiest approaches I came up with but I'm quite happy to have something providing me with something simpler.

这篇关于Silverlight-国际化的参数化资源价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆