Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:localization_using_resx_files [2011/06/17 11:41]
smayr
swdev:dotnet:localization_using_resx_files [2013/12/16 15:26] (current)
smayr
Line 1: Line 1:
-WPF Localization =+= Localization Using RESX Files =
  
-== Localization Using ResX Files ==+WinForms Localization = 
 +  * Create WinForms Application (eg ''MyApp.exe''). 
 +  * Optionally, create an Resource-only Assembly (eg ''Acme.App.Localization.dll'') where all localized resources will be placed, so that other assemblies or applications can share it. 
 +  * Create Resource file in local application or resource-only assembly. 
 +    * Add new ''Resource.resx'' file to the project under ''Properties'' folder. 
 +    * Set Access Modifier to ''Public'' (top toolbar in resource document). 
 +    * Add strings to the Resource file. Eg: ''rsClose'' ''Close'' 
 +  * Autotranslate original English RESX strings using Google Translate and [[http://resxtranslatorbot.codeplex.com|Resx Translator Bot]]. 
 +  * Create other resources files for other languages not supported in autotranslation. For example:  
 +    * ''Resources.es.resx'' for Spanish (es-ES) 
 +    * ''Resources.fr.resx'' for French Canadian (fr-CA), or potentially ''Resources.fr-CA.resx'' as well. 
 +  * Set Culture before calling anything else in the application. Modify ''Program.cs'' to include: <code csharp> 
 +using System; 
 +using System.Collections.Generic; 
 +using System.Linq; 
 +using System.Windows.Forms; 
 + 
 +namespace LocalizedAppWinForms 
 +
 +    static class Program 
 +    { 
 +        public static System.Resources.ResourceManager resmgr; 
 + 
 +        /// <summary> 
 +        /// The main entry point for the application. 
 +        /// </summary> 
 +        [STAThread] 
 +        static void Main() 
 +        { 
 +            // Sets the UI culture 
 +            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es"); // Spanish (Spain) 
 +            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en"); // English (US) 
 +            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr"); // French (France) 
 +            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de"); // German (Germany) 
 + 
 +            // Load localized resources 
 +            resmgr = new System.Resources.ResourceManager( 
 +                "LocalizedAppWinForms.Properties.Resources", 
 +                System.Reflection.Assembly.GetExecutingAssembly() 
 +            ); 
 + 
 +            Application.EnableVisualStyles(); 
 +            Application.SetCompatibleTextRenderingDefault(false); 
 +            Application.Run(new frmMain()); 
 +        } 
 +    } 
 +
 +</code> 
 + 
 +  * Localize controls. For example, using form ''frmMain'', modify file ''frmMain.cs'' to include: <code csharp> 
 +private void frmMain_Load(object sender, EventArgs e) 
 +
 +    LoadResources(); 
 +
 + 
 +private void LoadResources() 
 +
 +    // Optional method to load resources local to this form 
 +    //System.Resources.ResourceManager resmgrlocal = new System.Resources.ResourceManager( 
 +    //    "LocalizedAppWinForms.Properties.Resources", 
 +    //    System.Reflection.Assembly.GetExecutingAssembly() 
 +    //); 
 +    //lblFirstName.Text = resmgrlocal.GetString("rsFirstName"); 
 +    //lblLastName.Text  = resmgrlocal.GetString("rsLastName"); 
 +    //lblPatient.Text   = resmgrlocal.GetString("rsPatient"); 
 +    //btnClose.Text     = resmgrlocal.GetString("rsClose"); 
 + 
 +    // Method to load global resources (defined in Program.cs file) 
 +    lblFirstName.Text = Program.resmgr.GetString("rsFirstName"); 
 +    lblLastName.Text  = Program.resmgr.GetString("rsLastName"); 
 +    lblPatient.Text   = Program.resmgr.GetString("rsPatient"); 
 +    btnClose.Text     = Program.resmgr.GetString("rsClose"); 
 +
 +</code> 
 += WPF Localization =
   * Create WPF Application (eg ''MyApp.exe'').   * Create WPF Application (eg ''MyApp.exe'').
   * Optionally, create an Resource-only Assembly (eg ''Acme.App.Localization.dll'') where all localized resources will be placed, so that other assemblies or applications can share it.   * Optionally, create an Resource-only Assembly (eg ''Acme.App.Localization.dll'') where all localized resources will be placed, so that other assemblies or applications can share it.
   * Create Resource file in local application or resource-only assembly.   * Create Resource file in local application or resource-only assembly.
-    *  Add new ''Resource.resx'' file to the project under ''Properties'' folder.+    * Add new ''Resource.resx'' file to the project under ''Properties'' folder.
     * Set Access Modifier to ''Public'' (top toolbar in resource document).     * Set Access Modifier to ''Public'' (top toolbar in resource document).
     * Add strings to the Resource file. Eg: ''rsClose'' = ''Close''     * Add strings to the Resource file. Eg: ''rsClose'' = ''Close''
Line 12: Line 86:
     * ''Resources.fr.resx'' for French Canadian (fr-CA), or potentially ''Resources.fr-CA.resx'' as well.     * ''Resources.fr.resx'' for French Canadian (fr-CA), or potentially ''Resources.fr-CA.resx'' as well.
   * Localize controls.     * Localize controls.  
-    * Add reference to ''Window'''s or ''UserControl'''s namespace in XAML. For example: <code xml><UserControl . . . +    * Add reference to ''Window'''s or ''UserControl'''s namespace in XAML. For example, if using resources within the application: <code xml><UserControl . . . 
-    xmlns:properties="clr-namespace:MyApp.Properties" +    xmlns:properties="clr-namespace:MyApp.Properties"
-    xmlns:localization="clr-namespace:Acme.App.Localization; assembly=Acme.App.Localization"    <!--if using an assembly...--> +  . . . 
-  >+</UserControl>   
 +</code> Or, if using a resource-only assembly instead: <code xml><UserControl . . . 
 +    xmlns:localization="clr-namespace:Acme.App.Localization; assembly=Acme.App.Localization">
   . . .   . . .
 </UserControl>   </UserControl>  
-</code>   +</code>  
-    * Add resource to a control. For example, assuming a resource string ''rsClose'' exists: ''<Button Name="btnClose" Content="{x:Static properties:Resources.rsClose}">''+    * Add resource to a control. For example, assuming a resource string ''rsClose'' exists: <code xml><Button Name="btnClose" Content="{x:Static properties:Resources.rsClose}"></code>
   * Set ''Culture'' before calling anything else in the application.  Modify ''App.xaml.cs'' to include: <code csharp>   * Set ''Culture'' before calling anything else in the application.  Modify ''App.xaml.cs'' to include: <code csharp>
 public App() public App()
Line 138: Line 214:
 } }
 </code>         </code>        
 +
 +=== Deployment ===
 +  * Copy application executable, and localization resource-only assembly, if using one. Eg: ''MyApp.exe'', ''Acme.App.Localization.dll''
 +  * Copy language subfolders with localized resources or resource-only assemblies. Eg: 
 +    * ''..\de\MyApp.resources.dll'', or ''..\de\Acme.App.Localization.resources.dll''
 +    * ''..\es\MyApp.resources.dll'', or ''..\es\Acme.App.Localization.resources.dll''
 +    * ''..\fr\MyApp.resources.dll'', or ''..\fr\Acme.App.Localization.resources.dll''
 +    * ''..\pt\MyApp.resources.dll'', or ''..\pt\Acme.App.Localization.resources.dll''
 +  * Create an application settings file with default language setting. Eg: File ''//MyApp//.exe.config'': <code xml>. . .
 +<userSettings>
 +  <MyApp.Properties.Settings>
 +    <setting name="Culture" serializeAs="String">
 +      <!--Pick only one value-->
 +      <value>de-DE</value>
 +    </setting>
 +    <setting name="UICulture" serializeAs="String">
 +      <!--Pick only one value-->
 +      <value>de-DE</value>
 +    </setting>
 +  </MyApp.Properties.Settings>
 +</userSettings>
 +. . .
 +</code>
  
 == Localization Sample == == Localization Sample ==
Line 651: Line 750:
 } }
 </code> </code>
 +
  
 = Reference Material = = Reference Material =
Line 658: Line 758:
   * [[http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx?display=Mobile&fid=1538725&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=101|WPF Localization Using ResX Files]]   * [[http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx?display=Mobile&fid=1538725&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=101|WPF Localization Using ResX Files]]
   * [[http://www.codeproject.com/KB/WPF/WPF_Localization.aspx|Simple WPF Localization]]   * [[http://www.codeproject.com/KB/WPF/WPF_Localization.aspx|Simple WPF Localization]]
 +  * [[swdev:dotnet:localization:Editing RESX Files with SimpleResxEditor]]