Example #1
0
            /// <summary>Creates a new language from a file stream</summary>
            /// <param name="languageStream">The file stream</param>
            /// <param name="languageCode">The language code</param>
            internal Language(Stream languageStream, string languageCode)
            {
                Name          = "Unknown";
                LanguageCode  = languageCode;
                FallbackCodes = new List <string> {
                    "en-US"
                };
                InterfaceStrings  = new InterfaceString[16];
                myCommandInfos    = new CommandInfo[CommandInfos.Length];
                KeyInfos          = new KeyInfo[TranslatedKeys.Length];
                myQuickReferences = new InterfaceQuickReference();
                Array.Copy(CommandInfos, myCommandInfos, myCommandInfos.Length);
                Array.Copy(TranslatedKeys, KeyInfos, TranslatedKeys.Length);

                XDocument  xml   = XDocument.Load(languageStream);
                XNamespace xmlns = xml.Root.Name.Namespace;
                XElement   body  = xml.Root.Element(xmlns + "file").Element(xmlns + "body");

                int LoadedStringCount = 0;

                foreach (XElement groupProduct in body.Elements(xmlns + "group"))
                {
                    switch (groupProduct.Attribute("id").Value)
                    {
                    case "language":
                        foreach (XElement trans_unit in groupProduct.Elements(xmlns + "trans-unit"))
                        {
                            XElement source = trans_unit.Element(xmlns + "source");
                            XElement target = trans_unit.Element(xmlns + "target");

                            if (target == null && LanguageCode != "en-US")
                            {
                                continue;
                            }

                            string key  = trans_unit.Attribute("id").Value;
                            string text = LanguageCode != "en-US" ? target.Value : source.Value;

                            switch (key)
                            {
                            case "name":
                                Name = text;
                                break;

                            case "flag":
                                Flag = text;
                                break;
                            }
                        }
                        break;

                    default:
                    {
                        string productName = groupProduct.Attribute("id").Value;

                        if (productName == "openbve")
                        {
                            foreach (XElement group in groupProduct.Elements(xmlns + "group"))
                            {
                                foreach (XElement trans_unit in group.Elements(xmlns + "trans-unit"))
                                {
                                    XElement source = trans_unit.Element(xmlns + "source");
                                    XElement target = trans_unit.Element(xmlns + "target");

                                    if (target == null && LanguageCode != "en-US")
                                    {
                                        continue;
                                    }

                                    string section = group.Attribute("id").Value;
                                    string key     = trans_unit.Attribute("id").Value;
                                    string text    = LanguageCode != "en-US" ? target.Value : source.Value;
                                    text = text.Replace("\\n", Environment.NewLine);
                                    text = text.Replace("\\x20", " ");
                                    switch (section)
                                    {
                                    case "handles":
                                        switch (key)
                                        {
                                        case "forward":
                                            myQuickReferences.HandleForward = text;
                                            break;

                                        case "neutral":
                                            myQuickReferences.HandleNeutral = text;
                                            break;

                                        case "backward":
                                            myQuickReferences.HandleBackward = text;
                                            break;

                                        case "power":
                                            myQuickReferences.HandlePower = text;
                                            break;

                                        case "powernull":
                                            myQuickReferences.HandlePowerNull = text;
                                            break;

                                        case "brake":
                                            myQuickReferences.HandleBrake = text;
                                            break;

                                        case "locobrake":
                                            myQuickReferences.HandleLocoBrake = text;
                                            break;

                                        case "brakenull":
                                            myQuickReferences.HandleBrakeNull = text;
                                            break;

                                        case "release":
                                            myQuickReferences.HandleRelease = text;
                                            break;

                                        case "lap":
                                            myQuickReferences.HandleLap = text;
                                            break;

                                        case "service":
                                            myQuickReferences.HandleService = text;
                                            break;

                                        case "emergency":
                                            myQuickReferences.HandleEmergency = text;
                                            break;

                                        case "holdbrake":
                                            myQuickReferences.HandleHoldBrake = text;
                                            break;
                                        }
                                        break;

                                    case "doors":
                                        switch (key)
                                        {
                                        case "left":
                                            myQuickReferences.DoorsLeft = text;
                                            break;

                                        case "right":
                                            myQuickReferences.DoorsRight = text;
                                            break;
                                        }
                                        break;

                                    case "misc":
                                        switch (key)
                                        {
                                        case "score":
                                            myQuickReferences.Score = text;
                                            break;
                                        }
                                        break;

                                    case "commands":
                                        for (int k = 0; k < myCommandInfos.Length; k++)
                                        {
                                            if (string.Compare(myCommandInfos[k].Name, key, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                myCommandInfos[k].Description = text;
                                                break;
                                            }
                                        }
                                        break;

                                    case "keys":
                                        for (int k = 0; k < KeyInfos.Length; k++)
                                        {
                                            if (string.Compare(KeyInfos[k].Name, key, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                KeyInfos[k].Description = text;
                                                break;
                                            }
                                        }
                                        break;

                                    case "fallback":
                                        switch (key)
                                        {
                                        case "language":
                                            FallbackCodes.Add(text);
                                            break;
                                        }
                                        break;

                                    default:
                                        if (LoadedStringCount >= InterfaceStrings.Length)
                                        {
                                            Array.Resize(ref InterfaceStrings, InterfaceStrings.Length << 1);
                                        }

                                        InterfaceStrings[LoadedStringCount].Name = string.Format("{0}_{1}", section, key);
                                        InterfaceStrings[LoadedStringCount].Text = text;
                                        LoadedStringCount++;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            foreach (XElement group in groupProduct.Elements(xmlns + "group"))
                            {
                                foreach (XElement trans_unit in group.Elements(xmlns + "trans-unit"))
                                {
                                    XElement source = trans_unit.Element(xmlns + "source");
                                    XElement target = trans_unit.Element(xmlns + "target");

                                    if (target == null && LanguageCode != "en-US")
                                    {
                                        continue;
                                    }

                                    string section = group.Attribute("id").Value;
                                    string key     = trans_unit.Attribute("id").Value;
                                    string text    = LanguageCode != "en-US" ? target.Value : source.Value;
                                    text = text.Replace("\\n", Environment.NewLine);
                                    text = text.Replace("\\x20", " ");
                                    if (LoadedStringCount >= InterfaceStrings.Length)
                                    {
                                        Array.Resize(ref InterfaceStrings, InterfaceStrings.Length << 1);
                                    }

                                    InterfaceStrings[LoadedStringCount].Name = string.Format("{0}_{1}_{2}", productName, section, key);
                                    InterfaceStrings[LoadedStringCount].Text = text;
                                    LoadedStringCount++;
                                }
                            }
                        }
                    }
                    break;
                    }
                }

                Array.Resize(ref InterfaceStrings, LoadedStringCount);
            }
        /// <summary>Adds a language file to the available langauge list</summary>
        /// <param name="File">The absolute on-disk path to the language file we wish to load</param>
        internal static void AddLanguage(string File)
        {
            //Create new language
            Language newLanguage = new Language
            {
                Name          = "Unknown",
                LanguageCode  = System.IO.Path.GetFileNameWithoutExtension(File),
                FallbackCodes = new List <string>()
            };

            try
            {
                string[]                Lines          = System.IO.File.ReadAllLines(File, new System.Text.UTF8Encoding());
                string                  Section        = "";
                InterfaceString[]       LoadedStrings  = new InterfaceString[16];
                CommandInfo[]           LoadedCommands = new CommandInfo[Interface.CommandInfos.Length];
                KeyInfo[]               LoadedKeys     = new KeyInfo[Interface.TranslatedKeys.Length];
                InterfaceQuickReference QuickReference = new InterfaceQuickReference();
                Array.Copy(Interface.CommandInfos, LoadedCommands, Interface.CommandInfos.Length);
                Array.Copy(Interface.TranslatedKeys, LoadedKeys, Interface.TranslatedKeys.Length);
                var LoadedStringCount = 0;
                for (int i = 0; i < Lines.Length; i++)
                {
                    Lines[i] = Lines[i].Trim();
                    if (!Lines[i].StartsWith(";"))
                    {
                        if (Lines[i].StartsWith("[", StringComparison.Ordinal) & Lines[i].EndsWith("]", StringComparison.Ordinal))
                        {
                            Section = Lines[i].Substring(1, Lines[i].Length - 2).Trim().ToLowerInvariant();
                        }
                        else
                        {
                            int j = Lines[i].IndexOf('=');
                            if (j >= 0)
                            {
                                string a = Lines[i].Substring(0, j).TrimEnd().ToLowerInvariant();
                                string b = Interface.Unescape(Lines[i].Substring(j + 1).TrimStart());
                                switch (Section)
                                {
                                case "handles":
                                    switch (a)
                                    {
                                    case "forward": QuickReference.HandleForward = b; break;

                                    case "neutral": QuickReference.HandleNeutral = b; break;

                                    case "backward": QuickReference.HandleBackward = b; break;

                                    case "power": QuickReference.HandlePower = b; break;

                                    case "powernull": QuickReference.HandlePowerNull = b; break;

                                    case "brake": QuickReference.HandleBrake = b; break;

                                    case "locobrake": QuickReference.HandleLocoBrake = b; break;

                                    case "brakenull": QuickReference.HandleBrakeNull = b; break;

                                    case "release": QuickReference.HandleRelease = b; break;

                                    case "lap": QuickReference.HandleLap = b; break;

                                    case "service": QuickReference.HandleService = b; break;

                                    case "emergency": QuickReference.HandleEmergency = b; break;

                                    case "holdbrake": QuickReference.HandleHoldBrake = b; break;
                                    }
                                    break;

                                case "doors":
                                    switch (a)
                                    {
                                    case "left": QuickReference.DoorsLeft = b; break;

                                    case "right": QuickReference.DoorsRight = b; break;
                                    }
                                    break;

                                case "misc":
                                    switch (a)
                                    {
                                    case "score": QuickReference.Score = b; break;
                                    }
                                    break;

                                case "commands":
                                {
                                    for (int k = 0; k < LoadedCommands.Length; k++)
                                    {
                                        if (string.Compare(LoadedCommands[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                        {
                                            LoadedCommands[k].Description = b;
                                            break;
                                        }
                                    }
                                } break;

                                case "keys":
                                {
                                    for (int k = 0; k < LoadedKeys.Length; k++)
                                    {
                                        if (string.Compare(LoadedKeys[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                        {
                                            LoadedKeys[k].Description = b;
                                            break;
                                        }
                                    }
                                } break;

                                case "fallback":
                                    switch (a)
                                    {
                                    case "language": newLanguage.FallbackCodes.Add(b); break;
                                    }
                                    break;

                                case "language":
                                    switch (a)
                                    {
                                    case "name": newLanguage.Name = b; break;

                                    case "flag": newLanguage.Flag = b; break;
                                    }
                                    break;

                                default:
                                    if (LoadedStringCount >= LoadedStrings.Length)
                                    {
                                        Array.Resize <InterfaceString>(ref LoadedStrings,
                                                                       LoadedStrings.Length << 1);
                                    }
                                    LoadedStrings[LoadedStringCount].Name = Section + "_" + a;
                                    LoadedStrings[LoadedStringCount].Text = b;
                                    LoadedStringCount++;
                                    break;
                                }
                            }
                        }
                    }
                }
                newLanguage.InterfaceStrings     = LoadedStrings;
                newLanguage.CommandInfos         = LoadedCommands;
                newLanguage.KeyInfos             = LoadedKeys;
                newLanguage.InterfaceStringCount = LoadedStringCount;
                newLanguage.QuickReferences      = QuickReference;
                //We should always fall-back to en-US as the last-resort before failing to load a string
                newLanguage.FallbackCodes.Add("en-US");
                AvailableLangauges.Add(newLanguage);
            }
            catch (Exception)
            {
                //This message is shown when loading a language fails, and must not be translated, as otherwise it could produce a blank error message
                MessageBox.Show(@"An error occurred whilst attempting to load the language file: \n \n" + File);
                Environment.Exit(0);
            }
        }
Example #3
0
            /// <summary>Creates a new language from an on-disk language file</summary>
            /// <param name="languageFile">The absolute on-disk path to the language file we wish to load</param>
            internal Language(string languageFile)
            {
                Name          = "Unknown";
                LanguageCode  = System.IO.Path.GetFileNameWithoutExtension(languageFile);
                FallbackCodes = new List <string> {
                    "en-US"
                };
                InterfaceStrings  = new InterfaceString[16];
                myCommandInfos    = new CommandInfo[Translations.CommandInfos.Length];
                KeyInfos          = new KeyInfo[TranslatedKeys.Length];
                myQuickReferences = new InterfaceQuickReference();
                Array.Copy(Translations.CommandInfos, myCommandInfos, myCommandInfos.Length);
                Array.Copy(TranslatedKeys, KeyInfos, TranslatedKeys.Length);
                try
                {
                    string[] Lines             = File.ReadAllLines(languageFile, new System.Text.UTF8Encoding());
                    string   Section           = "";
                    var      LoadedStringCount = 0;
                    for (int i = 0; i < Lines.Length; i++)
                    {
                        Lines[i] = Lines[i].Trim();
                        if (!Lines[i].StartsWith(";"))
                        {
                            if (Lines[i].StartsWith("[", StringComparison.Ordinal) & Lines[i].EndsWith("]", StringComparison.Ordinal))
                            {
                                Section = Lines[i].Substring(1, Lines[i].Length - 2).Trim().ToLowerInvariant();
                            }
                            else
                            {
                                int j = Lines[i].IndexOf('=');
                                if (j >= 0)
                                {
                                    string a = Lines[i].Substring(0, j).TrimEnd().ToLowerInvariant();
                                    string b = Lines[i].Substring(j + 1).TrimStart().Unescape();
                                    switch (Section)
                                    {
                                    case "handles":
                                        switch (a)
                                        {
                                        case "forward":
                                            myQuickReferences.HandleForward = b;
                                            break;

                                        case "neutral":
                                            myQuickReferences.HandleNeutral = b;
                                            break;

                                        case "backward":
                                            myQuickReferences.HandleBackward = b;
                                            break;

                                        case "power":
                                            myQuickReferences.HandlePower = b;
                                            break;

                                        case "powernull":
                                            myQuickReferences.HandlePowerNull = b;
                                            break;

                                        case "brake":
                                            myQuickReferences.HandleBrake = b;
                                            break;

                                        case "locobrake":
                                            myQuickReferences.HandleLocoBrake = b;
                                            break;

                                        case "brakenull":
                                            myQuickReferences.HandleBrakeNull = b;
                                            break;

                                        case "release":
                                            myQuickReferences.HandleRelease = b;
                                            break;

                                        case "lap":
                                            myQuickReferences.HandleLap = b;
                                            break;

                                        case "service":
                                            myQuickReferences.HandleService = b;
                                            break;

                                        case "emergency":
                                            myQuickReferences.HandleEmergency = b;
                                            break;

                                        case "holdbrake":
                                            myQuickReferences.HandleHoldBrake = b;
                                            break;
                                        }
                                        break;

                                    case "doors":
                                        switch (a)
                                        {
                                        case "left":
                                            myQuickReferences.DoorsLeft = b;
                                            break;

                                        case "right":
                                            myQuickReferences.DoorsRight = b;
                                            break;
                                        }
                                        break;

                                    case "misc":
                                        switch (a)
                                        {
                                        case "score":
                                            myQuickReferences.Score = b;
                                            break;
                                        }
                                        break;

                                    case "commands":
                                        for (int k = 0; k < myCommandInfos.Length; k++)
                                        {
                                            if (string.Compare(myCommandInfos[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                myCommandInfos[k].Description = b;
                                                break;
                                            }
                                        }
                                        break;

                                    case "keys":
                                        for (int k = 0; k < KeyInfos.Length; k++)
                                        {
                                            if (string.Compare(KeyInfos[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                KeyInfos[k].Description = b;
                                                break;
                                            }
                                        }
                                        break;

                                    case "fallback":
                                        switch (a)
                                        {
                                        case "language":
                                            FallbackCodes.Add(b);
                                            break;
                                        }
                                        break;

                                    case "language":
                                        switch (a)
                                        {
                                        case "name":
                                            Name = b;
                                            break;

                                        case "flag":
                                            Flag = b;
                                            break;
                                        }
                                        break;

                                    default:
                                        if (LoadedStringCount >= InterfaceStrings.Length)
                                        {
                                            Array.Resize <InterfaceString>(ref InterfaceStrings,
                                                                           InterfaceStrings.Length << 1);
                                        }

                                        InterfaceStrings[LoadedStringCount].Name = Section + "_" + a;
                                        InterfaceStrings[LoadedStringCount].Text = b;
                                        LoadedStringCount++;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    Array.Resize(ref InterfaceStrings, LoadedStringCount);
                }
                catch
                {
                    //This message is shown when loading a language fails, and must not be translated, as otherwise it could produce a blank error message
                    MessageBox.Show(@"An error occurred whilst attempting to load the language file: \n \n" + languageFile);
                    //Pass the exception down the line
                    //TODO: Not currently handled specifically, but may be in future
                    throw;
                }
            }
Example #4
0
            /// <summary>Creates a new language from a file stream</summary>
            /// <param name="languageStream">The file stream</param>
            /// <param name="languageCode">The language code</param>
            internal Language(Stream languageStream, string languageCode)
            {
                Name          = "Unknown";
                LanguageCode  = languageCode;
                FallbackCodes = new List <string> {
                    "en-US"
                };
                InterfaceStrings  = new InterfaceString[16];
                myCommandInfos    = new CommandInfo[Translations.CommandInfos.Length];
                KeyInfos          = new KeyInfo[TranslatedKeys.Length];
                myQuickReferences = new InterfaceQuickReference();
                Array.Copy(Translations.CommandInfos, myCommandInfos, myCommandInfos.Length);
                Array.Copy(TranslatedKeys, KeyInfos, TranslatedKeys.Length);
                try
                {
                    string[] Lines;
                    using (StreamReader reader = new StreamReader(languageStream))
                    {
                        Lines = reader.ReadToEnd().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    }

                    string Section           = "";
                    var    LoadedStringCount = 0;
                    for (int i = 0; i < Lines.Length; i++)
                    {
                        Lines[i] = Lines[i].Trim();
                        if (!Lines[i].StartsWith(";"))
                        {
                            if (Lines[i].StartsWith("[", StringComparison.Ordinal) & Lines[i].EndsWith("]", StringComparison.Ordinal))
                            {
                                Section = Lines[i].Substring(1, Lines[i].Length - 2).Trim().ToLowerInvariant();
                            }
                            else
                            {
                                int j = Lines[i].IndexOf('=');
                                if (j >= 0)
                                {
                                    string a = Lines[i].Substring(0, j).TrimEnd().ToLowerInvariant();
                                    string b = Lines[i].Substring(j + 1).TrimStart().Unescape();
                                    switch (Section)
                                    {
                                    case "handles":
                                        switch (a)
                                        {
                                        case "forward":
                                            myQuickReferences.HandleForward = b;
                                            break;

                                        case "neutral":
                                            myQuickReferences.HandleNeutral = b;
                                            break;

                                        case "backward":
                                            myQuickReferences.HandleBackward = b;
                                            break;

                                        case "power":
                                            myQuickReferences.HandlePower = b;
                                            break;

                                        case "powernull":
                                            myQuickReferences.HandlePowerNull = b;
                                            break;

                                        case "brake":
                                            myQuickReferences.HandleBrake = b;
                                            break;

                                        case "locobrake":
                                            myQuickReferences.HandleLocoBrake = b;
                                            break;

                                        case "brakenull":
                                            myQuickReferences.HandleBrakeNull = b;
                                            break;

                                        case "release":
                                            myQuickReferences.HandleRelease = b;
                                            break;

                                        case "lap":
                                            myQuickReferences.HandleLap = b;
                                            break;

                                        case "service":
                                            myQuickReferences.HandleService = b;
                                            break;

                                        case "emergency":
                                            myQuickReferences.HandleEmergency = b;
                                            break;

                                        case "holdbrake":
                                            myQuickReferences.HandleHoldBrake = b;
                                            break;
                                        }
                                        break;

                                    case "doors":
                                        switch (a)
                                        {
                                        case "left":
                                            myQuickReferences.DoorsLeft = b;
                                            break;

                                        case "right":
                                            myQuickReferences.DoorsRight = b;
                                            break;
                                        }
                                        break;

                                    case "misc":
                                        switch (a)
                                        {
                                        case "score":
                                            myQuickReferences.Score = b;
                                            break;
                                        }
                                        break;

                                    case "commands":
                                        for (int k = 0; k < myCommandInfos.Length; k++)
                                        {
                                            if (string.Compare(myCommandInfos[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                myCommandInfos[k].Description = b;
                                                break;
                                            }
                                        }
                                        break;

                                    case "keys":
                                        for (int k = 0; k < KeyInfos.Length; k++)
                                        {
                                            if (string.Compare(KeyInfos[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                KeyInfos[k].Description = b;
                                                break;
                                            }
                                        }
                                        break;

                                    case "fallback":
                                        switch (a)
                                        {
                                        case "language":
                                            FallbackCodes.Add(b);
                                            break;
                                        }
                                        break;

                                    case "language":
                                        switch (a)
                                        {
                                        case "name":
                                            Name = b;
                                            break;

                                        case "flag":
                                            Flag = b;
                                            break;
                                        }
                                        break;

                                    default:
                                        if (LoadedStringCount >= InterfaceStrings.Length)
                                        {
                                            Array.Resize <InterfaceString>(ref InterfaceStrings,
                                                                           InterfaceStrings.Length << 1);
                                        }
                                        InterfaceStrings[LoadedStringCount].Name = Section + "_" + a;
                                        InterfaceStrings[LoadedStringCount].Text = b;
                                        LoadedStringCount++;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    Array.Resize(ref InterfaceStrings, LoadedStringCount);
                }
                catch
                {
                }
            }
Example #5
0
            /// <summary>Creates a new language from a file stream</summary>
            /// <param name="languageReader">The file stream</param>
            /// <param name="languageCode">The language code</param>
            internal Language(TextReader languageReader, string languageCode)
            {
                Name          = "Unknown";
                LanguageCode  = languageCode;
                FallbackCodes = new List <string> {
                    "en-US"
                };
                myCommandInfos    = new CommandInfo[CommandInfos.Length];
                KeyInfos          = new KeyInfo[TranslatedKeys.Length];
                myQuickReferences = new InterfaceQuickReference();
                Array.Copy(CommandInfos, myCommandInfos, myCommandInfos.Length);
                Array.Copy(TranslatedKeys, KeyInfos, TranslatedKeys.Length);

                string    prefix = string.Empty;
                XliffFile file   = new XliffFile(languageReader, languageCode);
                List <InterfaceString> strings = new List <InterfaceString>();

                ExportUnits(prefix, file.Units, strings);

                foreach (XliffFile.Group group in file.Groups)
                {
                    ExportGroup(prefix, group, strings);
                }

                InterfaceString[] groupLanguage = strings.Where(s => s.Name.StartsWith("language_")).ToArray();

                foreach (var interfaceString in groupLanguage)
                {
                    string key = interfaceString.Name.Split('_')[1];

                    switch (key)
                    {
                    case "name":
                        Name = interfaceString.Text;
                        strings.Remove(interfaceString);
                        break;

                    case "flag":
                        Flag = interfaceString.Text;
                        strings.Remove(interfaceString);
                        break;
                    }
                }

                InterfaceString[] groupOpenBve = strings.Where(s => s.Name.StartsWith("openbve_")).ToArray();

                foreach (var interfaceString in groupOpenBve)
                {
                    string section = interfaceString.Name.Split('_')[1];
                    string key     = string.Join("_", interfaceString.Name.Split('_').Skip(2));

                    switch (section)
                    {
                    case "handles":
                        switch (key)
                        {
                        case "forward":
                            myQuickReferences.HandleForward = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "neutral":
                            myQuickReferences.HandleNeutral = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "backward":
                            myQuickReferences.HandleBackward = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "power":
                            myQuickReferences.HandlePower = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "powernull":
                            myQuickReferences.HandlePowerNull = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "brake":
                            myQuickReferences.HandleBrake = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "locobrake":
                            myQuickReferences.HandleLocoBrake = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "brakenull":
                            myQuickReferences.HandleBrakeNull = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "release":
                            myQuickReferences.HandleRelease = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "lap":
                            myQuickReferences.HandleLap = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "service":
                            myQuickReferences.HandleService = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "emergency":
                            myQuickReferences.HandleEmergency = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "holdbrake":
                            myQuickReferences.HandleHoldBrake = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;
                        }
                        break;

                    case "doors":
                        switch (key)
                        {
                        case "left":
                            myQuickReferences.DoorsLeft = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;

                        case "right":
                            myQuickReferences.DoorsRight = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;
                        }
                        break;

                    case "misc":
                        switch (key)
                        {
                        case "score":
                            myQuickReferences.Score = interfaceString.Text;
                            strings.Remove(interfaceString);
                            break;
                        }
                        break;

                    case "commands":
                        for (int k = 0; k < myCommandInfos.Length; k++)
                        {
                            if (string.Compare(myCommandInfos[k].Name, key, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                myCommandInfos[k].Description = interfaceString.Text;
                                strings.Remove(interfaceString);
                                break;
                            }
                        }
                        break;

                    case "keys":
                        for (int k = 0; k < KeyInfos.Length; k++)
                        {
                            if (string.Compare(KeyInfos[k].Name, key, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                KeyInfos[k].Description = interfaceString.Text;
                                strings.Remove(interfaceString);
                                break;
                            }
                        }
                        break;

                    case "fallback":
                        switch (key)
                        {
                        case "language":
                            FallbackCodes.Add(interfaceString.Text);
                            strings.Remove(interfaceString);
                            break;
                        }
                        break;
                    }
                }

                InterfaceStrings = strings.ToArray();

                for (int i = 0; i < InterfaceStrings.Length; i++)
                {
                    if (InterfaceStrings[i].Name.StartsWith("openbve_"))
                    {
                        InterfaceStrings[i].Name = InterfaceStrings[i].Name.Replace("openbve_", string.Empty);
                    }
                }
            }
Example #6
0
        /// <summary>Adds a language file to the available langauge list</summary>
        /// <param name="File">The absolute on-disk path to the language file we wish to load</param>
	    internal static void AddLanguage(string File)
	    {
            //Create new language
            Language newLanguage = new Language
            {
                Name = "Unknown",
                LanguageCode = System.IO.Path.GetFileNameWithoutExtension(File),
                FallbackCodes = new List<string>()
            };
            try
            {
                string[] Lines = System.IO.File.ReadAllLines(File, new System.Text.UTF8Encoding());
                string Section = "";
                InterfaceString[] LoadedStrings = new InterfaceString[16];
                CommandInfo[] LoadedCommands = new CommandInfo[Interface.CommandInfos.Length];
				KeyInfo[] LoadedKeys = new KeyInfo[Interface.TranslatedKeys.Length];
				InterfaceQuickReference QuickReference = new InterfaceQuickReference();
                Array.Copy(Interface.CommandInfos, LoadedCommands, Interface.CommandInfos.Length);
				Array.Copy(Interface.TranslatedKeys, LoadedKeys, Interface.TranslatedKeys.Length);
                var LoadedStringCount = 0;
                for (int i = 0; i < Lines.Length; i++)
                {
                    Lines[i] = Lines[i].Trim();
                    if (!Lines[i].StartsWith(";"))
                    {
                        if (Lines[i].StartsWith("[", StringComparison.Ordinal) & Lines[i].EndsWith("]", StringComparison.Ordinal))
                        {
                            Section = Lines[i].Substring(1, Lines[i].Length - 2).Trim().ToLowerInvariant();
                        }
                        else
                        {
                            int j = Lines[i].IndexOf('=');
                            if (j >= 0)
                            {
                                string a = Lines[i].Substring(0, j).TrimEnd().ToLowerInvariant();
                                string b = Interface.Unescape(Lines[i].Substring(j + 1).TrimStart());
                                switch (Section)
                                {
                                    case "handles":
                                        switch (a)
                                        {
                                            case "forward": QuickReference.HandleForward = b; break;
                                            case "neutral": QuickReference.HandleNeutral = b; break;
                                            case "backward": QuickReference.HandleBackward = b; break;
                                            case "power": QuickReference.HandlePower = b; break;
                                            case "powernull": QuickReference.HandlePowerNull = b; break;
                                            case "brake": QuickReference.HandleBrake = b; break;
                                            case "brakenull": QuickReference.HandleBrakeNull = b; break;
                                            case "release": QuickReference.HandleRelease = b; break;
                                            case "lap": QuickReference.HandleLap = b; break;
                                            case "service": QuickReference.HandleService = b; break;
                                            case "emergency": QuickReference.HandleEmergency = b; break;
                                            case "holdbrake": QuickReference.HandleHoldBrake = b; break;
                                        } break;
                                    case "doors":
                                        switch (a)
                                        {
                                            case "left": QuickReference.DoorsLeft = b; break;
                                            case "right": QuickReference.DoorsRight = b; break;
                                        } break;
                                    case "misc":
                                        switch (a)
                                        {
                                            case "score": QuickReference.Score = b; break;
                                        } break;
                                    case "commands":
                                        {
                                            for (int k = 0; k < LoadedCommands.Length; k++)
                                            {
                                                if (string.Compare(LoadedCommands[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                                {
                                                    LoadedCommands[k].Description = b;
                                                    break;
                                                }
                                            }
                                        } break;
                                    case "keys":
                                        {
                                            for (int k = 0; k < LoadedKeys.Length; k++)
                                            {
                                                if (string.Compare(LoadedKeys[k].Name, a, StringComparison.OrdinalIgnoreCase) == 0)
                                                {
                                                    LoadedKeys[k].Description = b;
                                                    break;
                                                }
                                            }
                                        } break;
                                    case "fallback":
                                        switch (a)
                                        {
                                            case "language": newLanguage.FallbackCodes.Add(b); break;
                                        } break;
                                    case "language":
                                        switch (a)
                                        {
                                            case "name": newLanguage.Name = b; break;
                                            case "flag": newLanguage.Flag = b; break;
                                        } break;

                                    default:
                                        if (LoadedStringCount >= LoadedStrings.Length)
                                        {
                                            Array.Resize<InterfaceString>(ref LoadedStrings,
                                                LoadedStrings.Length << 1);
                                        }
                                        LoadedStrings[LoadedStringCount].Name = Section + "_" + a;
                                        LoadedStrings[LoadedStringCount].Text = b;
                                        LoadedStringCount++;
                                        break;
                                }
                            }
                        }
                    }
                }
                newLanguage.InterfaceStrings = LoadedStrings;
                newLanguage.CommandInfos = LoadedCommands;
	            newLanguage.KeyInfos = LoadedKeys;
                newLanguage.InterfaceStringCount = LoadedStringCount;
                newLanguage.QuickReferences = QuickReference;
                //We should always fall-back to en-US as the last-resort before failing to load a string
                newLanguage.FallbackCodes.Add("en-US");
                AvailableLangauges.Add(newLanguage);
            }
            catch (Exception)
            {
                //This message is shown when loading a language fails, and must not be translated, as otherwise it could produce a blank error message
                MessageBox.Show("An error occurred whilst attempting to load the language file: \n \n" + File);
                Environment.Exit(0);
            }
	    }