Silverlight实现多语言

首先添加一个主资源文件Text.resx 设置生成代码 public,然后添加Text.zh-cn.resx,Text.en-us.resx等等资源文件设置不生成代码。

程序会自动生成ResourceManager类,

添加代码:

 System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-us");
            System.Threading.Thread.CurrentThread.CurrentCulture = c;// new System.Globalization.CultureInfo("zh-CN");
            System.Threading.Thread.CurrentThread.CurrentUICulture = c;// new System.Globalization.CultureInfo("zh-CN");

 设置本地化

获取资源文件中的key value

    this.txtName.Text = SL_ResourceDemo.Resource.Text.name;
            this.txtSex.Text = SL_ResourceDemo.Resource.Text.sex;

 

注意在生成的xap 文件中不包含这些资源文件的dll文件。解决方法:

 

找到silverlight工程文件打开会看到

    <ProjectGuid>{DC4A1B28-8511-491D-B209-D2A31A8501E7}</ProjectGuid>
    <ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Shareidea.Web.UI.Control.Workflow.Designer</RootNamespace>
    <AssemblyName>ShareDesigner</AssemblyName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <SilverlightApplication>true</SilverlightApplication>
    <SupportedCultures>
	en-us,zh-cn
    </SupportedCultures>
    <XapOutputs>true</XapOutputs>
    <GenerateSilverlightManifest>true</GenerateSilverlightManifest>
    <XapFilename>ShareDesigner.xap</XapFilename>
    <SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate>
    <SilverlightAppEntry>design.App</SilverlightAppEntry>
    <TestPageFileName>TestPage.html</TestPageFileName>
    <CreateTestPage>true</CreateTestPage>
    <ValidateXaml>true</ValidateXaml>
    <ThrowErrorsInValidation>false</ThrowErrorsInValidation>
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
    <FileUpgradeFlags>

 注意这里:

  <SupportedCultures>
	en-us,zh-cn
    </SupportedCultures>

 这样生成xap文件就会包括 这两个语言dll文件了。

 

可以将本地化设置放到独立存储中,下次打开的时候直接读取独立存储中的数据

 public class Configure
    {
        static System.Globalization.CultureInfo currentCulture;
        public static System.Globalization.CultureInfo CurrentCulture
        {
            get
            {

                if (currentCulture == null)
                {
                    try
                    {
                        System.IO.IsolatedStorage.IsolatedStorageSettings appSetting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                        if (appSetting.Contains("language"))
                        {
                            currentCulture = new System.Globalization.CultureInfo((string)appSetting["language"]);
                        }
                    }
                    catch (Exception e)
                    {
                    }

                }
                if (currentCulture == null)
                {
                    currentCulture = new System.Globalization.CultureInfo("en-us");

                }
                return currentCulture;
            }
            set
            {
                currentCulture = value;
                System.Threading.Thread.CurrentThread.CurrentCulture = currentCulture;
                System.Threading.Thread.CurrentThread.CurrentUICulture = currentCulture;

                try
                {
                    System.IO.IsolatedStorage.IsolatedStorageSettings appSetting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                    if (appSetting.Contains("language"))
                    {
                        appSetting["language"] = currentCulture.Name;
                    }
                    else
                    {
                        appSetting.Add("language", currentCulture.Name);
                    }

                }
                catch (Exception e)
                {
                }
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值