Forex_Strategy_Builder.Language.GenerateNewLangFile C# (CSharp) Method

GenerateNewLangFile() public static method

Generates a new language file.
public static GenerateNewLangFile ( string sFileName, string sLang, string sAuthor, string sWebsite, string sEmail ) : bool
sFileName string
sLang string
sAuthor string
sWebsite string
sEmail string
return bool
        public static bool GenerateNewLangFile(string sFileName, string sLang, string sAuthor, string sWebsite, string sEmail)
        {
            string sContent    = Properties.Resources.Bulgarian;
            string patternMain = @"<main>(?<main>.*)</main>";
            string patternAlt  = @"<alt>(?<alt>.*)</alt>";
            Regex  expression  = new Regex(".*" + patternMain + patternAlt + ".*", RegexOptions.Compiled);
            string sFilePath   = Path.Combine(Data.LanguageDir, sFileName);
            StringBuilder sb   = new StringBuilder();
            foreach (string sLine in sContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
            {
                Match match = expression.Match(sLine);
                if (match.Success)
                {
                    string main = match.Groups["main"].Value;
                    string alt  = match.Groups["alt"].Value;
                    sb.AppendLine(sLine.Replace(alt, main));
                }
                else
                    sb.AppendLine(sLine);
            }
            sContent = sb.ToString();
            sContent = sContent.Replace("Български",           sLang);
            sContent = sContent.Replace("Forex Software Ltd.", sAuthor);
            sContent = sContent.Replace(@"http://forexsb.com", sWebsite);
            sContent = sContent.Replace(@"[email protected]",   sEmail);

            return SaveTextFile(sFilePath, sContent);
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Button click
        /// </summary>
        void Btn_Click(object sender, EventArgs e)
        {
            Button btn  = (Button)sender;
            string name = btn.Name;

            if (name == "Accept")
            {
                bool isCorrect = true;

                string language = atbxInputValues[0].Text;
                string fileName = atbxInputValues[1].Text + ".xml";
                string author   = atbxInputValues[2].Text;
                string website  = atbxInputValues[3].Text;
                string contacts = atbxInputValues[4].Text;

                // Language
                if (language.Length < 2)
                {
                    MessageBox.Show("The language name must be at least two characters in length!", "Language", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    isCorrect = false;
                }

                foreach (string lang in Language.LanguageList)
                {
                    if (language == lang)
                    {
                        MessageBox.Show("A translation in this language exists already!" + Environment.NewLine + "Change the language name.", "Language", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        isCorrect = false;
                    }
                }

                // Language file name
                if (fileName.Length < 2)
                {
                    MessageBox.Show("The language file name must be at least two characters in length!", "Language File Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    isCorrect = false;
                }

                if (Directory.Exists(Data.LanguageDir))
                {
                    string[] asFileNames = Directory.GetFiles(Data.LanguageDir);
                    foreach (string path in asFileNames)
                    {
                        if (fileName == Path.GetFileName(path))
                        {
                            MessageBox.Show("This file name exists already!" + Environment.NewLine + "Change the file name.", "Language File Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            isCorrect = false;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Could not find the language files directory!", "Language Files Directory", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    isCorrect = false;
                }

                if (isCorrect)
                {
                    if (Language.GenerateNewLangFile(fileName, language, author, website, contacts))
                    {
                        Configs.Language = language;
                        string sMassage = "The new language file was successfully created." + Environment.NewLine + "Restart the program and edit the translation.";
                        MessageBox.Show(sMassage, "New Translation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    return;
                }
            }

            this.Close();
        }