.NET localization Exceptional language when using ResourceManager

I recently delved into localization with .NET. Essentially, I learned how to set up a form (using the Language and Localizable property), and then change the culture accordingly.

However, I found that when transferring my hard-coded English strings to files of automatically generated files and using .GetString ("Key") - well, just say that it was not enough: P.

I decided to create a separate set of resx files designed exclusively for lowercase encoded strings. They complied with the agreement / requirement [name]. [Code-Culture] .resx. I did this for each appropriate language; e.g. appstrings.de.resx (for German) and appstrings.resx (as the invariant baseline).

To use these new resources, I created an instance of ResourceManager and Resource Set

Dim resManager As New ResourceManager("LanguageTest.appstrings", Assembly.GetExecutingAssembly) Dim resSet As ResourceSet = resManager.GetResourceSet(My.Application.UICulture, True, True) 

The current UI culture has been set (e.g. to German) using

 My.Application.ChangeUICulture("de") 

Original issue

If resSet.GetString ("Key") is explicitly defined in appstrings.de.resx , it will return an empty string. In any case, can I make this a backup appstrings.resx application (where "Key" really exists), which I assumed would be the default base base?

Update

Rhapsody made the suggestion below, while the actual tip itself did not work, it did trigger an interesting point using resManager.GetString ("Key") rather than resSet.GetString ("Key"). At the moment, it works without errors. That is, the values ​​presented in the specialized language file are returned, and the "missing" values ​​are returned to the default culture when accessing one key.

Subsequent problem

The only remaining issue is whether the performance impact of using the ResourceManger, unlike the cached ResourceSet, will be so harmful?

+7
resources localization embedded-resource
source share
3 answers

You can try declaring a default culture for the application as shown below

 [assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.Satellite)] 

This code announces that your default culture is en-US. In your example, if the current culture is β€œde-DE”, it searches for a resource in β€œde-DE”, then looks for the parent β€œde” culture, etc., And Ultimatly goes to a specific culture resource file (en-US), see MSDN for fallback strategies using resourceManage. The above normaly code goes to assenblyInfo.cs. The second parameter tells the resource manager where the resources are located in the main assembly or satellite.

Unfortunately, in this solution you need to use the name of the culture, not the name of the resource file. however, you can extend RM to use a specific resource by file name. But its way is easier to select the default culture and rename your resource file to that culture, and you have the solution out of the box.

+4
source share

Try

 Public Function GetString(ByVal Name As String) As String Return My.Resources.Strings.ResourceManager.GetString(Name) End Function 

Where Strings is the name of the resx files in your project. This will automatically return the original value if it is not available for the current crop.

This is a simple example, you can make it more stupid.

+2
source share

I once created a web application that had en-US as the backup language, but had other more specific ones, such as .de, based on the settings in the users web browser

Mostly for it to work, I install xml in web.config

 <globalization uiCulture="auto" culture="auto" requestEncoding="utf-8" responseEncoding="utf-8"/> 

From there, I used the internal static ResourceManager from the .cs file, which is provided with your default language, for example.

 Resources.<ResourceFileName>.ItemSearchNoResultsErrorText 

and in .aspx files:

 <%$ Resources:<ResourceFileName>, TimeSelect %> 

As I understand it, then it is important where your .cs file that contains the ResourceManager class lives, is it located behind the .de language file or the backup language? Since it compiles as a separate satellite assembly, it depends on where the .cs class is compiled and which satellite assembly is part.

As a rule, I always had the ResourceManager class as the en-US code, because the one I want to use as the backup language, if no more specific ones are found

Using this setting, I never had an empty value, she always used a spare language, even if I replace satellite assemblies at runtime.

0
source share

All Articles