2011-10-03 70 views
4

是否有可能从未设置为当前用户文化的资源文件获取值?我们的应用程序是基于数据的,而不是基于文化的。从resx手动检索资源值?

例如一份文件是一份法文文件,需要更新具体的标签和栏位并用新的数据取代。

是否可以指示资源管理器使用法语.resx而不是默认的.resx?

回答

2

可以设置typeto标签文本的语言在其中一种形式中,然后选择要显示给最终用户的语言与使用该标签文本IE的语言进行比较,if标签文本是法国人,那么你可以展示在法国

注意你的所有控件名称:你创造了法国的RESX文件,手动重写所有的标签和按钮控件的名字在法语名称有价值的东西等之后,其仅适用这个..

Name    value 
-----------  ------------- 
lblname.text  frenchtype name 


using System; 
using System.IO; 
using System.Linq; 
using System.Data; 
using System.Text; 
using System.Diagnostics; 
using System.Windows.Forms; 
using System.Collections.Generic; 
using System.ComponentModel; 

public partial class Form1 : Form 
{ 
    public form1() 
    { 
    System.Threading.Thread.CurrentThread.CurrentUICulture = new 
System.Globalization.CultureInfo("fr-FR"); 
    getlanguagaefile(); 
    InitializeComponent(); 
    } 

// blah 
// blah 

private void getlanguagaefile() 
{ 
    if (label1.Text == "French") 
    { 
     System.Threading.Thread.CurrentThread.CurrentUICulture = new 
    System.Globalization.CultureInfo("fr-FR"); 
     ComponentResourceManager resources = new ComponentResourceManager(typeof(Wait)); 
     resources.ApplyResources(this, "$this"); 
     applyResources(resources, this.Controls); 

    } 
    } 

可以显示法语所有标签文本和文本按钮加载窗体时

private void applyResources(ComponentResourceManager resources, Control.ControlCollection controlCollection) 
    { 
    foreach (Control ctl in controlCollection) 
    { 
     resources.ApplyResources(ctl, ctl.Name); 
     applyResources(resources, ctl.Controls); 
    } 
    } 
} 
3

可以使用ResourceManager.GetString()方法和provi以期望的文化作为参数:

var culture = CultureInfo.CreateSpecificCulture("fr-FR"); 
var localizedString = ResourceManager.GetString("labelName", culture); 
+0

对于未来的读者,要做到这一点,您必须根据您的资源创建一个新的'ResourceManager'实例。类似于'ResourceManager rm = new ResourceManager(typeof(YourProject.Properties.Resources))',然后在新实例上调用'GetString'。 – TheWanderingMind