Is it possible to manually specify fallback cultures with .NET resx globalization? - c#

I am working on a website where we need to deploy partially-tested translations (resx key-value pairs) to some users for review before rolling out those translations for all users.
One potential way to do this would be a to create a "beta" culture for a specific language and populate that culture's resx file with these untested translations. Then, we could switch our test users to this culture so that they can see the beta translations. Thus, I would like to establish a fallback chain like:
beta-chinese => chinese => default (english)
Is this possible within the .NET/ASP.NET resx architecture? If so, how do I set this up?

If you have the following resources it should work:
Resources.resx
Resources.zh.resx
Resources.zh-BT.resx
See here too:
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx
If you named your Chinese resources zh-CN then I think it will not work.
For zh-BT you have to create a custom culture:
Create custom culture in ASP.NET

Related

Can I use any other parameters besides CurrentCulture to select a C# resource file?

As you probably know .NET selects resource files in a solution based on the .resx filename and the CurrentCulture setting. Meaning that if I have 3 resource files (say Resource.resx, Resource.fr-FR.resx and Resource.nl-NL.resx), I can select the French resources by simply changing the CurrentCulture to "fr-FR" in my app.config. The code that gets the string from the resource file does not have to be changed.
For my current project I'm exploring ways to switch strings in a similar way to how this works, but based on a parameter of my choosing. Say I have Resource.resx, Resource.Bar.resx and Resource.Fu.resx: can I automatically select a resource by changing a custom setting that is not the CurrentCulture, or would I have to build my own ResourceManager extension?
Or could I just set the CurrentCulture to "Fu"? I imagine this would clash with other built-in .NET functions.

How to get localized *.resx Files (embedded Resource)

