I am using asp.net mvc and I am creating a multi-lingual website using localization.
Therefore I have a bunch of resources for every language and I can change the language in a dropdown menu.
I set the cultureInfo here
private void UpdateCulture(int langId)
{
string culture;
switch (langId)
{
case 1:
culture = "de";
break;
case 2:
culture = "en";
break;
case 3:
culture = "tr";
break;
default:
culture = "de";
break;
}
var cultureinfo = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = cultureinfo;
Thread.CurrentThread.CurrentUICulture = cultureinfo;
}
German and English is working just fine, but for some reason Turkish is causing me problems.
I have a Resources.tr.resx file and it seems to be not valid.
I know there are some special cases for Turkish like the Turkish-i, but if I use another culture code for my resource file aswell as in the code, it works.
For example, if I change Resources.tr.resx to Resources.kk.resx ("kk" for Kazakh) and also the CurrentCulture to "kk", my website is using the Turkish texts from the renamed resource file just like it should.
I spent alot of time figuring out what causes the problem and at least found that dirty solution to make the website work with Turkish language. Has anyone of you experienced a similar problem?
Related
I'm working on localization in a small test WPF app and want to have English and French. I created Resources.en-CA.resx and Resources.fr-CA.resx.
When my test app starts, I have a startup window with a dropdown to select English or French. When the user clicks OK, a method is called to switch the culture based on the selected language, and then it loads another window which will show some resource strings in the selected language.
switch (selectedLanguage)
{
case "English":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-CA");
break;
case "French":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
break;
}
The user can close then close this window and select a different language from the startup window and click OK to show the window again but with the new language they selected.
This is working but I also have a Resources.resx which is basically just a duplicate of Resources.en-CA.resx. I want to know if I can get rid of Resources.en-CA.resx, and if so, what do I set CurrentUICulture to so that it will use the values from Resources.resx when English is selected.
switch (selectedLanguage)
{
case "English":
//What would I do here?
break;
case "French":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
break;
}
I'm accessing the resources strings in code using
Properties.Resources.SomeLocalizedString
Yes, you can get rid of the Resources.en-CA.resx file if you like. You don't need to change anything as your current code will continue to work in the same way.
The localization in .Net is implemented with a fallback feature from the more specific to the more generic. If It cannot find the resource file for the specified language and local (e.g. Resources.fr-CA.resx ), it looks for the resource file of the language alone (e.g. Resources.fr.resx) and if it cannot find it, it uses the default resource file (e.g. Resources.resx).
For more details, see Hierarchical Organization of Resources for Localization.
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);
I've encountered several times the same problem in applications we develop:
We want to allow the user to edit/display it's data in his format (date, currency, ...), but we want to display the application in English only (for several reasons, it's a pro, international application, in a domain in which we communicate mostly in English).
There is no problem when we manage the whole application, but most of third-party pro frameworks that I used (Telerik, DevExpress) are using the CurrentCulture to display my data in the correct format AND in the corresponding language.
So, even if I have my computer in English, I have my regional settings set to fr-CH, I will have all third party user controls in French.
I cannot set the CurrentCulture to a specific culture and set the format of my user controls to something else (I would loose my default format) and I can't let the CurrentCulture to be the default one because I would have my third party components in another language.
I tried to build my own culture (CultureAndRegionInfoBuilder), with no success. When I change the language, I still have my application in the user-specific language.
Concrete problem
I'm using a date editor(basic, it has one text input and can popup a calendar). I want to have the date displayed in my OS locale(ch-FR, so 15 january 2013 would be "15.01.2013"), but I don't want that when I display the calendar month/day name appears in french.
What is the correct approach with this?
Store the original CultureInfo for your purposes and try editing CurrentCulture and CurrentUICulture properties of the CurrentThread property in System.Threading.Thread, maybe this will solve your problem.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I resolved my problem by having a custom culture info:
private static void UpdateCultureInfoWithoutLangage()
{
//We initialize a en-US cultureInfo and change all formats + number infor related
CultureInfo cultureInfoEn = new CultureInfo("en-US");
CultureInfo cultureInfoEnClone = (CultureInfo)cultureInfoEn.Clone();
//Setting DateTimeFormat(Without changing translations)
cultureInfoEnClone.DateTimeFormat.FirstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
cultureInfoEnClone.DateTimeFormat.FullDateTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
cultureInfoEnClone.DateTimeFormat.LongDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
cultureInfoEnClone.DateTimeFormat.LongTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
cultureInfoEnClone.DateTimeFormat.MonthDayPattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
cultureInfoEnClone.DateTimeFormat.ShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
cultureInfoEnClone.DateTimeFormat.ShortTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
cultureInfoEnClone.DateTimeFormat.TimeSeparator = CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator;
cultureInfoEnClone.DateTimeFormat.YearMonthPattern = CultureInfo.CurrentCulture.DateTimeFormat.YearMonthPattern;
cultureInfoEnClone.NumberFormat = CultureInfo.CurrentCulture.NumberFormat;
Thread.CurrentThread.CurrentCulture = cultureInfoEnClone;
Thread.CurrentThread.CurrentUICulture = cultureInfoEnClone;
Application.CurrentCulture = cultureInfoEnClone;
}
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.
I have setup a windows forms project to use localization so that it will support Chinese and English languages. I have built in a way of forcing the language to one or the other when the form loads. Before InitializeComponent() is called I have a bit of code that does this...
switch (Properties.Settings.Default.SelectedLanguage)
{
case "":
break;
case "English":
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
break;
case "Chinese":
try
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CHT");
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CHT");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
break;
}
So when the SelectedLanguage property is set to "Chinese" the program should use the Chinese localization right? I tested this while debugging and it worked exactly as expected. That is, when SelectedLanguage == "Chinese" at the program start all the buttons and labels display in the Chinese text that I entered. When the SelectedLangugage == "English" everything is displayed in the English Text that I have entered.
The problem is that when I install this program and run it (not debugging) it doesn't work. Even on my development machine. No matter what SelectedLanguage is set to the program always displays the English Localization. I even put in a message box to pop up at the beginning of the program which displays `Application.CurrentCulture.Name' and it shows the Chinese culture name (zh-CHT) but it still displays everything in English. So what is the difference between what happens during debugging and during actual run time? And how can i fix it?!?
Make sure that your localized resources Dll are correctly installed.
Your installer should put resources files into sub-folders such as
zh-CHT\AssemblyName.resources.dll
It sounds like the english default is being used at all times, which would suggest that the resources you have set up for Chinese are not being included in the build. Double check your resources and make sure it is included in your deployment.