• 使用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

     

  • 相关阅读:
    基于python+django+mysql的接口测试平台
    firefox没有装在C盘,webdriver启动firefox时报错
    Python知识点面试题
    Python面试题整理
    Python程序猿面试杂谈
    Python面试-websocket及web框架
    Python面试-DB相关
    Python面试简介及并行并发
    flink-demo2
    flink-table demo
  • 原文地址:https://www.cnblogs.com/cubean/p/2090725.html
Copyright © 2020-2023  润新知