예제 #1
0
            public void Analyze(TranslationFile file, ErrorCollector errorCollector)
            {
                var key = (file.Namespace.ToLower(), file.Locale.ToLower());

                if (localeNamespaces.Contains(key))
                {
                    errorCollector.AddError($"Another translation file for the namespace \"{file.Namespace}\" " +
                                            $"for locale \"{file.Locale}\" already encountered");
                }
                else
                {
                    localeNamespaces.Add(key);
                }
            }
 public void Analyze(TranslationFile file, ErrorCollector errorCollector)
 {
     using (var reader = new StreamReader(file.Path))
     {
         while (!reader.EndOfStream)
         {
             var line = reader.ReadLine();
             if (!string.IsNullOrWhiteSpace(line))
             {
                 if (!correctLineRegex.IsMatch(line))
                 {
                     errorCollector.AddError(
                         "Incorrect file format: single Tab character must be used to indent the keys");
                     return;
                 }
             }
         }
     }
 }
        public void Analyze(TranslationFile file, ErrorCollector errorCollector)
        {
            try
            {
                using (var streamReader = new StreamReader(file.Path))
                    using (var jsonReader = new JsonTextReader(streamReader))
                    {
                        var translationData = JObject.Load(jsonReader).ToObject <Dictionary <string, string> >();

                        foreach (var(key, translation) in translationData !)
                        {
                            var match = LocalizationKeyRegex.Match(key);
                            if (!match.Success)
                            {
                                errorCollector.AddError($"Key \"{key}\" is invalid. Valid key format is Namespace:Key");
                            }
                            else
                            {
                                var keyNamespace = match.Groups[1].Value;
                                if (!string.Equals(keyNamespace, file.Namespace, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    errorCollector.AddError($"Namespace in key \"{key}\" doesn't match the file namespace");
                                }
                            }

                            if (!quokkaAnalyzer.Analyze(translation))
                            {
                                errorCollector.AddError($"Translation for key \"{key}\" is not a valid Quokka template: \"{translation}\"");
                            }
                        }
                    }

                formatAnalyzer.Analyze(file, errorCollector);
            }
            catch (Exception ex)
            {
                errorCollector.AddError(ex.Message);
            }
        }
        static int Main(string[] args)
        {
            var errorsOccured    = false;
            var launchParameters = LaunchParameters.FromCommandLineArguments(args);

            if (launchParameters == null)
            {
                return(-1);
            }

            var baseDirectory = launchParameters.BaseDirectory;

            Console.WriteLine($"Working directory: {baseDirectory}");

            var analyzers = new List <IAnalyzer>
            {
                new FileContentAnalyzer()
            };

            if (!launchParameters.SkipProjectInclusionCheck)
            {
                analyzers.Add(new ProjectInclusionAnalyzer(baseDirectory));
            }

            analyzers.Add(new NamespaceUniquenessAnalyzer());

            Console.WriteLine("Searching for translation files...");

            var i18nFiles = LocateInternationalizationFiles(baseDirectory);

            foreach (var translationFilePath in i18nFiles)
            {
                var errorCollector = new ErrorCollector();

                var translationFile = TranslationFile.TryCreateFromFilePath(translationFilePath);
                if (translationFile == null)
                {
                    errorCollector.AddError("Could not retrieve locale and namespace from file path.");
                }
                else
                {
                    foreach (var analyzer in analyzers)
                    {
                        analyzer.Analyze(translationFile, errorCollector);
                    }
                }

                if (errorCollector.Errors.Any())
                {
                    errorsOccured = true;
                    LogError($"{Path.GetRelativePath(baseDirectory, translationFilePath)}:");
                    foreach (var error in errorCollector.Errors)
                    {
                        LogError(error);
                    }
                }
            }

            if (errorsOccured)
            {
                Console.WriteLine("Error: there are problems with some translation files.");
                Console.WriteLine("Inspect the log messages above.");
                return(-1);
            }

            Console.WriteLine("Success: no problems found with translation files.");
            return(0);
        }