示例#1
0
        private string OutputResult(LangFile file, List <LanguageError> fileErrors)
        {
            var result = $"{file.FileName}.xml - ({(file.Base ?? "") + " " + (file.Variant ?? "")})".ToBold(Parsemode) + "\n";

            if (fileErrors.Any(x => x.Level == ErrorLevel.Error))
            {
                result += "Errors:".ToItalic(Parsemode) + "\n";
                result  = fileErrors.Where(x => x.Level == ErrorLevel.Error).Aggregate(result, (current, fileError) => current + $"{fileError.Key}\n{fileError.Message}\n\n");
            }
            if (fileErrors.Any(x => x.Level == ErrorLevel.MissingString))
            {
                result += "Missing Values:".ToItalic(Parsemode) + "\n";
                result  = fileErrors.Where(x => x.Level == ErrorLevel.MissingString).Aggregate(result, (current, fileError) => current + $"{fileError.Key}\n");
            }
            if (fileErrors.Any(x => x.Level == ErrorLevel.FatalError))
            {
                result += "\nFatal errors:".ToBold(Parsemode) + "\n";
                result  = fileErrors.Where(x => x.Level == ErrorLevel.FatalError).Aggregate(result, (current, fileError) => current + $"{fileError.Key}\n{fileError.Message}\n\n");
            }
            if (fileErrors.Count == 0)
            {
                result += "No errors".ToItalic(Parsemode) + "\n";
            }

            result += $"\n" + "Base:".ToItalic(Parsemode) + $" {file.Base ?? ""}";
            result += $"\n" + "Variant:".ToItalic(Parsemode) + $" {file.Variant ?? ""}";

            return(result);
        }
示例#2
0
        /// <summary>
        /// Get a language information.
        /// </summary>
        /// <param name="fileName">The file name (without extension!) of the file you want to get</param>
        /// <returns>A language object containing informations about the language</returns>
        public Models.Language GetLanguage(string fileName)
        {
            var lang     = Languages.First(x => x.Key == fileName);
            var langfile = new LangFile(lang.Key, lang.Value, TempPath);

            return(new Models.Language(langfile.Base, langfile.Variant, langfile.FileName, langfile.LangCode));
        }
示例#3
0
        private void GetFileErrors(LangFile file, List <LanguageError> fileErrors, XDocument master)
        {
            var masterStrings = master.Descendants("string");

            foreach (var str in masterStrings)
            {
                var key = str.Attribute("key").Value;
                int.TryParse(str.Attributes().FirstOrDefault(x => x.Name == "maxlength")?.Value ?? "0", out int maxlength);
                //get the english string
                //get the locale values
                var masterString = GetString(key, master);
                var values       = file.Doc.Descendants("string")
                                   .FirstOrDefault(x => x.Attribute("key").Value == key)?
                                   .Descendants("value");
                if (values == null)
                {
                    fileErrors.Add(new LanguageError(file.FileName, key, $"Values missing"));
                    continue;
                }
                //check master string for {#} values
                int vars = 0;
                for (int i = 0; i < 10; i++)
                {
                    if (masterString.Contains("{" + i + "}"))
                    {
                        vars = i + 1;
                    }
                }

                foreach (var value in values)
                {
                    for (int i = 0; i <= 10 - 1; i++)
                    {
                        if (!value.Value.Contains("{" + i + "}") && vars - 1 >= i)
                        {
                            //missing a value....
                            fileErrors.Add(new LanguageError(file.FileName, key, "Missing {" + i + "}", ErrorLevel.Error));
                        }
                        else if (value.Value.Contains("{" + i + "}") && vars - 1 < i)
                        {
                            fileErrors.Add(new LanguageError(file.FileName, key, "Extra {" + i + "}", ErrorLevel.Error));
                        }
                    }

                    if (maxlength != 0 && value.Value.Length > maxlength)
                    {
                        fileErrors.Add(new LanguageError(file.FileName, key, $"String is longer than {maxlength} character(s)", ErrorLevel.FatalError));
                    }
                }
            }

            foreach (var key in file.Doc.Descendants("string").Select(x => x.Attribute("key").Value))
            {
                if (masterStrings.FirstOrDefault(x => x.Attribute("key").Value == key) == null)
                {
                    fileErrors.Add(new LanguageError(file.FileName, key, "Extra Strings", ErrorLevel.ExtraString));
                }
            }
        }
示例#4
0
        private void TestLength(LangFile file, List <LanguageError> fileErrors)
        {
            var test  = $"testing|-1001234567890|{file.Base ?? ""}|{file.Variant ?? ""}|t";
            var count = Encoding.UTF8.GetByteCount(test);

            if (count > 64)
            {
                fileErrors.Add(new LanguageError(file.FileName, "Language Node".ToBold(Parsemode), "Base and variant are too long.", ErrorLevel.FatalError));
            }
        }
