Subfolders of the project structure. NET for each language.

I created a new C # class library project that will contain resource files for all translations. The examples I saw have a flat structure with one giant resource file for each such language:

  • Messages.resx
  • Messages.fr-FR.resx

But for this particular project, it is preferable to split the resource files into logical groups:

  • SystemMessages.resx
  • UserMessages.resx
  • SystemMessages.fr-FR.resx
  • UserMessages.fr-FR.resx

This will be messy when we add more resource files and more translations, so it would be more manageable to have subfolders for each language, for example.

  • en-GB \ SystemMessages.resx
  • en-GB \ UserMessages.resx
  • FR-FR \ SystemMessages.fr-FR.resx
  • FR-FR \ UserMessages.fr-FR.resx

, . Translations.en-GB.SystemMessages.MyString, , en-GB , fr-FR.

?

+4
1

, :

enter image description here

ResourceManager .
T4 ( )

using ConsoleApplication6.Translations.French;
using System;
using System.Resources;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(SystemMessagesManager.GetString("Title"));
            Console.ReadLine();
        }

       public static class SystemMessagesManager
       {
           static ResourceManager rsManager;
           static SystemMessagesManager()
           {
               //Get the current manager based on the current Culture
              if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
                   rsManager = SystemMessagesFrench.ResourceManager;
              else if (Thread.CurrentThread.CurrentCulture.Name == "el-GR")
                   rsManager = SystemMessagesGreek.ResourceManager;
              else
                   rsManager = SystemMessagesEnglish.ResourceManager;
           }
           public static string GetString(string Key)
           {
              return rsManager.GetString(Key) ?? SystemMessagesEnglish.ResourceManager.GetString(Key);
           }
       }
   }
}

T4, :
resx T4
.
, : enter image description here

, :

<#@ template debug="false" hostspecific="true" language="C#" #>

<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Linq" #>

<#@ output extension=".cs" #>
using System.Resources;
using System.Threading;

namespace YourNameSpace
{
    public static class SystemMessagesManager
    {       
        static ResourceManager rsManager;
        static SystemMessagesManager()
        {
               //Get the current manager based on the current Culture
              if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
                    rsManager = SystemMessagesFrench.ResourceManager;
              else if (Thread.CurrentThread.CurrentCulture.Name == "el-GR")
                    rsManager = SystemMessagesGreek.ResourceManager;
              else
                    rsManager = SystemMessagesEnglish.ResourceManager;
         }
        private static string GetString(string Key)
        {
              return rsManager.GetString(Key) ?? SystemMessagesEnglish.ResourceManager.GetString(Key);
        }

        <# 
         XmlDocument xml = new XmlDocument();        
         xml.Load(Host.ResolvePath(@"ResourceStrings.resx"));

         foreach(string key in xml.SelectNodes("root/data").Cast<XmlNode>().Select(xn => xn.Attributes["name"].Value)){
         #>
public static string <#= key #> { get { return GetString("<#=key#>"); } }
        <# } #>                 
    }
}
+1

All Articles