All pastes #25139 Raw Edit

tobz

public text v1 · immutable
#25139 ·published 2005-10-10 16:04 UTC
rendered paste body
/*

 * DAWN OF LIGHT - The first free open source DAoC server emulator

 * 

 * This program is free software; you can redistribute it and/or

 * modify it under the terms of the GNU General Public License

 * as published by the Free Software Foundation; either version 2

 * of the License, or (at your option) any later version.

 * 

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 * 

 * You should have received a copy of the GNU General Public License

 * along with this program; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 *

 */

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Reflection;

using System.Text;

using System.Text.RegularExpressions;

using log4net;



namespace DOL.GS

{

    /// <summary>

    /// LanguageMgr take care about all support of multi-language on server

    /// </summary>

    public class LanguageMgr

    {

        #region Variables

        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);



        private static SortedList<String, SortedList<String, String>> m_languageArray;



        private static char m_dirSep = Path.DirectorySeparatorChar;



        private static string m_lngDir = "Language";



        private static string m_lngRegex = @"[(.*)] = [(.*)]";

        #endregion



        /// <summary>

        /// Initializes the LanguageMgr

        /// </summary>

        /// <returns>True if initialization succeeded</returns>

        public static bool Initialize()

        {

            m_languageArray = new SortedList<String, SortedList<String, String>>(StringComparer.CurrentCultureIgnoreCase);



            try

            {

                string path = "." + m_dirSep + m_lngDir + m_dirSep;

                ParseDirectory(path, "*.lng");

            }

            catch (Exception e)

            {

                if (log.IsErrorEnabled)

                    log.Error("Language file exception", e);



                return false;

            }



            return true;

        }



        /// <summary>

        /// Retrieves a stored key/value pair, and returns a default string if the specified key doesn't exist

        /// </summary>

        /// <param name="language">The language to look at</param>

        /// <param name="key">The key for the string</param>

        /// <param name="defaultString">The default string to return</param>

        /// <returns>The value associated with the key, or the default string</returns>

        public static string GetString(string language, string key, string defaultString)

        {

            SortedList<String, String> langDict = m_languageArray[language.ToLower()];

            if (langDict == null) return defaultString;



            if (tempDict[key] == null) return defaultString;



            return tempDict[key];

        }



        /// <summary>

        /// Parses a directory for all source files

        /// </summary>

        /// <param name="path">The root directory to start the search in</param>

        /// <param name="filter">A filter representing the types of files to search for</param>

        private static void ParseDirectory(string path, string filter)

        {

            if (!Directory.Exists(path))

            {

                Directory.CreateDirectory(path);

            }



            string[] dirs = Directory.GetDirectories(path);

            foreach (string dir in dirs)

            {

                ParseDirectory(dir, filter);

            }



            string[] files = Directory.GetFiles(path, filter);

            foreach (string file in files)

            {

                ParseLanguageFile(file);

            }

        }



        /// <summary>

        /// Parses a language file and stores all key/value pairs found

        /// </summary>

        /// <param name="file">The file name of the language file to parse</param>

        private static void ParseLanguageFile(string file)

        {

            if (!File.Exists(file))

            {

                throw new Exception("Invalid file: " + file);

            }



            using (StreamReader filereader = File.OpenText(file))

            {

                // Figure out the language of the file.

                FileInfo lngFileInfo = new FileInfo(file);

                string languageType = lngFileInfo.Name.Remove(lngFileInfo.Name.Length - 4);



                SortedList<String, String> languageDict = new SortedList<String, String>(StringComparer.CurrentCultureIgnoreCase);



                string line;

                while ((line = filereader.ReadLine()) != null)

                {

                    // Try and match a key/value pair.

                    Regex entryRegex = new Regex(m_lngRegex);

                    Match entryMatcher = entryRegex.Match(line);



                    if (entryMatcher.Groups.Count == 2)

                    {

                        // We have a match, store it.

                        languageDict.Add(entryMatcher.Groups[0].Value, entryMatcher.Groups[1].Value);

                    }

                }



                m_languageArray.Add(languageType.ToLower(), languageDict);

            }

        }

    }

}