示例#5
0
 private void CheckLanguageNode(LangFile langfile, List <LanguageError> errors)
 {
     if (String.IsNullOrWhiteSpace(langfile.Base))
     {
         errors.Add(new LanguageError(langfile.FileName, "Language Node".ToBold(Parsemode), "Base is missing", ErrorLevel.FatalError));
     }
     if (String.IsNullOrWhiteSpace(langfile.Variant))
     {
         errors.Add(new LanguageError(langfile.FileName, "Language Node".ToBold(Parsemode), "Variant is missing", ErrorLevel.FatalError));
     }
     if (String.IsNullOrWhiteSpace(langfile.LangCode))
     {
         errors.Add(new LanguageError(langfile.FileName, "Language Node".ToBold(Parsemode), "LangCode is missing", ErrorLevel.Error));
     }
 }
示例#6
0
        private string OutputResult(LangFile newFile, List <LanguageError> newFileErrors, LangFile curFile, List <LanguageError> curFileErrors)
        {
            var result = "NEW FILE\n" + $"{newFile.FileName}.xml - ({(newFile.Base ?? "") + " " + (newFile.Variant ?? "")})".ToBold(Parsemode) + "\n";

            if (newFileErrors.Any(x => x.Level == ErrorLevel.Error))
            {
                result += "Errors:".ToItalic(Parsemode) + "\n";
                result  = newFileErrors.Where(x => x.Level == ErrorLevel.Error).Aggregate(result, (current, fileError) => current + $"{fileError.Key}\n{fileError.Message}\n\n");
            }
            if (newFileErrors.Any(x => x.Level == ErrorLevel.MissingString))
            {
                result += "Missing Values:".ToItalic(Parsemode) + "\n";
                result  = newFileErrors.Where(x => x.Level == ErrorLevel.MissingString).Aggregate(result, (current, fileError) => current + $"{fileError.Key}\n");
            }
            if (newFileErrors.Any(x => x.Level == ErrorLevel.FatalError))
            {
                result += "\nFatal errors:".ToBold(Parsemode) + "\n";
                result  = newFileErrors.Where(x => x.Level == ErrorLevel.FatalError).Aggregate(result, (current, fileError) => current + $"{fileError.Key}\n{fileError.Message}\n\n");
            }
            if (newFileErrors.Count == 0)
            {
                result += "No errors".ToItalic(Parsemode) + "\n";
            }

            if (curFile != null)
            {
                result += "\n\n";
                result += "OLD FILE".ToBold(Parsemode) + $" (Last updated: {curFile.LatestUpdate.ToString("MMM dd")})\n{curFile.FileName}.xml - ({curFile.Base} {curFile.Variant})\n";
                result +=
                    $"Errors: {curFileErrors.Count(x => x.Level == ErrorLevel.Error)}\nMissing strings: {curFileErrors.Count(x => x.Level == ErrorLevel.MissingString)}";
            }
            else
            {
                result += "\n\n" + "No old file, this is a new language".ToBold(Parsemode);
                result += "\nPlease double check the filename, and the language base and variant, as you won't be able to change them.";
                result += $"\n" + "Base:".ToItalic(Parsemode) + $" {newFile.Base ?? ""}";
                if (!Directory.GetFiles(FilesPath, "*.xml").Select(x => new LangFile(x)).Any(x => x.Base == newFile.Base))
                {
                    result += " " + "(NEW)".ToBold(Parsemode);
                }
                result += $"\n" + "Variant:".ToItalic(Parsemode) + $" {newFile.Variant ?? ""}";
            }

            return(result);
        }
示例#7
0
        /// <summary>
        /// Validate an already existing language file.
        /// </summary>
        /// <param name="language">The language file to be validated.</param>
        /// <returns>Returns the validation results in the given parse mode.</returns>
        public string ValidateLanguage(string language)
        {
            var filePath = Path.Combine(FilesPath, language + ".xml");

            try
            {
                XDocument.Load(filePath);
            }
            catch (System.Xml.XmlException xe)
            {
                return($"Can't parse file {language}.xml:".ToBold(Parsemode) + "\n" + xe.Message);
            }

            var fileErrors = new List <LanguageError>();

            ReloadLanguages();
            var langs  = Languages.Select(x => new LangFile(x.Key, x.Value, TempPath));
            var master = Master;
            var file   = new LangFile(filePath);

            CheckLanguageNode(file, fileErrors);
            TestLength(file, fileErrors);

            var error = langs.FirstOrDefault(x =>
                                             (x.FileName == file.FileName && (x.Base != file.Base || x.Variant != file.Variant)) || //check for same filename and mismatching base-variant
                                             (x.Base == file.Base && x.Variant == file.Variant && x.FileName != file.FileName) //check for same base-variant and mismatching filename
                                             );

            if (error != null)
            {
                //problem....
                fileErrors.Add(new LanguageError(file.FileName, "Language Node".ToBold(Parsemode),
                                                 $"ERROR: The following file partially matches the same language node. Please check the file name, and the language base and variant. Aborting.\n\n" + $"{error.FileName}.xml".ToBold(Parsemode) + $"\n" + "Base:".ToItalic(Parsemode) + $" {error.Base}\n" + "Variant:".ToItalic(Parsemode) + $" {error.Variant}", ErrorLevel.FatalError));
            }

            GetFileErrors(file, fileErrors, master);
            return(OutputResult(file, fileErrors));
        }
