如何将 C# .NET 应用程序转换为支持多种语言?

发布于 2024-12-19 23:14:12 字数 70 浏览 1 评论 0原文

我有一个 C# 应用程序,需要将其转换为支持英语和西班牙语,是否有一种半简单的方法可以添加该应用程序并能够在以后添加其他语言?

I have a C# application that I need to convert to support English and Spanish, is there a semi easy way to add that in and be able to add other languages later on?

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

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

发布评论

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

评论(4

痞味浪人 2024-12-26 23:14:12

是的!它称为资源 (.resx) 文件。您要做的就是:

  1. 将可本地化表单的 Localized 属性更改为 true。这将使设计人员从 .resx 文件中获取文本和其他属性,而不是对它们进行硬编码。
  2. 用一种语言创建程序,比如说英语。
  3. 接下来,将所有表单更改为另一种语言,如下所示:
    1. 将表单的 Language 属性更改为其他语言,例如西班牙语。
    2. 更改所有控件上的文本。设计器将自动为该语言生成一个新的 .resx 文件。
    3. 在开发过程中根据需要来回交换。
  4. 发布时,进入程序集设置并更改语言。我认为您还可以更改代码中的语言。

瞧!你完成了!

Yes! It's called resource (.resx) files. What you do is this:

  1. Change the Localizable property of your localizable forms to true. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.
  2. Create your program in one language, let's say English.
  3. Next, change all your forms to another language like so:
    1. Change the Language property of the form to the other language, let's say Spanish.
    2. Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
    3. Swap back and forth as needed during development.
  4. When publishing, go into your Assembly Settings and change the language. You can also change the language in code, I think.

And voilà! You're done!

猥︴琐丶欲为 2024-12-26 23:14:12

您将所有表单和控件标记为可本地化。这会将所有 UI 相关文本(标签等)放入资源文件中。如果您需要在代码中创建字符串,则可以使用字符串资源文件并通过资源键查找字符串(例如StringResource.Get("My_Message"))。然后你可以使用一个工具来翻译你的所有资源。通常,您为每种语言创建一个本地化的 .dll。我们使用 Passolo 来完成此任务,但还有其他工具。

You mark all your forms and controls as localizable. This will put all UI related text (labels etc.) in resource files. If you need to create strings in code then you use string resource files and look up the string by the resource key (e.g StringResource.Get("My_Message")). Then you can use a tool to translate all your resources. Typically you create a localized .dll for each language. We use Passolo for that but there are other tools around.

时光暖心i 2024-12-26 23:14:12

您可以通过两种方式制作多语言应用程序:

  1. 通过使应用程序可本地化,因此当用户更改设备的文化时,如果您已将此语言添加到支持的语言中,应用程序将自动切换到文化的 UI应用。
    您可以通过将项目上每个表单的 Localized 属性设置为 Localizable,然后将 UI 更改为新的区域性来执行此操作。

  2. 为应用程序中添加的每种语言创建语言选项和资源文件 (.resx),并根据所选语言,您可以从所选语言的资源文件中加载图像或字符串。

You can make a multilingual application in two ways:

  1. By making the application Localizable, so when the user changes the culture of the device, the application will switch automatically to culture's UI if you added this language already to the supported languages in the application.
    You can perform this by setting each form's Localizable property on the project to Localizable, and then changing the UI to the new culture.

  2. By making a language option and a resource file (.resx) for each added language in your application, and depending on the selected language, you can load the images or the strings from selected language's resource file.

小嗲 2024-12-26 23:14:12

无需安装任何第 3 方工具、API 或 dll 对象,我就可以使用 App_LocalResources。尽管我仍然使用谷歌翻译来翻译单词和句子,并将其复制并粘贴到文件中,如下面的屏幕截图之一所示(或者您可以找个人翻译并手动输入来添加)。在您的项目文件夹(使用 MS Visual Studio 作为编辑器)中,添加 App_LocalResources 文件夹并创建英语和其他语言(resx 文件)。就我而言,它是西班牙语 (es-ES) 翻译。请参阅下面的屏幕截图。
输入图片此处描述

接下来,在您的 aspx 上,添加将在 App_LocalResources 中匹配的元标记 (meta:resourcekey)。一个用于英语文件,另一个用于西班牙语文件。请参阅下面的屏幕截图:

在此处输入图像描述

西班牙语:(filename.aspx.es-ES.resx)

在此处输入图像描述

英语:(filename.aspx.resx)

然后使用查询字符串在母版页文件上创建一个链接,该链接将切换页面翻译并将在所有页面上可用:

<%--ENGLISH/SPANISH VERSION BUTTON--%>
<asp:HyperLink ID="eng_ver" runat="server" Text="English" Font-Underline="false"></asp:HyperLink> | 
<asp:HyperLink ID="spa_ver" runat="server" Text="Español" Font-Underline="false"></asp:HyperLink>
<%--ENGLISH/SPANISH VERSION BUTTON--%>

在母版页代码后面,创建一个指向超链接标记的动态链接:

////LOCALIZATION
string thispage = Request.Url.AbsolutePath;
eng_ver.NavigateUrl = thispage;
spa_ver.NavigateUrl = thispage + "?ver=es-ES";
////LOCALIZATION

现在,在页面文件的代码后面,您可以设置一个会话变量,通过始终向网址添加查询字符串,使所有链接或重定向坚持所需的翻译。

在页面加载时:

        ///'LOCALIZATION
        //dynamic querystring; add this to urls ---> ?" + Session["add2url"]
        {
            if (Session["version"] != null)
            {
                Session["add2url"] = "?ver=" + Session["version"]; //SPANISH version
            }
            else
            {
                Session["add2url"] = ""; // ENGLISH as default
            }
        }
        ///'LOCALIZATION

.
单击事件示例:

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["FileName.aspx"] = null;
        Response.Redirect("FileName.aspx" + Session["add2url"]);
    }

