Exemplo n.º 1
0
        public ExtractorCsharp(Options options)
        {
            this.Options = options;
            this.Catalog = new Catalog();
            if (!Options.Overwrite && File.Exists(Options.OutFile))
            {
                Catalog.Load(Options.OutFile);
                foreach (CatalogEntry entry in Catalog)
                    entry.ClearReferences();
            }
            else
            {
                Catalog.Project = "PACKAGE VERSION";
            }

            this.Options.OutFile = Path.GetFullPath(this.Options.OutFile);
        }
Exemplo n.º 2
0
        public void ExtractorCSharpTest()
        {
            string ressourceId = String.Format("{0}.{1}", this.GetType().Assembly.GetName().Name, "Data.XgettextTest.txt");
            string text = "";
            using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(ressourceId))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    text = reader.ReadToEnd();
                }
            }

            Options options = new Options();
            options.InputFiles.Add(@"./Test/File/Name.cs"); // File wont be used, feed the plain text
            options.OutFile = @"./Test.pot";
            options.Overwrite = true;
            ExtractorCsharp extractor = new ExtractorCsharp(options);
            extractor.GetMessages(text, options.InputFiles[0]);
            extractor.Save();

            int ctx = 0;
            int multiline = 0;
            foreach(CatalogEntry entry in extractor.Catalog)
            {
                if (entry.HasContext)
                    ctx++;
                if (entry.String == "multiline-string-1-string-2" ||
                    entry.String == "Multiline Hint for label1")
                    multiline++;
            }

            Assert.AreEqual(2, ctx, "Context count");

            Assert.AreEqual(2, extractor.Catalog.PluralFormsCount, "PluralFormsCount");
            Assert.AreEqual(17, extractor.Catalog.Count, "Duplicates may not detected");
            Assert.AreEqual(2, multiline, "Multiline string");
        }
Exemplo n.º 3
0
        public static bool GetOptions(string[] args, String sopts, LongOpt[] lopts, Options options, out StringBuilder message)
        {
            message = new StringBuilder();
            Getopt.Getopt getopt = new Getopt.Getopt(
                Assembly.GetExecutingAssembly().GetName().Name,
                args, sopts, lopts)
                {
                    Opterr = false
                };

            options.Verbose = false;
            options.ShowUsage = false;
            options.Recursive = false;
            options.Overwrite = true;

            int option;
            while ((option = getopt.getopt()) != -1)
            {
                switch (option)
                {
                case 1:
                    options.InputFiles.Add(getopt.Optarg);
                    break;
                case 2:
                    options.Recursive = true;
                    break;
                case 3:
                    options.SearchPatterns.Add(getopt.Optarg);
                    break;
                case 4:
                    options.SetEncoding(getopt.Optarg);
                    break;
                case 5:
                    options.DetectEncoding = true;
                    break;
                case ':':
                    message.AppendFormat("Option '{0}' requires an argument", getopt.OptoptStr);
                    return false;
                case '?':
                    message.AppendFormat("Invalid option '{0}'", getopt.OptoptStr);
                    return false;
                case 'f':
                    string fileList = getopt.Optarg;
                    Utils.FileUtils.ReadStrings(fileList, options.InputFiles);
                    break;
                case 'j':
                    options.Overwrite = false;
                    break;
                case 'o':
                    options.OutFile = getopt.Optarg;
                    break;
                case 'D':
                    options.InputDirs.Add(getopt.Optarg);
                    break;
                case 'v':
                    options.Verbose = true;
                    break;
                case 'h':
                    options.ShowUsage = true;
                    return true;
                default:
                    PrintUsage();
                    return false;
                }
            }

            if (getopt.Opterr)
            {
                message.AppendLine();
                message.Append("Error in command line options. Use -h to read options usage");
                return false;
            }
            return true;
        }
Exemplo n.º 4
0
        public static int Main(string[] args)
        {
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            StringBuilder message;

            Options options = new Options();
            if (args.Length == 0)
            {
                PrintUsage();
                return 1;
            }
            else if (!GetOptions(args, SOpts, LOpts, options, out message))
            {
                Console.WriteLine(message.ToString());
                return 1;
            }
            else if (options.ShowUsage)
            {
                PrintUsage();
                return 0;
            }
            if (!AnalyseOptions(options, out message))
            {
                Console.WriteLine(message.ToString());
                return 1;
            }

            try
            {
                ExtractorCsharp extractor = new ExtractorCsharp(options);
                extractor.GetMessages();
                extractor.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error during execution: {0}", ex.Message);
                return 1;
            }

            Console.WriteLine("Template file '{0}' generated", options.OutFile);
            return 0;
        }
Exemplo n.º 5
0
        public static bool AnalyseOptions(Options options, out StringBuilder message)
        {
            message = new StringBuilder();
            try
            {
                if (options.InputFiles.Count == 0)
                    options.InputFiles.Add("*.cs");
                if (options.InputDirs.Count == 0)
                    options.InputDirs.Add(Environment.CurrentDirectory);

                foreach(string dir in options.InputDirs)
                {
                    if (!Directory.Exists(dir))
                    {
                        message.AppendFormat("Input directory '{0}' not found", dir);
                        return false;
                    }
                }
            }
            catch(Exception e)
            {
                message.Append(e.Message);
                return false;
            }

            return true;
        }