在WinForms中以可本地化的形式在运行时更改CurrentUICulture

时间:2017-09-01 20:32:50

标签: c# winforms currentculture

我一直在搜索如何更改Localizable属性设置为true的表单的语言。

https://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v=vs.110).aspx

这是设置表单的语言,但这需要在我们实例化表单之前设置。在此事件之后无法调用此方法。

在搜索信息时,我看到了以下问题:https://stackoverflow.com/a/11738932/3286975但是,正如评论所说,我在TabControl和MenuStrip中有控件,所以它们不受影响。

我试图通过获取表单的所有控件而没有运气来修改它。

enter image description here

enter image description here

在此菜单中,我调用以下回调:

    private void englishToolStripMenuItem_Click_1(object sender, EventArgs e)
    {
        string lang = (string) ((ToolStripMenuItem) sender).Tag;
        base.Culture = CultureInfo.CreateSpecificCulture(lang);
    }

    private void spanishToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string lang = (string) ((ToolStripMenuItem) sender).Tag;
        base.Culture = CultureInfo.CreateSpecificCulture(lang);
    }

我使用标签更改了文化。

当我点击它时没有任何反应。另外,我从上述答案中修改了一点ApplyResources方法。

private void ApplyResources(Control parent, CultureInfo culture)
{
        this.resManager.ApplyResources(parent, parent.Name, culture);

        foreach (Control ctl in parent.IterateAllChildren())
        {
            //this.ApplyResources(ctl, culture);
            this.resManager.ApplyResources(ctl, ctl.Name, culture);
        }
}

IterateAllChildren的地址如下:https://stackoverflow.com/a/16725020/3286975

另外,我尝试使用(System.LINQ):Controls.OfType<Label>()(因为我有一个标签来测试这个)没有运气......

enter image description here

enter image description here

但是当我选择西班牙语时,没有任何文字被改变。

所以也许,我对孩子们感到满意。或者也许通过调用方法CreateCulture,我不知道。

提前致谢!

修改

我已经通过文化信息测试获取表单的资源管理器,并且每次都返回默认值:

 ResourceSet resourceSet = new ResourceManager(typeof(frmCredentials)).GetResourceSet(new CultureInfo(lang), true, true);
            foreach (DictionaryEntry entry in resourceSet)
            {
                string resourceKey = entry.Key.ToString();
                object resource = entry.Value; //resourceSet.GetString(resourceKey);
                if (resource.GetType().Equals(typeof(string)))
                    Console.WriteLine("Key: {0}\nValue: {1}\n\n", resourceKey, (string) resource);
            }

new CultureInfo(lang),我还测试过:new CultureInfo("es")&amp; Thread.CurrentThread.CurrentCulture(CurrentUICulture)没有运气。就像它永远不存在或被替换,但在我的设计和文件浏览器中,我可以看到文件。

EDIT2:

也许是因为我正在使用ILMerge将所有dll合并为一个唯一的dll。我正在审核这个:Single-assembly multi-language Windows Forms deployment (ILMerge and satellite assemblies / localization) - possible?

回复EDIT2:

是的,删除ILMerge问题解决了,我给出的第一个解决方案解决了这个问题。但由于某种原因,西班牙语被视为默认语言,当我尝试从中获取资源集时,它并没有给我任何回报。

Aso,我已将Localizable属性设置为false,并且它没有创建带有值的默认resx文件。我不知道这是不是一个好习惯。

我会尝试新的东西......

3 个答案:

答案 0 :(得分:2)

MVVM(模型 - 视图 - ViewModel)方法有一些好处,可以在您的情况下使用。

为将用于本地化的语言创建新的资源文件。使用表单自己的资源文件可能很少出价,因为每次在设计器中进行更改时它都会重新生成 - 所以我认为自己的资源文件将更容易维护,甚至可以与其他表单甚至项目共享。

LocalizationValues.resx // (default english), set Access Modifier to "Internal" or "Public"
    "Title": "Title"
    "Description": "Description"

LocalizationValues.es.resx
    "Title": "Título"
    "Description": "Descripción"

Visual Studio生成静态类LocalizationValues,其属性为.resx文件的键。所以&#34;标题&#34;可以LocalizationValues.Title

访问

创建&#34; viewmodel&#34;表示您在本地化中使用的所有文本的类。类应该实现INotifyPropertyChanged接口。

public class LocalizationViewModel : INotifyPropertyChanged
{
    public string Title
    {
        get
        {
            return LocalizationValues.Title;
        }
    }

    public string Description
    {
        get
        {
            return LocalizationValues.Description;
        }
    }

