/* * 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 { /// /// LanguageMgr take care about all support of multi-language on server /// public class LanguageMgr { #region Variables private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static SortedList> m_languageArray; private static char m_dirSep = Path.DirectorySeparatorChar; private static string m_lngDir = "Language"; private static string m_lngRegex = @"[(.*)] = [(.*)]"; #endregion /// /// Initializes the LanguageMgr /// /// True if initialization succeeded public static bool Initialize() { m_languageArray = new SortedList>(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; } /// /// Retrieves a stored key/value pair, and returns a default string if the specified key doesn't exist /// /// The language to look at /// The key for the string /// The default string to return /// The value associated with the key, or the default string public static string GetString(string language, string key, string defaultString) { SortedList langDict = m_languageArray[language.ToLower()]; if (langDict == null) return defaultString; if (tempDict[key] == null) return defaultString; return tempDict[key]; } /// /// Parses a directory for all source files /// /// The root directory to start the search in /// A filter representing the types of files to search for 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); } } /// /// Parses a language file and stores all key/value pairs found /// /// The file name of the language file to parse 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 languageDict = new SortedList(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); } } } }