Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Defines a mechanism for retrieving a specific localizer template set `setname` f

### VersionOne.Localization.FileTemplateSetLoader

Implements `ITemplateSetLoader` interface for building localizer template sets from a set of text files (formatted according to `TextTemplateSet` specification above) located in a folder specified by the `path` constructor argument. For each requested `culture` and `setname` combination, it either produces an `ITemplateSet` instance (backed by `TextTemplateSet`) from a file named `<culture>.<setname>.txt`, or yields `null` if no such file exists.
Implements `ITemplateSetLoader` interface for building localizer template sets from a set of text files (formatted according to `TextTemplateSet` specification above) located in a folder specified by the `path` constructor argument. For each requested `culture` and `setname` combination, it either produces an `ITemplateSet` instance (backed by `TextTemplateSet`) from a file named `<culture>.<setname>.txt`, or yields `null` if no such file exists. **Text file content must be encoded using UTF8.**

Sample Web Application Implementation
----------------------------------
Expand Down
3 changes: 2 additions & 1 deletion source/FileTemplateSetLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Text;

namespace VersionOne.Localization
{
Expand All @@ -16,7 +17,7 @@ public ITemplateSet Load(string culture, string setname)
string filename = Path.Combine(_path, string.Format("{0}.{1}.txt", culture, setname));
if (!File.Exists(filename))
return null;
TextReader reader = new StreamReader(filename);
TextReader reader = new StreamReader(filename, new UTF8Encoding(false, true));
return new TextTemplateSet(reader);
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/FileTemplateSetLoaderTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace VersionOne.Localization.Tests
{
public class FileTemplateSetLoaderTester
{
private IEnumerable<Template> SetupTemplates(Encoding encoding)
{
using (var writer = new StreamWriter("no.Test.txt", false, encoding))
writer.WriteLine("løsningsforslag=solutions");

return LoadTemplates().ToList();
}

private IEnumerable<Template> LoadTemplates()
{
var loader = new FileTemplateSetLoader(".");
using (var set = loader.Load("no", "Test"))
yield return set.GetNextTemplate();
}

[Test] public void BOMless_UTF8_Works()
{
var templates = SetupTemplates(new UTF8Encoding(false));
Assert.IsNotEmpty(templates);
Assert.AreEqual("løsningsforslag", templates.First().Tag);
}

[Test] public void UTF8_Works()
{
var templates = SetupTemplates(new UTF8Encoding(true));
Assert.IsNotEmpty(templates);
Assert.AreEqual("løsningsforslag", templates.First().Tag);
}

[ExpectedException(typeof(DecoderFallbackException))]
[Test] public void ANSI_Throws()
{
var templates = SetupTemplates(Encoding.GetEncoding(CultureInfo.GetCultureInfo("no").TextInfo.ANSICodePage));
Assert.IsEmpty(templates);
}
}
}
18 changes: 18 additions & 0 deletions tests/LocalizerTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,23 @@ [Test] public void EmptyBraces()
loc.Add("A", "{}");
Assert.AreEqual("", loc.Resolve("A"));
}

[Test] public void TagWithSpecialCharacter()
{
const string tag = "l�sningsforslag";
const string translation = "solution";
var loc = new Localizer(null);
loc.Add(tag, translation);
Assert.AreEqual(translation, loc.Resolve(tag));
}

[Test] public void ValueWithSpecialCharacter()
{
const string tag = "solution";
const string translation = "l�sningsforslag";
var loc = new Localizer(null);
loc.Add(tag, translation);
Assert.AreEqual(translation, loc.Resolve(tag));
}
}
}
1 change: 1 addition & 0 deletions tests/VersionOne.Localization.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CulturalTester.cs" />
<Compile Include="FileTemplateSetLoaderTester.cs" />
<Compile Include="LocalizerTester.cs" />
<Compile Include="MyTemplateSetLoader.cs" />
<Compile Include="SetOverrideTester.cs" />
Expand Down