Example #1
0
 /// <summary>
 /// Sets (and stores) a language to this Manager
 /// Also refreshes all RegisterText-objects
 /// </summary>
 /// <param name="ISO_Code">ISO-Code of Language to set</param>
 public static void SetLanguage(ISOCode639 ISO_Code)
 {
     LoggingLevel.Info.Log(string.Format("LanguageManager: Setting Language from ISO-Code [{0}]", ISO_Code), instance.GetLoggingLevel(), instance);
     SetLanguage(ISO_Code.GetLanguage());
 }
Example #2
0
        /// <summary>
        /// Directly loads in a LanguageMap (from string)
        /// </summary>
        /// <param name="map">LanguageMap to load in</param>
        /// <param name="callBack">CallBackResponse used to check parse-status (if Data = true, execution has finished)</param>
        internal static void LoadLanguageMap(string map, Action <CallBackResponse <bool> > callBack = null, bool fallbackVersion = false)
        {
            LoggingLevel log = LanguageManager.Instance.GetLoggingLevel();

            language_map = new Dictionary <string, Dictionary <Language, string> >();
            callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING));
            LoggingLevel.Info.Log(string.Format("LanguageParser: Parsing LanguageMap: {0}", map), log);
            string[] lines = map.Split(new [] { LanguageManager.Instance.NewLineStr }, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length.Equals(0) && !fallbackVersion)
            {
                LoggingLevel.Error.Log("LanguageParser: Found 0 Lines. Newline-Error?", log);
                if (!fallbackVersion)
                {
                    LoadLanguageMap(LanguageManager.Instance.LanguageMap.text, callBack, true);
                }
                else
                {
                    callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING_FAILED, true));
                }
                return;
            }
            LoggingLevel.Debug.Log(string.Format("LanguageParser: [PARSE] Found [{0}] Lines", lines.LongLength), log);
            string[] column       = lines[0].Split(new [] { LanguageManager.Instance.Delimiter }, StringSplitOptions.None);
            int      columnAmount = column.Length; // ColumnSize

            LoggingLevel.Debug.Log(string.Format("LanguageParser: [PARSE] Parsing [{0}] Languages", column.Length - 1), log);
            // Load Language from Line 1
            Dictionary <int, Language> columnNo = new Dictionary <int, Language>();

            for (int i = 1; i < column.Length; i++)
            {
                string text = column[i];
                LoggingLevel.Debug.Log(string.Format("LanguageParser: [PARSE] Parsing column [{0}], Value: [{1}]", i, text), log);
                try
                {
                    int test;
                    // Prevent parsing an int-value to a language
                    if (int.TryParse(text, out test))
                    {
                        throw new ArgumentException();
                    }
                    Language l = (Language)Enum.Parse(typeof(Language), text);
                    if (Enum.IsDefined(typeof(Language), l))
                    {
                        columnNo.Add(i, l);
                        continue;
                    }
                    ISOCode639 iso = (ISOCode639)Enum.Parse(typeof(Language), text);
                    if (Enum.IsDefined(typeof(ISOCode639), iso))
                    {
                        columnNo.Add(i, iso.GetLanguage());
                        continue;
                    }
                    SystemLanguage language = (SystemLanguage)Enum.Parse(typeof(SystemLanguage), text);
                    if (Enum.IsDefined(typeof(SystemLanguage), language))
                    {
                        columnNo.Add(i, language.GetLanguage());
                        continue;
                    }
                    throw new ArgumentException();
                }
                catch (ArgumentException)
                {
                    LoggingLevel.Warning.Log(string.Format("LanguageParser: [PARSE] Invalid Language: [{0}] at Column: [{1}]", text, i), log);
                    callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING, false));
                }
                catch (Exception e)
                {
                    LoggingLevel.Error.Log(string.Format("LanguageParser: [PARSE] An error occurred , {0}", e), log);
                    callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING, true));
                    throw;
                }
            }
            if (columnNo.Count.Equals(0))
            {
                LoggingLevel.Error.Log("LanguageParser: [PARSE] No valid Languages found", log);
                callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING_FAILED, true));
                return;
            }
            LoggingLevel.Info.Log(string.Format("LanguageParser: [PARSE] Parsed [{0}] valid Languages", columnNo.Keys.Count), log);
            // Load Values
            for (int i = 1; i < lines.Length; i++)
            {
                try
                {
                    column = lines[i].Split(new char[] { LanguageManager.Instance.Delimiter }, StringSplitOptions.None);
                    string key = column[0];
                    if (!column.Length.Equals(columnAmount))
                    {
                        LoggingLevel.Error.Log("Failed line: " + lines[i], log);
                        LoggingLevel.Error.Log(string.Format("LanguageParser: [PARSE] Parsing Error on Line [{0}] - Invalid ColumnSize (Delimiter Error?). Skipping Line", i), log);
                        callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING, false));
                        continue;
                    }
                    if (string.IsNullOrEmpty(key))
                    {
                        LoggingLevel.Warning.Log(string.Format("LanguageParser: [PARSE] Parsing Error on Line [{0}] - Empty Key. Skipping Line", i), log);
                        callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING, false));
                        continue;
                    }
                    try
                    {
                        language_map.Add(key, new Dictionary <Language, string>());
                        LoggingLevel.Debug.Log(string.Format("LanguageParser: [PARSE] Parsing Line [{0}] - Added Key [{1}]", i, key), log);
                    }
                    catch (ArgumentException)
                    {
                        LoggingLevel.Warning.Log(string.Format("LanguageParser: [PARSE] Parsing Error on Line [{0}] - Duplicate Key Entry [{1}]. Skipping Line", i, key), log);
                        callBack?.Invoke(new CallBackResponse <bool>(LoadingState.OUTPUT_PARSING, false));
                        continue;
                    }
                    for (int j = 1; j < column.Length; j++)
                    {
                        string text = column[j];
                        text = text.Replace('|', ',');
                        text = text.Replace("\\n", "\n");
                        if (string.IsNullOrEmpty(text))
                        {
                            LoggingLevel.Debug.Log(string.Format("LanguageParser: [PARSE] Empty value for Line [{0}] Column [{1}]", i, j), log);
                            continue;
                        }
                        if (!columnNo.ContainsKey(j))
                        {
                            LoggingLevel.Debug.Log(string.Format("LanguageParser: [PARSE] No Language for Line [{0}] Column [{1}] for Value [{2}]", i, j, text), log);
                            continue;
                        }
                        language_map[key].Add(columnNo[j], text);
                        LoggingLevel.Development.Log(string.Format("LanguageParser: [PARSE] [L={0},C={1}] Parsed [{2}] to Key [{3}] in Language [{4}]", i, j, text, key, columnNo[j]), log);
                    }
                }
                catch (Exception)
                {
                    LoggingLevel.Error.Log(string.Format("LanguageParser: [PARSE] Error on Line : [{0}] - Stopping Parse.", i), log);
                    throw;
                }
            }
            LoggingLevel.Info.Log("LanguageParser: Parse Completed", log);
            callBack?.Invoke(new CallBackResponse <bool>(true));
        }