I have my loacalized *.resx files in another project to which I set a reference (it's a solution with mulltiple projects, so we have a *.Common project where we have styles, localization...). by now I have two *resx:
TextObjects.resx ------> english/invariant
TextObjects.de.resx --> german
How can i get these Files (the cultures of it)? Because I use WPFLocalizeExtension the *.resx-files need to be embedded resources.
I found only a way to watch inside the Resourcefile and get it's Keys, but not how many and which files i have.
Also I face two other problems:
The default invariant Language ist shown as this. Is there a way, without an Converter, to show it as english?
I like do set the systems CurrentCulture as first culture. If there is no localization for that the Extension correctly uses the Invariant Localization. BUT if the language doesn't exist it still get added to the MergedAvailableCultures and is as a result also shown in the ComboBox to which i bound this list. How can I avoid this?

Change the "(default)" ResX language in a C# app

I'm making an app in C#, with a lot of forms and other stuff.
I wanted to localize it, so I used the integrated localization functions in VS.
The app was originally in French (cause I am) with English translation RESXes, but now that it is on Codeplex with some other developers who doesn't speak french, I want to change the "default" language in the app and the RESX files so they can translate to other languages without reading French.
Cause when you choose another language in VS Form Designer, it shows the default language (here, French). I tried to rename all the files, but the problem is all the form properties are stored in the default RESX, the French one, so if I rename, it will break all the stuff. Is it possible to change the default language to English?
EDIT: I think I need to be more precise.
The app is originally in French. Then I localized it in English. There is now a MainWindow.resx that contains form properties + original french localization and a MainWindow.en-US.resx that contains ONLY english localization. So if in the Designer, in "Language" I choose "(default)" it shows me the MainWindow.resx FRENCH, and if I want to translate, It shows me the French texts. To make the translation more easy for the other developers, I want to make the MainWindow.resx ENGLISH localization and create a seperate MainWindow.fr-FR.resx. I also tried to change "Neutral Language" in AssemblyInfo.cs, no effect.
While you could do renames in file system to achieve neutral culture/resource change, you need to update project file as well, which is cumbersome. I found the simplest way was to do this via Visual studio extension named ResXManager.
Steps in detail:
1. Run & configure ResXManager
In VS: Tools -> ResXManager.
Choose configuration tab (below)
Disable option "Confirm adding new resource files". Otherwise you would have to click later per each file to accept its creation.
Set Neutral Resource language so it would show up with a correct flag for your viewing pleasure.
Switch back to main tab where you see all solution resources with translations side-by-side.
2. Extract neutral culture resources as explicitly French
Adda a new column for French using the "Add new language" button in the toolbar.
Copy all French resources from existing neutral language column (CTRL-SHIFT-DOWN helps top select full column)
Paste all resources to new French column.
Note that ResXManager will create new resx files for you and add them to project file.
3. Set neutral culture reousrces to English ones
Copy all English resources from specific culture column
Paste all resources to Neutral culture column
4. Inform .Net that neutral culture is now english
[assembly: NeutralResourcesLanguage("en-GB", UltimateResourceFallbackLocation.MainAssembly)]
5. Remove explicit English resx files.
Since the neutral resx files now already contain english resources then you don't need to keep them the culture-specific ones. Unfortunately ResXManager didn't seem to have a button for this but you can I chose to use VS solution manager filter "en-GB.resx" and deleted all files it found. Again, doing it within VS automatically updates your project file.
All done.
You can change the CurrentCulture and CurrentUICulture in your application through code.
It's the same approach that is used to change the apps language at runtime.
Have a look at this code:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Hope it helps.

Identify unlocalized text

In ASP.NET, an application can be localized using resource files. Resource files hold different translations. For example, one might have an English resource file and a Spanish resource file. When resource files are used, an attribute can be applied to controls on a web page to automatically populate that control with values from a resource file. Alternatively, the values can be programmatically loaded from a resource file and assigned to a control's property.
ASP.NET uses a fallback mechanism for loading translations. It tries to find the resource file that is most similar to the current user's culture. If the current user's culture is Spanish, ASP.NET tries to load the appropriate resource from the Spanish resource file. If the Spanish resource is not available, it falls back to a default resource file. Because of this behavior, text for a Spanish user may be shown in the default language for two reasons:
No Spanish translation is available. (The translators haven't provided a translation for this item yet.)
The text is not localized. (This may be the result of plain text appearing in the page or the message being hard-coded somewhere.)
If text appears in the default language, I want to know whether it was because of reason 1 or because of reason 2.
For every missing translation, I could insert some kind of placeholder text in a resource file. However, this means that I am throwing away the fallback mechanism. Even worse, if placeholder text accidentally makes it through to production, it looks much worse than showing the default text.
Does anyone have any suggestions (or solutions) for determining which of these two conditions is the reason for default text appearing during manual testing?
If I understood you correctly you want to verify that every localizable text is indeed in fact localizable and not burned in the code. To do this you should not be using a real culture (Spanish), instead you should create resources for a fake unsupported culture and provide an automatic translation for every localizable resource entry available in the default resources.
For example, if you have a default resource containing:
Entry1: This is a test!
you should create a resource in your fake culture containing:
Entry1: Th1s 1s # t€st!
You could even (and should) perform the creation of the fake resources automatically using a simple character mapping. This way, when you set the application to use your custom fake culture you know that every entry has a translation so you can find harcoded text. This strategy is used by Windows and is known as pseudo-locales. The use of pseudo translated strings makes it possible do development using the fake culture because the text is still readable and this improves your probability of finding hardcoded text.
Windows supports pseudo-locales since Windows Vista and Windows 2008 R2, so if your build and testing environment uses these operating systems you can associate your fake culture to one of these pseudo locales (for example qps-ploc). If you have unsupported operating systems just associate your fake resources to a real culture that you probably will never be supporting or just create your own culture.
Also note that even in a supported operating system, Visual Studio will not create satellite assemblies for these pseudo-locales unless you enable them on the registry.

C# Satellite Assemblies? Do I need to link default culture resource

We would like to employ satellite assemblies to contain various locale dependent resource files.
Question is ... do we need to link the default culture?
We have a separate project which will contain all of our different culture resource files. As is shown below, we have the project with two resource files inside of it.
ProjRES
Resource.resx
Resource.it-IT.resx
I am assuming that the "Resource.resx" will act as the default culture and if the Italian culture is selected, the application will adopt the it-IT resource file.
What do we need to do in order to get the rest of the application and projects to access the resource files. How do we set the namespaces for the resource files in order to be able to reference them.
Any help is greatly appreciated.
Basically,
if the current culture that the OS is using matches a certain culture that you've shipped, it will be used, if the current culture matches none of the cultures you've shipped, it will use the neutral culture.
In the most simplistic cases, you'll just need to include any of the localized dlls with the deployment and all will be fine..
When you're using resource managers, I think you can also pass in which culture you want to use explicitly, and the runtime will search for resources that match - this is better when a user of an ASP.Net site might have a certain culture preference that is different from that of the machine that the site is running on.
http://msdn.microsoft.com/en-us/magazine/cc163609.aspx seems to be a good starting point.

Categories