示例#8
0
        /// <summary>
        /// Downloads the file to the temporary language folder and validates it against the current master file.
        /// </summary>
        /// <param name="FileId">The file_id of the file that should be uploaded.</param>
        /// <param name="FileName">The file_name of the file that should be uploaded.</param>
        /// <param name="CanUpload">Whether the file can be uploaded like this or not. If strictErrors is enabled, it's only true if there are no errors, else it's true if there are no fatal errors.</param>
        /// <returns>Returns a string with the validation results, the given parse mode will be applied to it.</returns>
        public string PrepareUploadLanguage(string FileId, string FileName, out bool CanUpload)
        {
            CanUpload = false;
            if (FileId == null)
            {
                throw new ArgumentNullException("FileId");
            }
            if (FileName == null)
            {
                throw new ArgumentNullException("FileName");
            }

            var newFilePath = Path.Combine(TempPath, FileName);

            try
            {
                Api.DownloadFile(FileId, newFilePath);
            }
            catch (Exception e)
            {
                string message = "Exception occured while trying to download the file:".ToBold(Parsemode) + "\n\n";

                do
                {
                    message += e.Message;
                    e        = e.InnerException;
                }while (e != null);
                return(message);
            }

            try
            {
                XDocument.Load(newFilePath);
            }
            catch (System.Xml.XmlException xe)
            {
                return($"Can't parse file {FileName}:".ToBold(Parsemode) + "\n" + xe.Message);
            }

            //ok, we have the file.  Now we need to determine the language, scan it and the original file.
            var newFileErrors = new List <LanguageError>();

            //first, reload existing files to program
            ReloadLanguages();

            var langs   = Languages.Select(x => new LangFile(x.Key, x.Value, TempPath));
            var master  = Master;
            var newFile = new LangFile(newFilePath);

            //make sure it has a complete langnode
            CheckLanguageNode(newFile, newFileErrors);

            //test the length
            TestLength(newFile, newFileErrors);

            //check uniqueness
            var error = langs.FirstOrDefault(x =>
                                             (x.FileName == newFile.FileName && (x.Base != newFile.Base || x.Variant != newFile.Variant)) || //check for same filename and mismatching base-variant
                                             (x.Base == newFile.Base && x.Variant == newFile.Variant && x.FileName != newFile.FileName) //check for same base-variant and mismatching filename
                                             );

            if (error != null)
            {
                //problem....
                newFileErrors.Add(new LanguageError(newFile.FileName, "Language Node".ToBold(Parsemode),
                                                    $"ERROR: The following file partially matches the same language node. Please check the file name, and the language base and variant. Aborting.\n\n" + $"{error.FileName}.xml".ToBold(Parsemode) + $"\n" + "Base:".ToItalic(Parsemode) + $" {error.Base}\n" + "Variant:".ToItalic(Parsemode) + $" {error.Variant}", ErrorLevel.FatalError));
            }

            //get the errors in it
            GetFileErrors(newFile, newFileErrors, master);

            //need to get the current file
            var curFile       = langs.FirstOrDefault(x => x.FileName == newFile.FileName);
            var curFileErrors = new List <LanguageError>();

            if (curFile != null)
            {
                //test the length
                TestLength(curFile, curFileErrors);

                //get the errors in it
                GetFileErrors(curFile, curFileErrors, master);
            }

            if (StrictErrors)
            {
                CanUpload = newFileErrors.Count == 0;               //For strict error handling, the file may only be uploaded if there are no errors
            }
            else
            {
                CanUpload = newFileErrors.All(x => x.Level != ErrorLevel.FatalError);  //For normal handling, the file can always be uploaded if no fatal errors are present
            }
            //return the validation result
            return(OutputResult(newFile, newFileErrors, curFile, curFileErrors));
        }