使用silverlight4 Business构建企业级应用程序(一)——多语言支持

1.     资源的多语言支持

使用silverlight4生成默认的Silverlight Business Application后,出现Silverlight project ——MyApp and MyApp.Web.

·         MyAppAssets\Resources添加ApplicationStrings.zh-CN.resx,然后将ApplicationStrings.resx中的字符串拷贝到ApplicationStrings.zh-CN.resx中   改为中文资源说明;

·         编辑MyApp.csproj,添加多语言支持:<SupportedCultures>en-US; zh-CN; </SupportedCultures>

·         添加profile选项,支持用户选择语言:

首先在MyApp.Webweb.config中添加LanguagePreference 属性

<properties>

    <add name="FriendlyName"/>

<add name="LanguagePreference" type="string" defaultValue="auto" allowAnonymous="true"/>    

 </properties>

<anonymousIdentification enabled="true"/>

然后在Models\User.cs中添加

/// <summary>

/// Gets and sets the LanguagePreference name of the user.

/// </summary>

public string LanguagePreference { get; set; }

 

Models\RegistrationData.cs中添加

/// <summary>

 /// Gets and sets the LanguagePreference name of the user.

 /// </summary>

[Display(Order = 1, Name = "LanguagePreference", Description = "LanguagePreferenceDescription", ResourceType = typeof(RegistrationDataResources))]

public string LanguagePreference { get; set; }

 

Service\UserRegisterationService.cs中的CreateUser函数中添加

profile.SetPropertyValue("LanguagePreference", user.LanguagePreference);

 

编译整个solutionsilverlight端即可引用WebContext.Current.User.LanguagePreference

打开Views\RegistrationForm.xaml,我们会看到现在的注册表单中最下面已经多了LanguagePreference这一项,文本框输入。但是最好的情况是ComboBox,我们在后台代码中的RegisterForm_AutoGeneratingField函数中的最后添加:

else if (e.PropertyName == "LanguagePreference")

{

       ComboBox languageComboBox = new ComboBox();

       languageComboBox.ItemsSource = LanguagePreferenceList;

e.Field.ReplaceTextBox(languageComboBox, ComboBox.SelectedItemProperty, binding => binding.Converter = new TargetNullValueConverter());

}

这里的LanguagePreferenceList枚举出语言类型:

public IEnumerable<CultureInfo> LanguagePreferenceList

{

            get

            {

                // TODO: Resolve the available languages

                yield return new CultureInfo("en-US");

                yield return new CultureInfo("zh-CN");

                yield return new CultureInfo("de");

            }

}

 

为了在用户登录后调用相应的语言资源,在Views\Login\LoginStatus.xmal.cs中的UpdateLoginState()授权成功后的条件中添加:

    string cultureTag = WebContext.Current.User.LanguagePreference;
            
if (!string.IsNullOrEmpty(cultureTag) && !cultureTag.Equals("auto"))
            {
                    
//UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言
                    
//Culture - 决定各种数据类型是如何组织,如数字与日期

                    Thread.CurrentThread.CurrentUICulture 
= 
                        Thread.CurrentThread.CurrentCulture 
= 
                        ApplicationStrings.Culture 
= new CultureInfo(cultureTag);
            }

             
this.welcomeText.Text = string.Format(
                    CultureInfo.CurrentUICulture,
                    ApplicationStrings.WelcomeMessage,
                    WebContext.Current.User.DisplayName);

 

 

一般来说,至此就可以完成大部分情况下对多语言的支持。不过有些情况要求用户在程序运行时切换语言,然后所有打开的页面支持多语言的界面资源立即更新。这样我们就不得不采用另外的技术:参考文章http://www.wpftutorial.net/LocalizeMarkupExtension.html

 

转载于:https://www.cnblogs.com/cubean/archive/2011/06/26/2090725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值