/// <summary>
        /// Constructor that searches for *.Localization.xml files in the language-specific
        /// sub-directory of the Resources sub-directory</summary>
        public EmbeddedResourceStringLocalizer()
        {
            // Get our current language and culture identifiers for the directory names.
            string language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName; //"en" or "ja"
            string culture  = Thread.CurrentThread.CurrentUICulture.Name;                     //"en-US" or "ja-JP"

            // In the satellite assembly model, the English version is in the root directory plus our
            //  source code is written in English. No use doing a lot of searching for English translations.
            // Yes, this doesn't work if we port ATF to English-Australia. Unlikely!
            if (language == "en")
            {
                return;
            }

            // To speed things up, only check the GAC if this assembly has been installed in the GAC.
            // This allows us to skip mscorlib, etc.
            bool searchGAC = Assembly.GetExecutingAssembly().GlobalAssemblyCache;

            // Resources are named like "Some.Random.Namespace.Resources.ja.Another.Random.Name.Localization.xml".
            // Let's search for ".Resources.ja" to make sure we're locating the correct file.
            m_resourceDirectory1 = ".Resources." + language + ".";
            m_resourceDirectory2 = ".Resources." + culture + ".";

            foreach (Assembly assembly in AssemblyUtil.GetLoadedAssemblies())
            {
                if (searchGAC ||
                    !assembly.GlobalAssemblyCache)
                {
                    LoadEmeddedResources(assembly);
                }
            }

            // Subscribe to new assemblies
            AppDomain.CurrentDomain.AssemblyLoad += CurrentDomainOnAssemblyLoad;
        }
        /// <summary>
        /// Constructor that searches for *.Localization.xml files in the language-specific
        /// sub-directory of the Resources sub-directory</summary>
        public EmbeddedResourceStringLocalizer()
        {
            // Get our current language and culture identifiers for the directory names.
            string language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName; //"en" or "ja"
            string culture  = Thread.CurrentThread.CurrentUICulture.Name;                     //"en-US" or "ja-JP"

            // Embedded resource namespaces can't contain certain characters and get renamed
            //  automatically by the compiler. So, '-' got replaced by '_' while compiling.
            // https://msdn.microsoft.com/en-us/library/ms145952.aspx
            culture = culture.Replace('-', '_');

            // To speed things up, only check the GAC if this assembly has been installed in the GAC.
            // This allows us to skip mscorlib, etc.
            bool searchGAC = Assembly.GetExecutingAssembly().GlobalAssemblyCache;

            // Resources are named like "Some.Random.Namespace.Resources.ja.Another.Random.Name.Localization.xml".
            // Let's search for ".Resources.ja" to make sure we're locating the correct file.
            m_resourceDirectory1 = ".Resources." + language + ".";
            m_resourceDirectory2 = ".Resources." + culture + ".";

            foreach (Assembly assembly in AssemblyUtil.GetLoadedAssemblies())
            {
                if (searchGAC ||
                    !assembly.GlobalAssemblyCache)
                {
                    LoadEmeddedResources(assembly);
                }
            }

            // Subscribe to new assemblies
            AppDomain.CurrentDomain.AssemblyLoad += CurrentDomainOnAssemblyLoad;
        }
Exemple #3
0
        /// <summary>
        /// Constructor that searches for *.Localization.xml files in the language-specific
        /// sub-directory of the Resources sub-directory</summary>
        public EmbeddedResourceStringLocalizer()
        {
            // Get our current language and culture identifiers for the directory names.
            string language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName; //"en" or "ja"
            string culture  = Thread.CurrentThread.CurrentUICulture.Name;                     //"en-US" or "ja-JP"

            // In the satellite assembly model, the English version is in the root directory plus our
            //  source code is written in English. No use doing a lot of searching for English translations.
            // Yes, this doesn't work if we port ATF to English-Australia. Unlikely!
            if (language == "en")
            {
                return;
            }

            // To speed things up, only check the GAC if this assembly has been installed in the GAC.
            // This allows us to skip mscorlib, etc.
            bool searchGAC = Assembly.GetExecutingAssembly().GlobalAssemblyCache;

            // Resources are named like "Some.Random.Namespace.Resources.ja.Another.Random.Name.Localization.xml".
            // Let's search for ".Resources.ja" to make sure we're locating the correct file.
            string resourceDirectory1 = ".Resources." + language + ".";
            string resourceDirectory2 = ".Resources." + culture + ".";

            foreach (Assembly assembly in AssemblyUtil.GetLoadedAssemblies())
            {
                //dynamically generated assemblies don't implement GetManifestResourceXxx()
                if (assembly is AssemblyBuilder)
                {
                    continue;
                }

                if (searchGAC ||
                    !assembly.GlobalAssemblyCache)
                {
                    foreach (string resourceName in assembly.GetManifestResourceNames())
                    {
                        if (IsEmbeddedResource(resourceName, resourceDirectory1, resourceDirectory2))
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(assembly.GetManifestResourceStream(resourceName));
                            AddLocalizedStrings(xmlDoc);
                        }
                    }
                }
            }
        }