我希望我的描述足够简单。

Without installing any 3rd party tool, APIs, or dll objects, I am able to utilize the App_LocalResources. Although I still use Google Translate for the words and sentences to be translated and copy and paste it to the file as you can see in one of the screenshots below (or you can have a person translator and type manually to add). In your Project folder (using MS Visual Studio as editor), add an App_LocalResources folder and create the English and other language (resx file). In my case, it's Spanish (es-ES) translation. See screenshot below.
enter image description here

Next, on your aspx, add the meta tags (meta:resourcekey) that will match in the App_LocalResources. One for English and another to the Spanish file. See screenshots below:

enter image description here

Spanish: (filename.aspx.es-ES.resx)

enter image description here

English: (filename.aspx.resx)

enter image description here

.

Then create a link on your masterpage file with a querystring that will switch the page translation and will be available on all pages:

<%--ENGLISH/SPANISH VERSION BUTTON--%>
<asp:HyperLink ID="eng_ver" runat="server" Text="English" Font-Underline="false"></asp:HyperLink> | 
<asp:HyperLink ID="spa_ver" runat="server" Text="Español" Font-Underline="false"></asp:HyperLink>
<%--ENGLISH/SPANISH VERSION BUTTON--%>

.

On your masterpage code behind, create a dynamic link to the Hyperlink tags:

////LOCALIZATION
string thispage = Request.Url.AbsolutePath;
eng_ver.NavigateUrl = thispage;
spa_ver.NavigateUrl = thispage + "?ver=es-ES";
////LOCALIZATION

.

Now, on your page files' code behind, you can set a session variable to make all links or redirections to stick to the desired translation by always adding a querystring to urls.

On PageLoad:

        ///'LOCALIZATION
        //dynamic querystring; add this to urls ---> ?" + Session["add2url"]
        {
            if (Session["version"] != null)
            {
                Session["add2url"] = "?ver=" + Session["version"]; //SPANISH version
            }
            else
            {
                Session["add2url"] = ""; // ENGLISH as default
            }
        }
        ///'LOCALIZATION

.
On Click Events sample:

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["FileName.aspx"] = null;
        Response.Redirect("FileName.aspx" + Session["add2url"]);
    }

I hope my descriptions were easy enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文