Add / switch between embedded / linked resources at running time - c#

I have the resources.resx in my c# code with some strings filled:
Text1,"Some Text"
and i can call it during running time by
Properties.Resources.Text1
which results in
"Some Text"
Now i want to have Text1 a different output (another language for example or something)
so that Properties.Resources.Text1 results in "Different Text".
How can i achieve this?
EDIT1: i discovered this but i was looking for a different approach with the resource files.

If you want to use different Resource Files, you can use the ResourceManager:
ResourceManager rm;
if (Configuration.Default.Culture == "en-US")
rm = new ResourceManager(typeof(Resource1));
else
// ...
String label = rm.GetString("Text1");
Save the the Culture in the User Settings, add a configuration file and define a user variable.
Configuration.Default.Culture= "en-US";
Configuration.Default.Save();
Updated question according to information

I am afraid you have to add another resource file for other culture. just look into this thread
How to use localization in C#
In reference to a comment:
get the current cultureinfo and load resources like this:
Thread.CurrentThread.CurrentCulture.ClearCachedData();
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
if(currentCulture.Name == "en-US")
Console.WriteLine(resources.Text1);
else if if(currentCulture.Name == "ja-JP")
Console.WriteLine(resourcesJapan.Text1);

Related

Why wpf ResourceManager GetString (localization) returns english only

I have a localization issue.
Here,in below code (auto generated in designer file) the str always returns english values even if the application is in other locale.
.
ResourceManager rm = new System.Resources.ResourceManager("A.b.Properties.Resources", typeof(Resources).Assembly);
.
.
var culture = Thread.CurrentThread.CurrentUICulture; //zh-CN selected as locale
var str = rm.GetString(key, culture); //always returns english :(
.
My resource file sits under the zh-CN folder wherever the executables got built:
"..\zh-CN\A.b.resources.dll" (checked the dll internally, it has the all the strings-values available in cn language)
Project name is: A.b
Namespace in the designer file is: A.b.Properties
I am not able to figure out why the application returns only the english values and not the locale specific ones!
I tried it a lot but unable to figure out whats wrong!
Though i, noticed that the _resourceSets [zh-CN] object Values has all english items in it.
Any idea/reference is much much appreciated.
At application startup, my application determines my preferred language culture, gets the system CultureInfo object for it, and sets the following two values. Are you doing something like this?
System.Threading.Thread.CurrentThread.CurrentUICulture = desiredCulture;
CultureInfo.DefaultThreadCurrentUICulture = desiredCulture;
Also, I don't know if it matters but all of my language-specific resource DLLs live in subfolders that are just the TwoLetterISOLanguageName code from the CultureInfo in question.
So for example, my French translations live in the "fr" subfolder. Are you sure your resources should not live in a "\zh" subfolder, instead of "\zh-cn"?

How to use Multi language in C#

I'm trying to prepare an application with more languages available.
I prepared the simplest example to learn it, I done a lot of tentative but I'm not able to do it.
CultureInfo cul = new CultureInfo("de-De");
Resources.Culture = new System.Globalization.CultureInfo("de-De");
label1.Text = TestLanguages.Properties.Resources.Saluto;
In my application I have two resources different resources , one for Italian language, one for German.
Italian Resource : Saluto -> Ciao
German Resource : Saluto -> Hallo
But I can't use the German one. How can I do it?
You have to change the UI culture of the currently executing thread.
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-De");
label1.Text = TestLanguages.Properties.Resources.Saluto;
See the documentation for Thread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.

Why are my different language resx files being ignored

I have been following this page to make my site support multiple cultures.
the code all compiles and runs, but it seems to be ignoring my culture specific resx files.
Right now the test is pretty superficial, and I simply have 2 resource files;
Resources.resx and Resources.au.resx
My template is calling #Resources.PageFooter which is the only string defined in both files.
If I browse to http://mysite/index and inspect the page footer it contains my default value. If I change my url to http://mysite/au/index then it still displays the default value. However inspecting CurrentThread.CurrentUICulture and CurrentThread.CurrentCulture shows that both are now set to au, but it is still picking up and using Resources.resx not Resources.au.resx
My Resource files are in a separate folder Resources, as the article suggests.
So I am stuck.
After some investigation, I have found that if I create another resx file (for Italian), then that works.
So it looks like the files that are being ignored are the 'variations' such as American English, Australian English and so on.
First, please change au to en-AU, since this is the correct colture code for Australian English.
In your Global.asax file, set the Application_BeginRequest method like below:
void Application_BeginRequest(Object sender, EventArgs e)
{
string langCode = "en"; //default
if (somecondition)
langCode = "en-AU";
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(langCode);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(langCode);
}
You need the somecondition I mentioned above to check where the user is coming from - by header, cookie, URI or any other way you want.

managing MultiLanguage resources

I have created two resource files in my project GlobalRes.ge.resx and GlobalResources.en.resx
I receive language as an input parameters . I want to know how can I read my values based on language. for example if string lang = "en" then by globalres.welcome I should see WOLCOME but if I choose lang = "ge" then globalres.welcome should be willkommen
(I have already created the welcome line in both files)
The Resource Designer will load the appropriate text based on the CurrentUICulture
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
bCancel.Text = Resource.Cancel;
In .NET 4.5 and later you can use the following properties to set the DefaultThreadCurrentCulture & DefaultThreadCurrentUICulture culture.
CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");

Problem with changing language using CultureInfo

Language is not getting changed when we are giving specific culture like "fr-FR" through Resource File.please help me out of it if any one knows ,Thanks in advance.
CultureInfo cinfo = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = cinfo;
Thread.CurrentThread.CurrentUICulture = cinfo;
The below code indicate that accessing the value through resource1 file according to the culture .
_inboxpage.Text = Resource1.Ready;
The resource files need to have a special naming convention to work transparently with different culture information.
You create a separate resource file for each language that you want to support or for a language and culture. Have one separate neutral resource file for the application to fall back upon in case the required key/value pair is not found.
ex:
Resources.resx //neutral resource file
Resources.fr.resx //french specific file
Resources.fr-FR.resx //French language for France
and so on.
You can get more details here:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
Finally i got a solution that while doing Localization in Plugin Application we need to copy that culture folders like "fr-FR" to corresponding Main Application then it will works fine.

Categories