简体   繁体   English

ResourceManager.GetString使用错误的编码进行本地化

[英]ResourceManager.GetString gives localization with wrong encoding

I use ResourceManager to localize strings in my web forms application. 我使用ResourceManager在我的Web窗体应用程序中本地化字符串。 The application supports en-us and sv-se . 该应用程序支持en-ussv-se The strings are fetched correctly from the respective resource files, but when they are displayed, unicode characters come as junk values. 可以从相应的资源文件中正确提取字符串,但是在显示字符串时,unicode字符会成为垃圾值。

resources.sv-se.restext

...
ContactInfoFormatPrimary={0} (primär)
...

Output 产量

在此处输入图片说明

Code that gets the resource: 获取资源的代码:

ResourceManager resourceManager  = GetResourceManager(...)
...
text = resourceManager.GetString(resourceId);

I tried debugging the code, and the text returned is {0} (prim r) 我尝试调试代码,返回的文本为{0} (prim r)

What is causing the resource to decode incorrectly? 是什么导致资源解码错误?

UPDATE: 更新:

I was able to use the same architecture in a console app and obtain the correct string, is there anything related to application configuration that i need to take care of? 我能够在控制台应用程序中使用相同的体系结构并获取正确的字符串,我需要注意与应用程序配置相关的任何事情吗?

Most likely you are not saving your resource text files ( .txt , .restext ) as UTF-8 or UTF-16 / Unicode . 很可能您没有将资源文本文件( .txt.restext )保存为UTF-8UTF-16 / Unicode See the following paragraph from the MSDN documentation of Resgen.exe : 请参阅Resgen.exeMSDN文档中的以下段落:

A text file that contains resources must be saved with UTF-8 or Unicode (UTF-16) encoding unless it contains only characters in the Basic Latin range (to U+007F). 包含资源的文本文件必须以UTF-8或Unicode(UTF-16)编码保存,除非它仅包含基本拉丁语范围(至U + 007F)中的字符。 Resgen.exe removes extended ANSI characters when it processes a text file that is saved using ANSI encoding. Resgen.exe处理使用ANSI编码保存的文本文件时,将删除扩展的ANSI字符。

The easiest way to check the enconding of a text file is to open it in Notepad , click Save as... in the File menu and check what's displayed in the Encoding drop down in the lower right corner. 检查文本文件最简单的方法是在Notepad中将其打开,在“ File菜单中单击“ Save as... ,然后检查右下角的“ Encoding下拉菜单中显示的内容。 It should be set to UTF-8 or Unicode . 应该将其设置为UTF-8Unicode

To fix the encoding just select UTF-8 or Unicode in the Save as... dialog of Notepad and hit Save overwriting the original file. 修复编码,只需在Notepad的“ Save as...对话框中选择UTF-8Unicode ,然后单击“ Save覆盖原始文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM