How to Unescape and Reescape Strings in .Net

How to change CurrentCulture at runtime?

Changing the current UI culture:

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("he-IL");

or better, retrieve a cached read-only instance of the he-IL culture:

System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("he-IL");

At run time, ASP.NET uses the resource file that is the best match for the setting of the CurrentUICulture property. The UI culture for the thread is set according to the UI culture of the page. For example, if the current UI culture is Spanish, ASP.NET uses the compiled version of the WebResources.es.resx file. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads the default resource file. In this example, the default resource file is WebResource.resx.

CultureInfo.CurrentCulture doesnot change at runtime

Run this first:

CultureInfo.CurrentCulture.ClearCachedData();

Then do your messagebox.

I'm just curious as to why you need live updates from the culture info? I think the average user would set that once in Windows, and load their applications.

WPF: How to change the CurrentUICulture at runtime

What am I missing?

You changed the culture registered with the thread, and String.Format will use this now, but you need to reload all localized items in the WPF hierarchy.

WPF Localization – On-the-fly Language Selection has more information.

Changing CurrentUICulture at runtime in a Localizable Form in WinForms

After some tries, I have realized that several things are failing.

I have to say also that all the given help in this question is greatly appreciated, and also that the LocalizedForm snippet is very useful.

The first issue is that I have realized is that the controls that are under a level of more than 1 in the child hierarchy with this solution doesn't work.

And iterating over all the controls is a expensive task. Maybe mine is worst but has less iterations (because I only seach for Controls with Text)

    /// <summary>
/// Current culture of this form
/// </summary>
[Browsable(false)]
[Description("Current culture of this form")]
[EditorBrowsable(EditorBrowsableState.Never)]
public CultureInfo Culture
{
get { return this.culture; }
set
{
if (this.culture != value)
{
ResourceSet resourceSet = new ComponentResourceManager(GetType()).GetResourceSet(value, true, true);
IEnumerable<DictionaryEntry> entries = resourceSet
.Cast<DictionaryEntry>()
.Where(x => x.Key.ToString().Contains(".Text"))
.Select(x => { x.Key = x.Key.ToString().Replace(">", "").Split('.')[0]; return x; });

foreach (DictionaryEntry entry in entries)
{
if (!entry.Value.GetType().Equals(typeof(string))) return;

string Key = entry.Key.ToString(),
Value = (string) entry.Value;

try
{
Control c = Controls.Find(Key, true).SingleOrDefault();
c.Text = Value;
}
catch
{
Console.WriteLine("Control {0} is null in form {1}!", Key, GetType().Name);
}
}

this.culture = value;
this.OnCultureChanged();
}
}
}

What I do is the following, first, I search for the ResourceManager, take care! Because here is the second issue and is that if you use CultureInfo.CreateSpecificCulture instead CultureInfo.GetCultureInfo in some cases a new culture will be created and default values will be returned (Form1.resx values instead of Form1.es.resx values (for example)).

Once we have loaded all the value from the resx file, we iterate over all of them, and we delete the double >> (it appears in some cases) and we get the name of those Controls that only have declared the Text attribute.

The next step is find the Control and replace its Text...

Well, I have a little mess with the derived classes, that's why I created a try-catch system, because, Controls.Find search in all the Derived Classes I would prefer to be a little bit more specific but I don't know how... (That's why I created this question)

With this we haven't to save any object because we won't clear and recreate them.

The main problem here wasn't the way I was doing this, because it was correct.
The problem is that Assemblies merged do weird things when you call for example this:

ResourceSet resourceSet = new ComponentResourceManager(GetType()).GetResourceSet(value, true, true);

The value will be the default... Something like that this Culture doesn't exist, and is because, the merged Assembly can't find the resx file.

So, I will try AppDomain.CurrentDomain.AssemblyResolve that @ScottChamberlain has suggested to me. Or ILRepack maybe.

Any help for optimization or ideas (in comments) of why this doesn't work will be appreciated!

How to change my MVC Website's Current Culture at Runtime

The good way to do it is to make a method which sets a cookie in your browser :

public void ChangeCulture(string lang)
{
Response.Cookies.Remove("Language");

HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["Language"];

if (languageCookie == null) languageCookie = new HttpCookie("Language");

languageCookie.Value = lang;

languageCookie.Expires = DateTime.Now.AddDays(10);

Response.SetCookie(languageCookie);

Response.Redirect(Request.UrlReferrer.ToString());
}

After this ( the tricky way ) you need to make every controller to inherit from one BaseController. It is tricky because you need to override Initialize.

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{

HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["Language"];
if (languageCookie != null)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCookie.Value);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageCookie.Value);
}
else
{
//other code here
}

base.Initialize(requestContext);
}

and in your Method call ChangeCulture() with lang

[AllowAnonymous]
[HttpPost]
public ActionResult SelectLanguage(LoginViewModel model)
{
switch (model.SelectedLanguage)
{
case "French":
ChangeCulture("fr-Fr");
break;
}

return RedirectToAction("Index");

}

How to change Application Culture in wpf?

try this

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");


Related Topics



Leave a reply



Submit