public void TestExecute() { var collection = new LocalizableStringCollection { "neutralValue1", { "en-US", "americaValue1" } }; var neutralCommand = new SetLocalizableString(collection, new LocalizableString { Value = "neutralValue2" }); var americanCommand = new SetLocalizableString(collection, new LocalizableString { Language = new CultureInfo("en-US"), Value = "americaValue2" }); neutralCommand.Execute(); collection.GetExactLanguage(LocalizableString.DefaultLanguage) .Should().Be("neutralValue2", because: "Unspecified language should default to English generic"); neutralCommand.Undo(); collection.GetExactLanguage(LocalizableString.DefaultLanguage) .Should().Be("neutralValue1", because: "Unspecified language should default to English generic"); americanCommand.Execute(); collection.GetExactLanguage(new CultureInfo("en-US")) .Should().Be("americaValue2"); americanCommand.Undo(); collection.GetExactLanguage(new CultureInfo("en-US")) .Should().Be("americaValue1"); }
public void Process(string path, string basePath, LocalizableStringCollection strings) { if (string.IsNullOrEmpty(path)) { path = _defaultPath; } if (string.IsNullOrEmpty(basePath)) { basePath = _defaultPath; } var codeMetadataProvider = new CodeMetadataProvider(basePath); var csharpWalker = new ExtractingCodeWalker( new IStringExtractor <SyntaxNode>[] { new SingularStringExtractor(codeMetadataProvider), new PluralStringExtractor(codeMetadataProvider), new DataAnnotationStringExtractor(codeMetadataProvider) }, strings); foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories).OrderBy(file => file)) { using (var stream = File.OpenRead(file)) { using (var reader = new StreamReader(stream)) { var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file); csharpWalker.Visit(syntaxTree.GetRoot()); } } } }
public void TestExecute() { var collection = new LocalizableStringCollection { "neutralValue1", {"en-US", "americaValue1"} }; var neutralCommand = new SetLocalizableString(collection, new LocalizableString {Value = "neutralValue2"}); var americanCommand = new SetLocalizableString(collection, new LocalizableString {Language = new CultureInfo("en-US"), Value = "americaValue2"}); neutralCommand.Execute(); collection.GetExactLanguage(LocalizableString.DefaultLanguage) .Should().Be("neutralValue2", because: "Unspecified language should default to English generic"); neutralCommand.Undo(); collection.GetExactLanguage(LocalizableString.DefaultLanguage) .Should().Be("neutralValue1", because: "Unspecified language should default to English generic"); americanCommand.Execute(); collection.GetExactLanguage(new CultureInfo("en-US")) .Should().Be("americaValue2"); americanCommand.Undo(); collection.GetExactLanguage(new CultureInfo("en-US")) .Should().Be("americaValue1"); }
public override void Process(string path, string basePath, LocalizableStringCollection strings) { var codeMetadataProvider = new CodeMetadataProvider(basePath); var csharpWalker = new ExtractingCodeWalker( new IStringExtractor <SyntaxNode>[] { new SingularStringExtractor(codeMetadataProvider), new PluralStringExtractor(codeMetadataProvider), new DataAnnotationStringExtractor(codeMetadataProvider) }, strings); foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories).OrderBy(file => file)) { if (Path.GetFileName(file).EndsWith(".cshtml.g.cs")) { continue; } using (var stream = File.OpenRead(file)) { using (var reader = new StreamReader(stream)) { var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file); csharpWalker.Visit(syntaxTree.GetRoot()); } } } base.Process(path, basePath, strings); }
public void Add_CreatesNewLocalizableString_IfTheCollectionDoesntContainSameString() { var sut = new LocalizableStringCollection(); sut.Add(s1); sut.Add(otherS); Assert.Contains(sut.Values, o => o.Text == s1.Text); Assert.Contains(sut.Values, o => o.Text == s2.Text); }
static void Main(string[] args) { if (args.Length < 2) { WriteHelp(); return; } var basePath = args[0]; var outputBasePath = args[1]; string[] projectFiles; if (Directory.Exists(basePath)) { projectFiles = Directory.EnumerateFiles(basePath, $"*{ProjectExtension.CS}", SearchOption.AllDirectories).OrderBy(file => file) .Union(Directory.EnumerateFiles(basePath, $"*{ProjectExtension.VB}", SearchOption.AllDirectories).OrderBy(file => file)).ToArray(); } else { WriteHelp(); return; } var processors = new IProjectProcessor[] { new CSharpProjectProcessor(), new VisualBasicProjectProcessor() }; foreach (var projectFilePath in projectFiles) { var projectPath = Path.GetDirectoryName(projectFilePath); var projectBasePath = Path.GetDirectoryName(projectPath) + Path.DirectorySeparatorChar; var projectRelativePath = projectPath.TrimStart(basePath + Path.DirectorySeparatorChar); var outputPath = Path.Combine(outputBasePath, Path.GetFileNameWithoutExtension(projectFilePath) + PoWriter.PortaleObjectTemplateExtension); var strings = new LocalizableStringCollection(); foreach (var processor in processors) { processor.Process(projectPath, projectBasePath, strings); } if (strings.Values.Any()) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); using (var potFile = new PoWriter(outputPath)) { potFile.WriteRecord(strings.Values); } } Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {strings.Values.Count()} strings."); } }
public void TestContainsExactLanguage() { var dictionary = new LocalizableStringCollection { "neutralValue", {"de-DE", "germanyValue"} }; dictionary.ContainsExactLanguage(LocalizableString.DefaultLanguage).Should().BeTrue(because: "Unspecified language should default to English generic"); dictionary.ContainsExactLanguage(new CultureInfo("de-DE")).Should().BeTrue(); dictionary.ContainsExactLanguage(new CultureInfo("de")).Should().BeFalse(); dictionary.ContainsExactLanguage(new CultureInfo("de-AT")).Should().BeFalse(); dictionary.ContainsExactLanguage(new CultureInfo("en-US")).Should().BeFalse(); }
public void Add_CreatesNewLocalizableString_IfTheCollectionIsEmpty() { var sut = new LocalizableStringCollection(); sut.Add(s1); Assert.Single(sut.Values); var result = sut.Values.Single(); Assert.Equal(s1.Text, result.Text); Assert.Contains(s1.Location, result.Locations); }
public void ExtractLocalizedNameFromDisplayAttribute() { // Arrange var csProjectProcessor = new FakeCSharpProjectProcessor(); var localizableStringCollection = new LocalizableStringCollection(); // Act csProjectProcessor.Process(string.Empty, string.Empty, localizableStringCollection); // Assert var localizedStrings = localizableStringCollection.Values.Select(s => s.Text).ToList(); Assert.Contains(localizedStrings, s => s == "First name"); }
public void DataAnnotationsExtractorShouldRespectErrorMessageOrder() { // Arrange var csProjectProcessor = new FakeCSharpProjectProcessor(); var localizableStringCollection = new LocalizableStringCollection(); // Act csProjectProcessor.Process(string.Empty, string.Empty, localizableStringCollection); // Assert var localizedStrings = localizableStringCollection.Values.Select(s => s.Text).ToList(); Assert.Contains(localizedStrings, s => s == "Age should be in the range [15-45]."); }
public void TestRemoveRange() { var dictionary = new LocalizableStringCollection { "neutralValue", {"de-DE", "germanyValue"}, // Intential duplicates (should be ignored) "neutralValue", {"de-DE", "germanyValue"} }; dictionary.Set(LocalizableString.DefaultLanguage, null); dictionary.ContainsExactLanguage(LocalizableString.DefaultLanguage).Should().BeFalse(because: "Unspecified language should default to English generic"); dictionary.Set(new CultureInfo("de-DE"), null); dictionary.ContainsExactLanguage(new CultureInfo("de-DE")).Should().BeFalse(); }
public void Add_AddsLocationToLocalizableString_IfTheCollectionContainsSameString() { var sut = new LocalizableStringCollection(); sut.Add(s1); sut.Add(s2); Assert.Single(sut.Values); var result = sut.Values.Single(); Assert.Equal(s1.Text, result.Text); Assert.Contains(s1.Location, result.Locations); Assert.Contains(s2.Location, result.Locations); }
public void ExtractlocalizedStringsFromDataAnnotations() { // Arrange var csProjectProcessor = new FakeCSharpProjectProcessor(); var localizableStringCollection = new LocalizableStringCollection(); // Act csProjectProcessor.Process(string.Empty, string.Empty, localizableStringCollection); // Assert var localizedStrings = localizableStringCollection.Values.Select(s => s.Text).ToList(); Assert.NotEmpty(localizedStrings); Assert.Equal(2, localizedStrings.Count()); Assert.Contains(localizedStrings, s => s == "The username is required."); }
public void Process(string path, string basePath, LocalizableStringCollection strings) { /* C# */ var codeMetadataProvider = new CodeMetadataProvider(basePath); var csharpWalker = new ExtractingCodeWalker( new IStringExtractor <SyntaxNode>[] { new SingularStringExtractor(codeMetadataProvider), new PluralStringExtractor(codeMetadataProvider) }, strings); foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories)) { if (Path.GetFileName(file).EndsWith(".cshtml.g.cs")) { continue; } using (var stream = File.OpenRead(file)) { using (var reader = new StreamReader(stream)) { var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file); csharpWalker.Visit(syntaxTree.GetRoot()); } } } /* CSHTML */ var razorMetadataProvider = new RazorMetadataProvider(basePath); var razorWalker = new ExtractingCodeWalker( new IStringExtractor <SyntaxNode>[] { new SingularStringExtractor(razorMetadataProvider), new PluralStringExtractor(razorMetadataProvider) }, strings); var compiledViews = ViewCompiler.CompileViews(path); foreach (var view in compiledViews) { try { var syntaxTree = CSharpSyntaxTree.ParseText(view.GeneratedCode, path: view.FilePath); razorWalker.Visit(syntaxTree.GetRoot()); } catch (Exception) { Console.WriteLine("Process failed for: {0}", view.FilePath); } } }
public void Process(string path, string basePath, LocalizableStringCollection strings) { var liquidMetadataProvider = new LiquidMetadataProvider(basePath); var liquidVisitor = new ExtractingLiquidWalker(new[] { new LiquidStringExtractor(liquidMetadataProvider) }, strings); foreach (var file in Directory.EnumerateFiles(path, "*.liquid", SearchOption.AllDirectories).OrderBy(file => file)) { using (var stream = File.OpenRead(file)) { using (var reader = new StreamReader(stream)) { if (_parser.TryParse(reader.ReadToEnd(), out var template, out var errors)) { ProcessTemplate(template, liquidVisitor, file); } } } } }
public virtual void Process(string path, string basePath, LocalizableStringCollection strings) { var razorMetadataProvider = new RazorMetadataProvider(basePath); var razorWalker = new ExtractingCodeWalker(GetStringExtractors(razorMetadataProvider), strings); var compiledViews = ViewCompiler.CompileViews(path); foreach (var view in compiledViews) { try { var syntaxTree = CSharpSyntaxTree.ParseText(view.GeneratedCode, path: view.FilePath); razorWalker.Visit(syntaxTree.GetRoot()); } catch { Console.WriteLine("Process failed for: {0}", view.FilePath); } } }
public void TestSaveLoad() { var collection1 = new LocalizableStringCollection { "neutralValue", {"en-US", "americaValue"}, {"en-GB", "gbValue"}, {"de", "germanValue"}, {"de-DE", "germanyValue"} }; // Serialize and deserialize data Assert.That(collection1, Is.XmlSerializable); string data = collection1.ToXmlString(); var collection2 = XmlStorage.FromXmlString<LocalizableStringCollection>(data); // Ensure data stayed the same collection2.Should().Equal(collection1, because: "Serialized objects should be equal."); collection2.GetSequencedHashCode().Should().Be(collection1.GetSequencedHashCode(), because: "Serialized objects' hashes should be equal."); ReferenceEquals(collection1, collection2).Should().BeFalse(because: "Serialized objects should not return the same reference."); }
public void Process(string path, string basePath, LocalizableStringCollection strings) { var liquidMetadataProvider = new LiquidMetadataProvider(basePath); var liquidVisitor = new ExtractingLiquidWalker(new[] { new LiquidStringExtractor(liquidMetadataProvider) }, strings); foreach (var file in Directory.EnumerateFiles(path, "*.liquid", SearchOption.AllDirectories)) { using (var stream = File.OpenRead(file)) { using (var reader = new StreamReader(stream)) { if (_parser.TryParse(reader.ReadToEnd(), true, out var ast, out var errors)) { foreach (var statement in ast) { liquidVisitor.Visit(new LiquidStatementContext() { Statement = statement, FilePath = file }); } } } } } }
/// <summary> /// Initializes a new instance of the <see cref="ExtractingCodeWalker"/> class /// </summary> /// <param name="extractors">the collection of extractors to use</param> /// <param name="strings">the <see cref="LocalizableStringCollection"/> where the results are saved</param> public ExtractingCodeWalker(IEnumerable <IStringExtractor <SyntaxNode> > extractors, LocalizableStringCollection strings) { _extractors = extractors; _strings = strings; }
/// <summary> /// Creates a new localizable string command. /// </summary> /// <param name="collection">The collection to be modified.</param> /// <param name="element">The entry to be set in the <paramref name="collection"/>.</param> public SetLocalizableString(LocalizableStringCollection collection, LocalizableString element) { _collection = collection; _entry = element; }
static void Main(string[] args) { if (args.Length < 2) { WriteHelp(); return; } var basePath = args[0]; var outputBasePath = args[1]; var parseLiquid = args.Length > 2 && args[2] == "--liquid"; string[] projectFiles; if (Directory.Exists(basePath)) { projectFiles = Directory.EnumerateFiles(basePath, "*.csproj", SearchOption.AllDirectories).ToArray(); } else { WriteHelp(); return; } var processors = new List <IProjectProcessor>(); processors.Add(new CSharpProjectProcessor()); if (parseLiquid) { processors.Add(new LiquidProjectProcessor(ConfigureFluidParser)); } ; foreach (var projectFilePath in projectFiles) { var projectPath = Path.GetDirectoryName(projectFilePath); var projectBasePath = Path.GetDirectoryName(projectPath) + Path.DirectorySeparatorChar; var projectRelativePath = projectPath.TrimStart(basePath + Path.DirectorySeparatorChar); var outputPath = Path.Combine(outputBasePath, Path.GetFileNameWithoutExtension(projectFilePath) + ".pot"); if (ProjectBlacklist.Any(o => projectRelativePath.StartsWith(o))) { continue; } var strings = new LocalizableStringCollection(); foreach (var processor in processors) { processor.Process(projectPath, projectBasePath, strings); } if (strings.Values.Any()) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); using (var potFile = new PoWriter(outputPath)) { potFile.WriteRecord(strings.Values); } } Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {strings.Values.Count()} strings."); } }
public LiquidVisitor(IEnumerable <IStringExtractor <LiquidExpressionContext> > extractors, LocalizableStringCollection strings) { this.Extractors = extractors; this.Strings = strings; }
public void WhenCreated_ValuesCollectionIsEmpty() { var sut = new LocalizableStringCollection(); Assert.Empty(sut.Values); }
public void TestGetExactLanguage() { var dictionary = new LocalizableStringCollection { "neutralValue", {"en-US", "americaValue"}, {"en-GB", "gbValue"}, {"de", "germanValue"}, {"de-DE", "germanyValue"} }; dictionary.GetExactLanguage(LocalizableString.DefaultLanguage).Should().Be("neutralValue", because: "Unspecified language should default to English generic"); dictionary.GetExactLanguage(new CultureInfo("en-US")).Should().Be("americaValue"); dictionary.GetExactLanguage(new CultureInfo("en-CA")).Should().BeNull(); dictionary.GetExactLanguage(new CultureInfo("en-GB")).Should().Be("gbValue"); dictionary.GetExactLanguage(new CultureInfo("de")).Should().Be("germanValue"); dictionary.GetExactLanguage(new CultureInfo("de-DE")).Should().Be("germanyValue"); dictionary.GetExactLanguage(new CultureInfo("de-AT")).Should().BeNull(); }
/// <summary> /// Initializes a new instance of the <see cref="ExtractingLiquidWalker"/> class /// </summary> /// <param name="extractors">the collection of extractors to use</param> /// <param name="strings">the <see cref="LocalizableStringCollection"/> where the resuluts are saved</param> public ExtractingLiquidWalker(IEnumerable <IStringExtractor <LiquidExpressionContext> > extractors, LocalizableStringCollection strings) { _extractors = extractors; _strings = strings; }
public void TestGetBestLanguage() { var dictionary = new LocalizableStringCollection { {"de", "germanValue"}, {"de-DE", "germanyValue"}, {"en-US", "americaValue"}, {"en-GB", "gbValue"}, "neutralValue" }; dictionary.GetBestLanguage(LocalizableString.DefaultLanguage).Should().Be("neutralValue", because: "Unspecified language should default to English generic"); dictionary.GetBestLanguage(new CultureInfo("en-US")).Should().Be("americaValue"); dictionary.GetBestLanguage(new CultureInfo("en-CA")).Should().Be("neutralValue", because: "No exact match, should fall back to English generic"); dictionary.GetBestLanguage(new CultureInfo("en-GB")).Should().Be("gbValue"); dictionary.GetBestLanguage(new CultureInfo("de")).Should().Be("germanValue"); dictionary.GetBestLanguage(new CultureInfo("de-DE")).Should().Be("germanyValue", because: "No exact match, should fall back to German generic"); dictionary.GetBestLanguage(new CultureInfo("de-AT")).Should().Be("germanValue", because: "No exact match, should fall back to German generic"); dictionary.GetBestLanguage(new CultureInfo("es-ES")).Should().Be("neutralValue", because: "No match, should fall back to English generic"); dictionary.Set(LocalizableString.DefaultLanguage, null); dictionary.GetBestLanguage(new CultureInfo("es-ES")).Should().Be("americaValue", because: "No English generic, should fall back to English US"); dictionary.Set(new CultureInfo("en-US"), null); dictionary.GetBestLanguage(new CultureInfo("es-ES")).Should().Be("germanValue", because: "No English US, should fall back to first entry in collection"); }
public void TestSet() { var dictionary = new LocalizableStringCollection { "neutralValue", {"de-DE", "germanyValue"}, // Intential duplicates (should be removed) "neutralValue", {"de-DE", "germanyValue"} }; dictionary.Set(LocalizableString.DefaultLanguage, "neutralValue2"); dictionary.Set(new CultureInfo("de-DE"), "germanyValue2"); dictionary.GetExactLanguage(LocalizableString.DefaultLanguage).Should().Be("neutralValue2"); dictionary.GetExactLanguage(new CultureInfo("de-DE")).Should().Be("germanyValue2"); }