c# - localization without re-compiling project -


i building project , need make customizable. trying build support 4 languages. , user have admin panel he/she can change label's text or button's text. want user go admin panel , change button's text without calling me :)

i have used old style of localization .resx files. have sample code below.

private void combobox1_selectedindexchanged(object sender, eventargs e)     {          if (combobox1.selecteditem.tostring().equals("en-gb"))         {             thread.currentthread.currentuiculture = cultureinfo.getcultureinfo("en-gb");             label1.text= formlabels.test1;             label2.text = formlabels.test2;         }         else if (combobox1.selecteditem.tostring().equals("de-de"))         {             thread.currentthread.currentuiculture = cultureinfo.getcultureinfo("de-de");             label1.text = formlabels.test1;             label2.text = formlabels.test2;         }      } 

if let user change button's text in "formlabels.en-gb.resx" file. project must recompiled see changes.

i need find solution user can change button's text recompiling. how can that?

the thing can think have localization in external files. create xml file like:

ex: languagessupported.xml

    <languages>         <language name="english" file="en.dat" />         <language name="french" file="fr.dat" />         <language name="japanese" file="jp.dat" />     </languages> 

like can add more languages later on.

now in each file need like:

(ex: en.dat)

    <language name="english">         <localized name="hello" value="hello">         <localized name="goodbye" value="goodbye">     </language> 

(ex: fr.dat)

    <language name="french">         <localized name="hello" value="bonjour">         <localized name="goodbye" value="au revoir">     </language> 

in code that:

    private dictionary<string, dictionary<string, string>> _localizations = new dictionary<string, dictionary<string, string>>();      private string _currentlocalization = "english";      private bool loadlocalizations()     {         try         {             if (file.exists("languagessupported.xml") == false)             {                 return false;             }              xmldocument xmldoc = new xmldocument();             xmldoc.load("languagessupported.xml");             xmlnodelist nodelist = xmldoc.selectnodes("languages/language");              if (nodelist.count > 0)             {                 foreach (xmlnode node in nodelist)                 {                     loadlocalization(node.attributes["name"].value, node.attributes["file"].value);                 }             }              return true;         }         catch (exception ex)         {             return false;         }     }      private bool loadlocalization(string plang, string pfile)     {         try         {             if (file.exists(pfile) == false)             {                 return false;             }              xmldocument xmldoc = new xmldocument();             xmldoc.load(pfile);             xmlnodelist nodelist = xmldoc.selectnodes("language/localized");              _localizations.add(plang, new dictionary<string,string>());              if (nodelist.count > 0)             {                 foreach (xmlnode node in nodelist)                 {                     _localizations[plang].add(node.attributes["name"].value, node.attributes["value"].value);                 }             }              return true;         }         catch (exception ex)         {             return false;         }     }      private void setlocalization()     {         labelhello.text = _localizations[_currentlocalization]["hello"];         labelgoodbye.text = _localizations[_currentlocalization]["goodbye"];     } 

after that, each time user changes language, update _currentlocalization , call setlocalization();

you can populate dropdownlist of language using keys _localizations.

that way make localization dynamic.

if want use cultureinfo, map culture language name.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -