我可以向。net resx文件添加非UTF-8内容吗?

时间:2021-12-24 19:40:09

I'm using resx files to store localized content for some web apps. I've been asked to write a test mechanism to know if those resource files have non UTF-8 content on the values.

我正在使用resx文件来存储一些web应用程序的本地化内容。我已经被要求编写一个测试机制,以了解这些资源文件是否具有非UTF-8的值。

The resx files are XML files that on their header specify that they are UTF-8 encoded. So my question (maybe obvious or silly) is, can somebody add non UTF-8 strings into the resource file values?

resx文件是XML文件,在它们的头上指定它们是UTF-8编码的。因此,我的问题(可能是明显的或愚蠢的)是,是否有人可以将非UTF-8字符串添加到资源文件值中?

I've already searched how to get the encoding for some string, but there is not bullet proof method, so I'm not sure if that test that I've been asked to create is either needed or possible...

我已经搜索了如何获取一些字符串的编码,但是没有防弹的方法,所以我不确定我被要求创建的测试是必需的还是可能的…

EDIT Just for clarification, the test should be performed within the localized app, and if the string is not a valid UTF-8 then display a default (localized) value

编辑只是为了澄清,测试应该在本地化应用程序中执行,如果字符串不是有效的UTF-8,则显示默认的(本地化)值。

1 个解决方案

#1


0  

To some degree, yes. You can store plain ASCII text with no difficulty.

在某种程度上,是的。您可以毫不费力地存储普通ASCII文本。

Outside of that? Your best bet is to write a test for it. Define some non-UTF8 strings in your code, store them in a .resx file (use ResxRescourceWriter for simplicity) and pull them back out. Print them to the screen and see if they match.

外的吗?最好的方法是为它编写一个测试。在代码中定义一些非utf8字符串,将它们存储在.resx文件中(简单地使用ResxRescourceWriter)并将它们拖出。把它们打印到屏幕上,看看它们是否匹配。

It's certainly possible as well to change the encoding of a resx file. If you do, you can then store strings encoded in that format.

当然也有可能改变resx文件的编码。如果你这样做了,你就可以存储以这种格式编码的字符串。

Your best bet is to try and see what happens.

最好的办法是尝试看看会发生什么。

Edit: Based on clarification within a comment.

编辑:在评论的基础上澄清。

If you're needing to verify existing values, your best bet is likely to iterate over the resx files, doing a dump of all their text to a .txt file.

如果您需要验证现有的值,那么最好的方法是迭代resx文件,将其所有文本转储到.txt文件中。

If you then open that file in Notepad++, you can specify UTF-8 encoding. Anything that doesn't display readably is not UTF-8 text and will not display correctly when pulled from the resx.

如果您在Notepad++中打开该文件,您可以指定UTF-8编码。任何无法显示的内容都不是UTF-8文本,当从resx中提取时,它不会正确显示。

#1


0  

To some degree, yes. You can store plain ASCII text with no difficulty.

在某种程度上,是的。您可以毫不费力地存储普通ASCII文本。

Outside of that? Your best bet is to write a test for it. Define some non-UTF8 strings in your code, store them in a .resx file (use ResxRescourceWriter for simplicity) and pull them back out. Print them to the screen and see if they match.

外的吗?最好的方法是为它编写一个测试。在代码中定义一些非utf8字符串,将它们存储在.resx文件中(简单地使用ResxRescourceWriter)并将它们拖出。把它们打印到屏幕上,看看它们是否匹配。

It's certainly possible as well to change the encoding of a resx file. If you do, you can then store strings encoded in that format.

当然也有可能改变resx文件的编码。如果你这样做了,你就可以存储以这种格式编码的字符串。

Your best bet is to try and see what happens.

最好的办法是尝试看看会发生什么。

Edit: Based on clarification within a comment.

编辑:在评论的基础上澄清。

If you're needing to verify existing values, your best bet is likely to iterate over the resx files, doing a dump of all their text to a .txt file.

如果您需要验证现有的值,那么最好的方法是迭代resx文件,将其所有文本转储到.txt文件中。

If you then open that file in Notepad++, you can specify UTF-8 encoding. Anything that doesn't display readably is not UTF-8 text and will not display correctly when pulled from the resx.

如果您在Notepad++中打开该文件,您可以指定UTF-8编码。任何无法显示的内容都不是UTF-8文本,当从resx中提取时,它不会正确显示。