/// <summary>
        /// Initializes a new instance of the <see cref="SeeingSharpTranslator" /> class.
        /// </summary>
        internal SeeingSharpTranslator()
        {
            // Initialize members
            m_loadedFilesByCategory = new Dictionary <string, List <TranslationXmlFile> >();
            m_startupCulture        = CultureInfo.CurrentCulture;

            // Select current language which is to be loaded
            m_currentLanguage = SeeingSharpLanguageKey.Default;
            Enum.TryParse <SeeingSharpLanguageKey>(m_startupCulture.TwoLetterISOLanguageName.ToUpper(), out m_currentLanguage);
            if (m_currentLanguage == SeeingSharpLanguageKey.Default)
            {
                m_currentLanguage = SeeingSharpLanguageKey.EN;
            }
        }
        /// <summary>
        /// Loads a translation file from the given stream.
        /// </summary>
        /// <param name="fileName">The name of the file from which the given stream loads data.</param>
        /// <param name="inStream">The input stream.</param>
        private void LoadTranslationFile(string fileName, Stream inStream)
        {
            // Parse and set the language key (parsed from file name)
            //  .. return here if the language does not match the currently loaded
            //  .. load default language everytime
            SeeingSharpLanguageKey langKey = CommonTools.GetLanguageKeyFromFileName(fileName);

            if ((langKey != SeeingSharpLanguageKey.Default) &&
                (langKey != m_currentLanguage))
            {
                return;
            }

            // Deserialize the translation file
            XmlSerializer      xmlSerializer   = SerializerRepository.Current.GetSerializer <TranslationXmlFile>();
            TranslationXmlFile translationData = xmlSerializer.Deserialize(inStream) as TranslationXmlFile;

            translationData.LanguageKey = langKey;
            translationData.BuildDictionary();
            if (translationData == null)
            {
                return;
            }

            // Get the category of the language file
            string category = CommonTools.GetLanguageFileCategory(fileName);

            // Register loaded translation file in global dictionary
            List <TranslationXmlFile> fileList = null;

            if (!m_loadedFilesByCategory.TryGetValue(category, out fileList))
            {
                fileList = new List <TranslationXmlFile>();
                m_loadedFilesByCategory.Add(category, fileList);
            }

            // Place default language-key on first places
            if (langKey == SeeingSharpLanguageKey.Default)
            {
                fileList.Insert(0, translationData);
            }
            else
            {
                fileList.Add(translationData);
            }
        }
예제 #3
0
        /// <summary>
        /// Gets the language key from the given file name;
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        public static SeeingSharpLanguageKey GetLanguageKeyFromFileName(string fileName)
        {
            string[] splittedFileName = fileName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            if (splittedFileName.Length < 3)
            {
                return(SeeingSharpLanguageKey.Default);
            }

            // normaly the format is <filename>.<language>.<extension>
            string langKey = splittedFileName[splittedFileName.Length - 2]
                             .ToLower();

            if (!langKey.StartsWith("lang-"))
            {
                return(SeeingSharpLanguageKey.Default);
            }

            // Try to parse the language key
            SeeingSharpLanguageKey result = SeeingSharpLanguageKey.Default;

            Enum.TryParse <SeeingSharpLanguageKey>(langKey.Substring(5).ToUpper(), out result);
            return(result);
        }