如何在运行时更改 WinForms 应用程序的区域性

发布于 2024-12-06 13:14:44 字数 531 浏览 0 评论 0原文

我用 C# 创建了 Windows 窗体程序。我在本地化方面遇到一些问题。我有两种语言的资源文件(一种是英语,另一种是法语)。我想单击每个语言按钮并在运行时更改语言。

但是当我点击按钮时,它不起作用。我正在使用这个代码。

private void btnfrench_Click(object sender, EventArgs e)
{
    getlanguage("fr-FR");
}

private void getlanguage(string lan)
{
    foreach (Control c in this.Controls)
    {
        ComponentResourceManager cmp = 
            new ComponentResourceManager(typeof(BanksForm));
        cmp.ApplyResources(c, c.Name, new CultureInfo(lan));
    }
}

请帮忙解决这个问题吗……

非常感谢……

I have created Windows Form Program in C#. I have some problems with localization. I have resource files in 2 languages(one is for english and another is for french). I want to click each language button and change language at runtime.

But when i am clicking on button, it doesn't work. i am using this code.

private void btnfrench_Click(object sender, EventArgs e)
{
    getlanguage("fr-FR");
}

private void getlanguage(string lan)
{
    foreach (Control c in this.Controls)
    {
        ComponentResourceManager cmp = 
            new ComponentResourceManager(typeof(BanksForm));
        cmp.ApplyResources(c, c.Name, new CultureInfo(lan));
    }
}

would any pls help on this......

Many Thanks....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒冷纷飞旳雪 2024-12-13 13:14:44

这很有效:

private void button1_Click(object sender, EventArgs e)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-BE");
    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
    resources.ApplyResources(this, "$this");
    applyResources(resources, this.Controls);
}

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

小心避免添加这样没人会使用的口哨。它充其量只是一个演示功能,实际上用户不会即时更改其母语。

This worked:

private void button1_Click(object sender, EventArgs e)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-BE");
    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
    resources.ApplyResources(this, "$this");
    applyResources(resources, this.Controls);
}

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

Be careful to avoid adding whistles like this that nobody will ever use. It at best is a demo feature, in practice users don't change their native language on-the-fly.

没︽人懂的悲伤 2024-12-13 13:14:44

您可能必须在控件上递归调用ApplyResources:

private void btnfrench_Click(object sender, EventArgs e)
{
    ApplyResourceToControl(
        this, 
        new ComponentResourceManager(typeof(BanksForm)), 
        new CultureInfo("fr-FR"))
}

private void ApplyResourceToControl(
   Control control, 
   ComponentResourceManager cmp, 
   CultureInfo cultureInfo)
{
    cmp.ApplyResources(control, control.Name, cultureInfo);

    foreach (Control child in control.Controls)
    {
        ApplyResourceToControl(child, cmp, cultureInfo);
    }
}

You might have to call ApplyResources recursively on the controls:

private void btnfrench_Click(object sender, EventArgs e)
{
    ApplyResourceToControl(
        this, 
        new ComponentResourceManager(typeof(BanksForm)), 
        new CultureInfo("fr-FR"))
}

private void ApplyResourceToControl(
   Control control, 
   ComponentResourceManager cmp, 
   CultureInfo cultureInfo)
{
    cmp.ApplyResources(control, control.Name, cultureInfo);

    foreach (Control child in control.Controls)
    {
        ApplyResourceToControl(child, cmp, cultureInfo);
    }
}
花桑 2024-12-13 13:14:44

在运行时更新 CultureInfo 可能会重置组件大小。此代码保留控件的大小和位置
(不过仍然会有可见的闪烁,使用 SuspendLayout() 无法修复)


    private void langItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        //I store the language codes in the Tag field of list items
        var itemClicked = e.ClickedItem;
        string culture = itemClicked.Tag.ToString().ToLower();

        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
        ApplyResourceToControl(
        this,
        new ComponentResourceManager(typeof(GUI)),
        new CultureInfo(culture));           
    }

    private void ApplyResourceToControl(
       Control control,
       ComponentResourceManager cmp,
       CultureInfo cultureInfo)
    {
        foreach (Control child in control.Controls)
        {
            //Store current position and size of the control
            var childSize = child.Size;
            var childLoc = child.Location;
            //Apply CultureInfo to child control
            ApplyResourceToControl(child, cmp, cultureInfo);
            //Restore position and size
            child.Location = childLoc;
            child.Size = childSize;
        }
        //Do the same with the parent control
        var parentSize = control.Size;
        var parentLoc = control.Location;
        cmp.ApplyResources(control, control.Name, cultureInfo);
        control.Location = parentLoc;
        control.Size = parentSize;
    }

Updating the CultureInfo at runtime might reset component size. This code preserves the size and position of the controls
(there will still be visible flickering though, which using SuspendLayout() couldn't fix)


    private void langItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        //I store the language codes in the Tag field of list items
        var itemClicked = e.ClickedItem;
        string culture = itemClicked.Tag.ToString().ToLower();

        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
        ApplyResourceToControl(
        this,
        new ComponentResourceManager(typeof(GUI)),
        new CultureInfo(culture));           
    }

    private void ApplyResourceToControl(
       Control control,
       ComponentResourceManager cmp,
       CultureInfo cultureInfo)
    {
        foreach (Control child in control.Controls)
        {
            //Store current position and size of the control
            var childSize = child.Size;
            var childLoc = child.Location;
            //Apply CultureInfo to child control
            ApplyResourceToControl(child, cmp, cultureInfo);
            //Restore position and size
            child.Location = childLoc;
            child.Size = childSize;
        }
        //Do the same with the parent control
        var parentSize = control.Size;
        var parentLoc = control.Location;
        cmp.ApplyResources(control, control.Name, cultureInfo);
        control.Location = parentLoc;
        control.Size = parentSize;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文