-1

I have used resource files for localization in design time support. The localized string will be retrieved for designer action items to be added in designer of a control. Here is the code which i used to get the current culture from the resource manager in design mode.

if (resourceManager != null)
{
   CultureInfo currentUICulture = CultureInfo.CurrentUICulture;
   if (resourceManager.GetResourceSet(currentUICulture, true, true) != null)
   {
       ResourceManager result = resourceManager;
       return result;
   }
}

and i have changed the current culture using below code in form level.

 public Form1()
{

    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");

    CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
    CultureInfo.CurrentUICulture = new CultureInfo("fr-FR");

    InitializeComponent();

 }

My Issue - Changed culture for French is not retrieved at design time. Culture got is en-US all the time.

enter image description here

Does anyone know, how to change the culture to reflect at runtime in order to solve the above mentioned issue?

Regards, Amal Raj

Amal
  • 576
  • 1
  • 6
  • 26

1 Answers1

1

Consider these facts:

  • CultureInfo.CurrentUICulture returns current thread UI culture.
  • Your design-time in visual studio, is visual studio's run-time.
  • Constructor of your Form1 will not execute at design time. Its InitializeComponent methis will be just deserialized to initialize a an instance of its base class Form which is shown in VS designer.

Since your current thread is Visual Studio UI thread and it uses en-US in your system, so you will receive en-US in design time as current UI thread.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Then CurrentCulture cannot be changed for design time? – Amal Feb 21 '17 at 12:43
  • It can be changed, but in a correct place. Constructor of your form in not the correct place. Read [this post](http://stackoverflow.com/a/32299687/3110834) to better know how the designer works. – Reza Aghaei Feb 21 '17 at 12:48
  • Also don't try to change the current thread culture, if you write your control designer correctly, to test it, probably it's enough to change your windows display language to see the result. – Reza Aghaei Feb 21 '17 at 12:48
  • So i should change the Culture in InitializeComponent() of form's designer ? – Amal Feb 21 '17 at 13:00
  • No it doesn't have any impact on the result. I recommend you read the linked post carefully and see its interesting example. It's really useful. – Reza Aghaei Feb 21 '17 at 13:05
  • Also I'll try to answer your previous question, which is somehow related to this one. But the current question needs the current answer :) – Reza Aghaei Feb 21 '17 at 13:06
  • While the current answer is true, trying to solve your previous question, I couldn't managed to work with `ResX` files in design time. The designer uses neutral resources regardless of current thread UI culture. – Reza Aghaei Feb 21 '17 at 16:08
  • Yes, The designer uses the neutral resource since the culture is not changed at design time – Amal Feb 22 '17 at 03:53
  • Even it doesn't use `en-US` it uses neutral. – Reza Aghaei Feb 22 '17 at 03:56
  • Yes, it uses the resource file with no culture specified. I use that resource as en-us. How to overcome this issue and use other resources with change in culture? – Amal Feb 22 '17 at 05:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136345/discussion-between-amal-and-reza-aghaei). – Amal Feb 22 '17 at 11:27
  • I tried to solve the problem and used a totally different way for localizing resources successfully. Buts since I guess there should be a better way I prefer to not post it here. The idea which I used is based on `ResxResourceReader` and embedding resX files with a different naming conventions and a different file extension. I was successful but not satisfied with the solution. I shared the idea just for your information. – Reza Aghaei Feb 22 '17 at 11:51