使用资源文件本地化

发布于 2024-10-20 09:57:08 字数 211 浏览 2 评论 0原文

我使用 *.resx 文件进行本地化。名称 - 它是英语中的一个短语或单词。值 - 该短语到另一种语言的翻译。我选择这种方法是为了为整个应用程序提供一个本地化文件。任何拥有此文件的人都可以自行翻译。

但在 Visual Studio 2010 resx 编辑器中,名称中包含空格的每条记录都会出现警告:“资源名称不是有效的标识符”。 虽然它可以编译并运行,但是请告诉我我在这里是否做错了什么。

I use a *.resx file for the localization purpose. The Name - it's a phrase or word in English. The Value - the translation to another language of that phrase. I choose this approach to have a one localization file for the whole application. And anyone who have this file can make translation by themselves.

But in the Visual Studio 2010 resx editor, each record with name which have spaces in it, have a warning: "The resource name is not a valid identifier."
Though it compiles and works, but please tell me if I am doing something wrong here.

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

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

发布评论

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

评论(3

满天都是小星星 2024-10-27 09:57:09

首先,resx 文件的想法是为每种文化提供一个单独的资源文件。您可以通过为相同键创建具有不同值的新文件来提供新翻译。

例如,您可以创建 Forms.en-GB.resx、Forms.pl-PL.resx、Forms.de-DE.resx,并且将根据当前 UI 区域性选择适当的文件,而无需您执行任何操作 (除了确保设置相关文化之外)。

Visual Studio 将生成一个资源类,其中包含资源文件中的所有键/值对作为属性 - 这使得在代码中使用更容易。您收到的警告意味着您在资源文件中提供的键不是有效的标识符(它们包含空格)。您可能想在按键中使用 _ 而不是空格。

如果您不想使用生成的类,您可以忽略此警告 - 您的 resx 文件很好,可以直接使用。您可以从 resx 文件(属性窗口)的“自定义工具”属性中删除 ResXFileCodeGenerator,或者如果不需要生成类,则可以在 resx 文件编辑器中将“访问修饰符”设置为“无代码生成” ,但您仍然会收到警告。

First of all, the idea of the resx files is to have a separate resource file for each culture. You can provide the new translation by creating a new file with different values for the same keys.

For example, you can create Forms.en-GB.resx, Forms.pl-PL.resx, Forms.de-DE.resx and the appropriate file will be picked up based on the current UI culture without you having to do anything (except ensuring relevant culture is set).

Visual Studio will generate a resource class that contains all your key/value pairs from resource file as properties - that makes it easier to use in code. The warning you get means that the keys you've provided in resource file are not a valid identifiers (they contain spaces). You might want to use _ instead of space in the keys.

If you don't want to use the generated class you can ignore this warning - your resx files are fine and can be used directly. You can remove ResXFileCodeGenerator from 'Custom Tool' property of your resx file (properties windows) or set 'Access Modifier' to 'No code generation' in resx file editor if you do not need to generate a class, but you will still get the warning.

柠檬色的秋千 2024-10-27 09:57:09

使用 resx 文件进行本地化的优势在于,您计算机上的文化决定了您的应用程序应使用哪种语言。根据我的说法,如果您只使用一个 resx 文件,您就会忽略它的威力。相反,尝试为您想要集成的每种语言创建一个 resx 文件。例如:默认语言是英语,那么您有一个默认页面localization.resx,其中只保留英语句子。假设您需要法语翻译,请创建另一个名为 localization.fr-FR.resx 的 resx 文件。因此,在计算机上启用 fr-FR 文化的用户将在程序中使用该语言,而无需任何特定于代码的工作。如果具有未集成到您的应用程序中的文化的人启动该程序,它将寻找它,如果没有找到它,它会选择默认值,即英语。

所以我认为,不要对不同的语言使用 1 个 resx 文件,而是使用框架中给出的权力。

The strength of localization with resx files is that the culture on your computer decides what language your application should be in. If you keep to one resx file, according to me, you ignore it's power. Instead, try making a resx file for each language you want to integrate. for example: the default language is english, then you have a default page localization.resx where you only keep english sentences. Say you need a French translation, make another resx file called localization.fr-FR.resx. So users who have the fr-FR culture enabled on there computer will have that language on the program without any code specific work. If someone with a culture not integrated in your application starts the program, it will look for it, and if it doesn't find it, it chooses the default, ie english, one.

So to my opinion, don't use 1 resx file for different languages, but use the powers given in the framework.

不必了 2024-10-27 09:57:09

我认为它有效,但这并不是您应该遵循的策略。

请查看此处;基本思想是,您利用 .NET 中的控件自动获得正确的本地化本身,因此您无需担心进行翻译。

不过,我并不总是使用这个,而且我做了一些像你一样的事情,但我倾向于使用标识符,所以我可能会:

UserWelcome  Hey, {name}, thanks for dropping by ...

然后我会翻译它。它很有帮助,因为它允许语言的通用性(例如,某些语言应该正式问候,而另一些则不需要,您不想受到“您”的直接翻译的限制)。

希望这一点是清楚的。

如果你所拥有的有效,那么我想这确实是可行的,但这不是“通用”的做法。

I suppose it works, but it's not really the strategy you are supposed to follow.

Take a look here; the basic idea is that you take advantage of the controls in .NET to automatically get the correct localisation themselves, so you kind of don't need to worry about doing the translating.

Though, I don't use this all the time, and I do somewhat do as you do, but I tend to use an identifier, so I may have:

UserWelcome  Hey, {name}, thanks for dropping by ...

And then I'll translate that. It's helpful because it allows generality in the languages (say, for example, some languages should be greeted formally, and others not, you don't want to be contrained by a direct translation of, "You", say).

Hope this is clear.

If what you've got works, then I suppose that's something, but it's not the "general" way of doing it.

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