简体   繁体   中英

How to force a certain culture on a string c#

I'm working on localizing hard coded string messages in a solution that has more than 30 projects. All the user messages should be localized ,except for the Log messages, as those will be read by US developers so they have to stay in US English language.In all solution projects, a string with the same message had been created in every file (with different variable name in each) which goes into the User and Log messages as well.

String s = "Unable to populate list: "
Log.Warning(LogCategory.NameLookupDialog, s + ex.Message + ex.StackTrace);
TXMessageBox.Show(s);

The string in the TXMessageBox will be pulled out of the resources files,but the one that goes into the Log message should stay US English.

Is there a way to force the US English culture on the s string when it goes into the Log message ?On other words , if the culture of the application changed for example to Indian , the Log message should stay with the US English. Here is what I have tried so far : I changed the culture of the application to Indian , then I tried to set the culture of a label text only to US English ,but it didn't work :

public void SwitchCulture()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-IN");
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
            this.lblLogMessage.Text = string.Format(new CultureInfo("en-US"), this.lblLogMessage.Text);
        }

When I run this I still see all the labels in Indian . So any ideas how to force the culture on the string ?

It could be that the font you are using on your label won't accept Latin characters. See this previous answer: How to get Hindi (Unicode) Text From Textbox?

If that doesn't work, try setting the CultureInfo on the current thread to EN-US before you set the label's text. Then set it back to Hindi when you're done.

AppResources.ResourceManager.GetString("ResourceKey", new CultureInfo("en-US"))

where AppResources is the file name and class name of the resources and ResourceKey is the resource key name inside in resx file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM