【问题标题】:localization without re-compiling project本地化无需重新编译项目
【发布时间】:2013-07-31 11:48:43
【问题描述】:

我正在构建一个项目,我需要使其非常可定制。我正在尝试构建它以支持 4 种语言。用户将拥有一个管理面板,他/她可以在其中更改标签的文本或按钮的文本。我希望用户无需致电我即可转到该管理面板并更改按钮的文本:)

我使用过旧但很好的本地化风格,即 .resx 文件。我有下面的示例代码。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (comboBox1.SelectedItem.ToString().Equals("en-GB"))
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB");
            label1.Text= FormLabels.test1;
            label2.Text = FormLabels.test2;
        }
        else if (comboBox1.SelectedItem.ToString().Equals("de-DE"))
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
            label1.Text = FormLabels.test1;
            label2.Text = FormLabels.test2;
        }
     }

如果我让用户更改“FormLabels.en-GB.resx”文件中的按钮文本。必须重新编译项目才能看到更改。

我需要找到一个解决方案,让用户可以通过重新编译来更改按钮的文本。我该怎么做?

【问题讨论】:

标签: c# xml winforms localization


【解决方案1】:

我唯一能想到的就是在外部文件中进行本地化。 创建一个 xml 文件,如:

例如:languagesSupported.xml

    <Languages>
        <language name="English" file="en.dat" />
        <language name="French" file="fr.dat" />
        <language name="Japanese" file="jp.dat" />
    </Languages>

这样您以后实际上可以添加更多语言。

现在在每个文件中,您需要执行以下操作:

(例如:en.dat)

    <Language name="English">
        <Localized name="hello" value="Hello">
        <Localized name="goodbye" value="Goodbye">
    </Language>

(例如:fr.dat)

    <Language name="French">
        <Localized name="hello" value="Bonjour">
        <Localized name="goodbye" value="Au revoir">
    </Language>

在你的代码中你会做这样的事情:

    private Dictionary<string, Dictionary<string, string>> _localizations = new Dictionary<string, Dictionary<string, string>>();

    private string _currentLocalization = "English";

    private bool LoadLocalizations()
    {
        try
        {
            if (File.Exists("languagesSupported.xml") == false)
            {
                return false;
            }

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("languagesSupported.xml");
            XmlNodeList nodeList = xmldoc.SelectNodes("languages/language");

            if (nodeList.Count > 0)
            {
                foreach (XmlNode node in nodeList)
                {
                    LoadLocalization(node.Attributes["name"].Value, node.Attributes["file"].Value);
                }
            }

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    private bool LoadLocalization(string pLang, string pFile)
    {
        try
        {
            if (File.Exists(pFile) == false)
            {
                return false;
            }

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(pFile);
            XmlNodeList nodeList = xmldoc.SelectNodes("language/localized");

            _localizations.Add(pLang, new Dictionary<string,string>());

            if (nodeList.Count > 0)
            {
                foreach (XmlNode node in nodeList)
                {
                    _localizations[pLang].Add(node.Attributes["name"].Value, node.Attributes["value"].Value);
                }
            }

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    private void SetLocalization()
    {
        labelHello.text = _localizations[_currentLocalization]["hello"];
        labelGoodbye.text = _localizations[_currentLocalization]["goodbye"];
    }

之后,每次您的用户更改语言时,您只需更新 _currentLocalization 并调用 SetLocalization();

您甚至可以使用 _localizations 中的键填充您的语言下拉列表。

这样您就可以使本地化完全动态化。

如果您真的想使用 CultureInfo,只需将文化映射到语言名称即可。

【讨论】:

    【解决方案2】:

    ResourceManager 仅适用于嵌入式资源。 您必须自己编写 ResourceManager。

    您可以使用 ResXResouceSet (http://msdn.microsoft.com/en-us/library/system.resources.resxresourceset.aspx),然后围绕它编写自己的 ResourceManager。 这并不难做到。 然后,您可以自己阅读 resx 文件并在 resx 扩展之前添加文化。

    祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 2022-09-25
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多