.NET 本地化

发布于 2024-12-02 10:41:22 字数 1080 浏览 5 评论 0原文

我正在开发一个支持多个区域设置的应用程序。

这是通过一个单独的 dll 来完成的,该 dll 具有每种语言的资源,每个资源都存储字符串到特定语言的翻译。

这在 WPF 应用程序上运行得很好,每次应用程序需要字符串时,都会向 dll 发出请求,并根据应用程序上选择的语言返回文本。如果语言发生更改,语言 dll 返回的文本会反映更改。

现在我正在 ASP.Net 中使用 MVC 创建一个网站,该网站使用相同的语言 dll。

问题是,据我所知,语言 dll 上的资源管理器总是返回系统文化的文本。我更改了资源管理器使用的文化信息,但它不起作用。

有谁知道我该如何解决这个问题? 谢谢

更新: 抱歉回复晚了。周末无法访问互联网...这是我第一次有一个项目在 Web 和 Windows 上使用相同的 dll,我希望行为是相同的,但事实并非如此。基本上我有一个存储用户和应用程序设置的全局类,其中之一是当前的cultureinfo,当用户更改它时,语言管理器更新是当前的cultureinfo变量。当我调用代码来获取语言字符串时,

resmgr.GetString(label, ci);

其中 resmgr 是语言管理器的 ResourceManager 对象,ci 是cultureinfo 对象,标签是我想要获取的字符串,它返回标签/语言的文本。 我有一个针对项目支持的每种文化的 resx 文件,还有一个针对不变/默认的文化 在 Windows 应用程序中工作正常,但在网络上和我所做的一些单元测试中却不能,即使我将 ci 值更改为新的区域性,结果也始终相同,即来自不变区域性的字符串。 我尝试更改当前的线程文化和当前的 ui 文化,但它不起作用

更新 2: 为什么资源管理器方法 GetResourceSet 返回在 wpf 应用程序中传递的区域性的正确资源集,但单元测试或 web 中的相同代码返回不变的资源集。 我错过了一些东西,只是不知道它是什么......

更新3:由于IOC的使用,并且因为语言项目不是引用而是在完成编译时复制到文件夹中,我发现该文件夹为每个资源创建的文件并未复制到单元测试调试文件夹和网站 bin 文件夹。我更正了这一点,现在正在复制文件,但问题仍然存在。

I'm developing an application that supports multiple locales.

This is done through a separate dll that has a resource for each language, each resource stores the translation of a string to the specific language.

This is working just fine on a WPF application, every time a string is needed on the app, a request is made to the dll and it returns the text acording to the language selected on the app. If the language is changed the text return from the language dll reflects the change.

Now I'm creating a website in ASP.Net with MVC that uses the same language dll.

The problem is that resourcemanager on the language dll, for what I can understand, always returns the text for the system culture. I changed the cultureinfo used by the resource manager, but it doesn't work.

Does anyone have a idea on how can I solve this?
Thanks

UPDATE:
Sorry for the late reply. No access to the internet during the weekend... This is the first time that I have a project that uses the same dll for both web and windows and I expected the behavior to be same, but it isn't. Basically I have a global class that stores user and app settings, one of then is the current cultureinfo, when the user changes it, the language manager update is current cultureinfo variable. When I call the code to get the string the language,

resmgr.GetString(label, ci);

Where resmgr is the ResourceManager object of the language manager, ci is the cultureinfo object and label the string that I want to get, it returns the text for the label/language.
I have a resx file for each culture that the project supports and one for the invariant/default
In the windows app works fine, but on the web and in some unit tests that I made it doesn't, even thou I change the ci value to a new culture the result is always in the same, the string from the invariant culture.
I tried to change the current thread culture and current ui culture but it didn't work

UPDATE 2:
Why does the resource manager method GetResourceSet returns the correct resourceset for the culture that passing in a wpf application but the same code in unit testing or web returns the invariant resourceset.
I'm missing something just don't know what it is....

UPDATE 3: Because of the us of IOC and because the language porject is not reference but copied to the folder when it finishes compiling, i found that the folders that are created for each resource weren't being copy to the unit test debug folder and to the website bin folder. I correct that and now the file are being copy but the problem persists.

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

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

发布评论

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

评论(3

萌酱 2024-12-09 10:41:22

请参阅此多部分教程,因为它是一个相当扩展的主题。

See this multipart tutorial, as it is quite an extended subject.

就像说晚安 2024-12-09 10:41:22

您可能需要设置您的web.config

<configuration>

  <system.web>
    ...
    <globalization uiCulture="auto" culture="auto" />
    <!--
    <globalization uiCulture="auto" culture="auto" />
    <globalization uiCulture="en" culture="en-US" />
    <globalization uiCulture="de" culture="de-CH" />
    -->
    ...
  </system.web>

</configuration>

You may have to setup your web.config:

<configuration>

  <system.web>
    ...
    <globalization uiCulture="auto" culture="auto" />
    <!--
    <globalization uiCulture="auto" culture="auto" />
    <globalization uiCulture="en" culture="en-US" />
    <globalization uiCulture="de" culture="de-CH" />
    -->
    ...
  </system.web>

</configuration>
梦罢 2024-12-09 10:41:22

就像我对 update3 感到难过一样,我没有复制每种语言的资源 dll,这使得当我尝试获取标签时网站和单元测试失败,因为资源管理器找不到正确的语言,它总是会返回与标签关联的不变字符串。起初,dll 的副本似乎不起作用,但在我从 Visual Studio 运行“清理解决方案”和“重建解决方案”后,网站和单元测试开始按预期工作。
感谢您的所有提示,这些对我有很大帮助,然后我就能确定问题。

Like i sad on update3 the fact that I wasn't copy the resources dlls for each language made that the website and unit test failed when I tried to get a label, as the resourcemanager didn't find the correct language, it would return always the invariant string associated to the label. At first the copy of the dlls didn't seem to work, but after I run "Clean Solution" and "Rebuild Solution" from visual studio the website and unit testing started to work as expected.
Thanks for all the hints, those were of a big help for me, with then I was able to pin down the problem.

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