    public void SetLanguage(string language)
    {
        var culture = new CultureInfo(language);
        Thread.CurrentThread.CurrentUICulture = culture;

        // This is important, 
        // By raising PropertyChanged you notify Form to update bounded controls
        NotifyAllPropertyChanged();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyAllPropertyChanged()
    {
        // Passing empty string as propertyName
        // will indicate that all properties of viewmodel have changed
        NotifyPropertyChanged(string.Empty);
    }

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后在Form中将viewmodel绑定到控件

public partial class YourForm : Form
{
    private LocalizationViewModel _viewmodel;

    public YourForm()
    {
        InitializeComponent();

        _viewmodel = new LocalizationViewModel();

        // Bound controls to correspondent viewmodel's properties
        LblTitle.DataBindings.Add("Text", _viewmodel, "Title", true);
        LblDescription.DataBindings.Add("Text", _viewmodel, "Description", true);
    }

    // Menu buttons to change language
    private void SpanishToolStripMenuItem_Click(object sender, EventArgs e)
    {
        _viewmodel.SetLanguage("es");
    }

    private void EnglishToolStripMenuItem_Click(object sender, EventArgs e)
    {
        _viewmodel.SetLanguage("en");
    }
}

上面的方法将提供更多的好处,然后只更新控件。您可以清楚地分离应用程序的各个部分,这些部分可以相互独立地进行测试。

答案 1 :(得分:1)

有解决问题的不同解决方案,包括其他答案中提到的MVVM。但你也可以考虑其他一些选择。

在所有控件上调用ApplyResource

您可以在所有控件上设置当前线程的currunt UI文化和调用ApplyResource。为此,您需要创建一个方法来返回所有控件,然后只需在所有控件上调用ApplyResource,例如:

private void englishToolStripMenuItem_Click(object sender, EventArgs e)
{
    SetCulture("en-US");
}
private void persianToolStripMenuItem_Click(object sender, EventArgs e)
{
    SetCulture("fa-IR");
}
public void SetCulture(string cultureName)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture =
       System.Globalization.CultureInfo.GetCultureInfo(cultureName);
    var resources = new System.ComponentModel.ComponentResourceManager(this.GetType());
    GetChildren(this).ToList().ForEach(c => {
        resources.ApplyResources(c, c.Name);
    });
}
public IEnumerable<Control> GetChildren(Control control)
{
    var controls = control.Controls.Cast<Control>();
    return controls.SelectMany(ctrl => GetChildren(ctrl)).Concat(controls);
}

创建文本本地化扩展程序组件

您还可以创建一个可在设计时和运行时使用的扩展程序组件,并为控件分配一些资源文件和资源键。这样,您可以通过更改当前线程的当前UI文化来简单地在语言之间切换。只是看一个想法的例子,看看这篇文章:

答案 2 :(得分:0)

经过一些尝试,我意识到有几件事情失败了。

我还要说,非常感谢这个问题中给出的所有帮助,以及LocalizedForm snippet非常有用。

第一个问题是我已经意识到,使用此解决方案的子层次结构中的控制级别超过1的控件不起作用。

迭代所有控件是一项昂贵的任务。也许我的情况最糟糕,但迭代次数较少(因为我只使用文本搜索控件)

    /// <summary>
    /// Current culture of this form
    /// </summary>
    [Browsable(false)]
    [Description("Current culture of this form")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public CultureInfo Culture
    {
        get { return this.culture; }
        set
        {
            if (this.culture != value)
            {
                ResourceSet resourceSet = new ComponentResourceManager(GetType()).GetResourceSet(value, true, true);
                IEnumerable<DictionaryEntry> entries = resourceSet
                    .Cast<DictionaryEntry>()
                    .Where(x => x.Key.ToString().Contains(".Text"))
                    .Select(x => { x.Key = x.Key.ToString().Replace(">", "").Split('.')[0]; return x; });

                foreach (DictionaryEntry entry in entries)
                {
                    if (!entry.Value.GetType().Equals(typeof(string))) return;

                    string Key = entry.Key.ToString(),
                           Value = (string) entry.Value;

                    try
                    {
                        Control c = Controls.Find(Key, true).SingleOrDefault();
                        c.Text = Value;
                    }
                    catch
                    {
                        Console.WriteLine("Control {0} is null in form {1}!", Key, GetType().Name);
                    }
                }

                this.culture = value;
                this.OnCultureChanged();
            }
        }
    }

我的工作如下,首先,我搜索 ResourceManager ,注意!因为这是第二个问题,如果您在某些情况下使用CultureInfo.CreateSpecificCulture代替CultureInfo.GetCultureInfo,则会创建新文化并返回默认值(Form1.resx而不是{{1}值(例如))。

我们从 resx文件加载了所有值后,我们迭代所有这些值,然后删除double&gt;&gt; (在某些情况下会显示)并且我们得到那些仅声明文本属性控件的名称。

下一步是找到控件并替换其文本...

好吧,我对派生类有点混乱,这就是我创建 try-catch系统的原因,因为控件。在所有派生类中查找搜索我希望更具体一点,但我不知道如何...(这就是我创建this question的原因)

有了这个,我们无法保存任何对象,因为我们不会清除并重新创建它们。

这里的主要问题不是我这样做的方式,因为它是正确的。 问题在于,当您调用此示例时程序集合并做了奇怪的事情

Form1.es.resx

ResourceSet resourceSet = new ComponentResourceManager(GetType()).GetResourceSet(value, true, true);将是默认设置......这样的文化不存在,因为合并的程序集可以&找不到resx文件。

所以,我会尝试@ScottChamberlain向我建议的value。或者 ILRepack

对于为什么这不起作用的任何优化或想法(在评论中)的帮助将不胜感激!