问一个简单的问题,请帮忙! [英] Ask a simple question, please help!

查看:148
本文介绍了问一个简单的问题,请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是C#的新手.现在,我正在本地化公司的软件产品.我了解我有两个资源文件副本.例如,一个是XXX.en-GB.resx,我默认使用它.我还有一个是XXX.fr-FR.resx.因此,首先,我将打开XXX.en-GB.resx文件,我将字符串命名为"string 1",并赋予值"hello".然后保存.然后我打开XXX.fr-FR.resx文件,我也用英语(默认)命名一个字符串"string 1",并赋予其"salut".我的问题是,是否应该替换项目中由其他人创建的所有"hello"?通过字符串1"?因为在我的两个.resx文件中,我使用字符串1"替换了该值.提前致谢.非常感谢.

Hi all,

I am a very new to C#. Now I am localizing my company''s software product. I understand I have two copy of resource file. For example, one is XXX.en-GB.resx, I use this one for default. I have another is XXX.fr-FR.resx. so the first, I will open XXX.en-GB.resx file, I name a string "string 1",give the value "hello". then save it. Then I open XXX.fr-FR.resx file, I also name a string "string 1" the same as in english (default),and give the vale "salut". My question is, should I replace all the "hello" in the the project, which is created by some else? by "string 1"? Because in my two .resx files, I use "string 1" to replace the value. Thanks in advance. Many thanks.

推荐答案

如果我理解您的问题,答案是肯定的.
实际上,您应该将所有文字字符串"hello"替换为字符串"string 1".实际上,项目中的所有文字字符串都应替换为资源文件中的字符串.

以下是一些有关本地化的文章,可能会有所帮助:
.NET-使用资源文件进行本地化 [使用RESX文件进行WPF本地化 [使用.NET Framework SDK的资源和本地化 [ ^ ]
.NET中的全球化和本地化 [
If I understand you question the answer is Yes.
You should indeed replace all literal strings "hello" with the string "string 1". In fact all literal strings in your project should be replaced with strings from the resource files.

Here are some articles on localization that might help:
.NET - Localization using Resource file[^]
WPF Localization Using RESX Files[^]
Resources and Localization Using the .NET Framework SDK[^]
Globalization and Localization in .NET[^]


如果我正确理解了, 是的.基本上,在进行本地化之前,您应该先完成 globalization .首先,这意味着应将各种字符串常量放入资源中.好吧,差不多.

首先,您需要消除所有即时常量,即硬编码的常量.即使"也没有生存权(一旦您排除在外,您的生活就会更加艰难),应使用string.Empty.显式声明的常量怎么样?您可能需要其中的一些,但只需要有限的一组:永远不会本地化的某种 atoms :标记名,连接字符串,SQL元素等.但是,上面的某些确实属于某些配置文件,例如连接字符串,文件路径名(硬编码的相对或绝对路径在任何情况下都没有用,但是将它们包含在配置文件中是没有问题的.)

对于包含类型名称或其任何成员的名称的字符串,请格外小心,例如接口名称,类名称,属性名称等.它们永远不需要,也不应该使用,因为使用它们会极大地损害维护性;如果该名称在代码中拼写错误,则编译器将显示问题,但如果字符串内容拼写错误,则不会出现此问题;最典型的是在重构后引起问题的情况.如果您不知道如何解决此问题,请在另一个问题中问我,最有可能我知道解决方案.实际上,Microsoft在ComponentModel中使用了此类内容,但这是所有.NET库中最糟糕的部分.

将所有内容移动到资源和数据文件中时,可以使用一种简单的技术:将一个或很少几个文件的所有字符串常量都使用不同的特殊名称.例如,我将所有此类文件命名为"ConfigurationSet.cs",并将这些常量放入具有相同名称的静态类中.在所有其他文件中搜索字符串.如果没有这样的符号,则全球化完成,请到了进行本地化的时间.:-)

实际上,全球化还有更多的要点,例如UI与字符串长度的独立性(或UI的容忍度),关心货币,时间的表示等,但是这些主题花费的精力少得多,而且超出了这个问题. .

最后,让我检查一下以防万一:您是否知道如何创建和使用卫星程序集后备工作原理?如果不是,请阅读这些主题,也许您正在尝试实现的某些事情已经为您完成. :-)



剩下的问题是如何使用资源.在全球化步骤中执行此操作,而忽略将来的本地化.创建资源时,请注意自动生成的C#文件,并将其作为资源文件节点的子节点放置.这是您现在的字符串常量的新源代码.看这个文件;您会发现一些带有字符串类型的静态成员的静态类;它们的名称将与您将它们添加到资源中时所给它们的资源字符串的名称相同或相似.只需在正式使用字符串常量的地方使用那些静态属性即可.无需像很多人一样错误地使用资源管理器.

(顺便说一下,这种自动生成的代码非常聪明.如果您向资源中添加文件,并且该文件恰好是PNG格式,它将被识别;并且生成的静态变量的类型将为Bitmap. ,其中已经读取了数据;无需从资源流中读取;如果VS不知道文件格式,那么您将需要它,因为它只是二进制数据的一滴.)

当您执行此操作时,全球化代码将起作用.如果以后您适当地添加了本地化的资源,附属程序集的机制将在运行时自动为您找到资源.无需更改代码.您只需要根据项目中资源的原始位置,根据区域性的缩写名称和名称空间,以观察者命名约定即可.如果您在MSDN上阅读了创建附属程序集",则将找到所有内容.我已经基于标准文档找到并使用了它,因此您也可以这样做.

从这里开始:
http://msdn.microsoft.com/en-us/library/21a15yht.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/sb6a8618.aspx [ ^ ].

请自行查找所有相关内容.

祝你好运,
—SA
If I understand you correctly, yes. Basically, before doing localization, you should complete globalization. First and foremost, it means that all kinds of string constants should be placed in resources. Well, almost.

First of all, you need to exterminate all kinds of immediate constants, hard-coded ones. Even "" does not have a right to exist (once you make an exclusion, you make you life harder), string.Empty should be used. How about explicitly declared constants? You might need some of them, but only a limited set of them: some kind of atoms which will never localize: tag names, connection strings, elements of SQL, etc. However, some of the above really belong to some configuration files, such as connection strings, file path names (there is no situation when a hard-coded relative or absolute path can be useful, but having them in configuration files is fine).

Be extra careful with strings which contain names of types or any of their members, such as interface name, class name, property name, etc. They are never needed and should not be used because using them greatly compromise maintenance; if such name is misspelled in code, compiler with show the problem, but not if the string content is misspelled; most typically if cause problems after refactorization. If you don''t know how to get around this, ask me in a separate question, most likely I know the solution. Actually, Microsoft uses such things in ComponentModel, but this is the worst part in all of .NET libraries.

When you move everything in resources and data files, you can use a simple technique: place all the string constants of one or very few files with distinctly special names. For example, I names all such files "ConfigurationSet.cs" and place the constants in the static classes of the same name. Search all other files for a string ". If there are no such symbol, you globalization is done, time to get to localization. :-)

Actually, there are some more points of globalization, such as independence of UI from the string lengths (or tolerance of UI), taking care about currency, presentation of time, etc., but these topic takes much less effort and go beyond this question.

Finally, let me check just in case: are you know how to create and work with satellite assemblies how fallback works? If not, read about those topics, maybe some things you''re trying to achieve are already done for you. :-)



