Manually retrieve a resource value from a resx?

拥有回忆 提交于 2019-12-05 18:16:36

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);

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);
    }
  }
}
KreepN

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"

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!