Marqueone
Marqueone

Reputation: 1213

Manually retrieve a resource value from a resx?

Is it at all possible to get the value from a resource file that is not set to the current user's culture? Our application is data-based opposed to culture-based.

e.g. a document is a French document and specific labels and fields need to be updated and replaced with new data.

Can the resource manager be instructed to use a French .resx instead of the default .resx?

Upvotes: 4

Views: 4308

Answers (3)

Ryan Kohn
Ryan Kohn

Reputation: 13509

You can use the ResourceManager.GetString() method and provide the desired culture as a parameter:

var culture = CultureInfo.CreateSpecificCulture("fr-FR");
var localizedString = ResourceManager.GetString("labelName", culture);

Upvotes: 3

Glory Raj
Glory Raj

Reputation: 17701

you can set the language typeto the label text in one of the forms and then you choose the language which one you want to show to the end user compare with the language with that label text I.E, if the label text is french then you can show all your control names in french

NOte: its only works after you creating the resx file in french and rewrite manually all label and button control names in french as name value something like this..

  Name              value 
-----------        -------------
 lblname.text      frenchtype name 


 using System;
 using System.IO;
using System.Linq;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;

public partial class Form1 : Form
{
   public form1()
   {
    System.Threading.Thread.CurrentThread.CurrentUICulture = new
 System.Globalization.CultureInfo("fr-FR");
    getlanguagaefile();
    InitializeComponent();
   }

 // blah
 // blah

private void getlanguagaefile()
{
    if (label1.Text == "French")
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new
  System.Globalization.CultureInfo("fr-FR");
        ComponentResourceManager resources = new ComponentResourceManager(typeof(Wait));
        resources.ApplyResources(this, "$this");
        applyResources(resources, this.Controls);

    }
  } 

you can display french language to all label texts and button texts when the form loads

   private void applyResources(ComponentResourceManager resources, Control.ControlCollection controlCollection)
  {
    foreach (Control ctl in controlCollection)
    {
        resources.ApplyResources(ctl, ctl.Name);
        applyResources(resources, ctl.Controls);
    }
  }
}

Upvotes: 2

KreepN
KreepN

Reputation: 8598

You can do so via localization, where the resource files are adjusted to the different languages you wish to support. The following links, taken from here should get you what you want.

".NET Localization, Part 1: Resource Managers - Check under "Creating Resources For Multiple Languages" for a good start.

.NET Localization, Part 2: Creating Satellite Assemblies

.NET Localization, Part 3: Localizing Text

.NET Localization, Part 4: Localizing Units"

Upvotes: 1

Related Questions