It leaves the question how to use the resources. Do it in the globalization step, disregard future localization. When you create a resource, pay attention at the C# file auto-generated and placed as the child node to the resource file node. This is your new source code for the string constants now. Look at this file; you will find some static class with some static members of the string type; their names will be the same or resembling the names of the resource strings you have given them when adding them to the resource. Just use those static property where you formally used string constants. No need to use Resource Manager as many mistakenly do.

(By the way, this auto-generate code it quite clever. If you add a file to your resource, and this file will happen to be in PNG format, it will be recognized; and the generated static variable will be of the type Bitmap, with data already read in it; no need to read from resource stream; you would need it if the file format is unknown to VS, than it will be just a blob of binary data.)

When you do this your globalized code will work. If later you add localized resources properly, the mechanism of satellite assemblies will automatically find resources for you during run-time. No code change will be required. You only need to observer naming conventions, based in the abbreviated names of cultures, and namespaces, based on your original location of resources in the project. You will find it all if you read "Creating of satellite assemblies" on MSDN. I''ve found and used it just based on standard documentation, so you can do it, too.

Start here:
http://msdn.microsoft.com/en-us/library/21a15yht.aspx[^],
http://msdn.microsoft.com/en-us/library/sb6a8618.aspx[^].

Please find all what''s involve by yourself.

Good luck,
—SA


SA,

谢谢您昨天的回答.但是我有一个关于全球化的问题.如果我将每个常量都删除到源文件中,但是我应该对项目的原始副本执行什么操作.需要我声明另一个字符串(例如,我将其命名为First_Name),其名称与我声明的源文件中的名称(First_Name,value ="Tom")完全相同,然后使用此字符串名称(First_Name)替换所有的"Tom"在原始项目中?太感谢了!如果我确实初始化了字符串变量,则会出现编译器错误:未分配的局部变量.那么如何删除所有像"这样的空字符串呢?
Hi SA,

Thank you for your answer yesterday. But I have a question about globalization. If I remove every constant into source file, but what shoud I do for the original copy of project. Need I declare another string (e.g. I name it as First_Name) whose name is exactly the same as in the source file I declared (First_Name, value="Tom"), and then use this string name (First_Name) to replace all "Tom" in the original project? Thank you so much! If I did initialize the string variable, there is a compiler error: unassigned local variable. So how can I remove all the empty string like this ""?


这篇关于问一个简单的问题,请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