mirror of
https://github.com/abdelkader/vCardEditor
synced 2025-12-12 08:27:19 +07:00
Added test and refactored the localization
This commit is contained in:
@@ -8,7 +8,7 @@ namespace vCardEditor
|
||||
{
|
||||
private readonly LocalizationFile _localization;
|
||||
private string _currentLanguage;
|
||||
public JsonLocalizationProvider(LocalizationFile localization, string defaultLanguage = "fr")
|
||||
public JsonLocalizationProvider(LocalizationFile localization, string defaultLanguage = "en")
|
||||
{
|
||||
_localization = localization;
|
||||
_currentLanguage = defaultLanguage;
|
||||
@@ -32,9 +32,9 @@ namespace vCardEditor
|
||||
return value;
|
||||
}
|
||||
|
||||
if (_localization.languages.TryGetValue("en", out var fallback))
|
||||
if (_localization.languages.TryGetValue("en", out var fallbackLang))
|
||||
{
|
||||
if (lang.messages.TryGetValue(key, out var fallbackMsg))
|
||||
if (fallbackLang.messages.TryGetValue(key, out var fallbackMsg))
|
||||
return fallbackMsg;
|
||||
}
|
||||
|
||||
|
||||
7
vCardEditor/Libs/TinyJson/IParser.cs
Normal file
7
vCardEditor/Libs/TinyJson/IParser.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace vCardEditor.Libs.TinyJson
|
||||
{
|
||||
public interface IParser
|
||||
{
|
||||
T Deserialize<T>(string json);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +1,28 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using TinyJson;
|
||||
using vCardEditor.Repository;
|
||||
|
||||
namespace vCardEditor.Libs.TinyJson
|
||||
{
|
||||
public static class LocalizationLoader
|
||||
public class LocalizationLoader
|
||||
{
|
||||
private const string EmbeddedResourceName = "vCardEditor.i18n.lang.json";
|
||||
private readonly IParser _parser;
|
||||
private readonly IFileHandler _fileHandler;
|
||||
|
||||
public static LocalizationFile LoadEmbedded()
|
||||
public LocalizationLoader(IParser parser, IFileHandler fileHandler)
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
|
||||
using (var stream = assembly.GetManifestResourceStream(EmbeddedResourceName))
|
||||
{
|
||||
if (stream == null)
|
||||
throw new FileNotFoundException($"Ressource embarquée '{EmbeddedResourceName}' introuvable.");
|
||||
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
var json = reader.ReadToEnd();
|
||||
return Deserialize(json);
|
||||
}
|
||||
}
|
||||
_parser = parser;
|
||||
_fileHandler = fileHandler;
|
||||
}
|
||||
|
||||
//public static LocalizationFile LoadFromFile(string filePath)
|
||||
//{
|
||||
// if (!File.Exists(filePath))
|
||||
// return new LocalizationFile(); // fichier inexistant, retourne un fichier vide
|
||||
|
||||
// var json = File.ReadAllText(filePath);
|
||||
// return Deserialize(json);
|
||||
//}
|
||||
|
||||
private static LocalizationFile Deserialize(string json)
|
||||
public LocalizationFile LoadEmbedded(string EmbeddedResourceName = "vCardEditor.i18n.lang.json")
|
||||
{
|
||||
var result = JSONParser.FromJson<LocalizationFile>(json);
|
||||
string json = _fileHandler.LoadJsonFromAssembly(EmbeddedResourceName);
|
||||
return Deserialize(json);
|
||||
}
|
||||
|
||||
|
||||
private LocalizationFile Deserialize(string json)
|
||||
{
|
||||
var result = _parser.Deserialize<LocalizationFile>(json);
|
||||
return result ?? new LocalizationFile();
|
||||
}
|
||||
|
||||
|
||||
12
vCardEditor/Libs/TinyJson/TinyJsonParser.cs
Normal file
12
vCardEditor/Libs/TinyJson/TinyJsonParser.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using TinyJson;
|
||||
|
||||
namespace vCardEditor.Libs.TinyJson
|
||||
{
|
||||
public class TinyJsonParser : IParser
|
||||
{
|
||||
public T Deserialize<T>(string json)
|
||||
{
|
||||
return JSONParser.FromJson<T>(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,11 @@ namespace vCardEditor
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
|
||||
var embedded = LocalizationLoader.LoadEmbedded();
|
||||
var localizationProvider = new JsonLocalizationProvider(embedded);
|
||||
|
||||
FileHandler fileHandler = new FileHandler();
|
||||
|
||||
var embeddedlang = new LocalizationLoader(new TinyJsonParser(), fileHandler).LoadEmbedded();
|
||||
var localizationProvider = new JsonLocalizationProvider(embeddedlang);
|
||||
|
||||
MainForm mainForm = new MainForm();
|
||||
new MainPresenter(mainForm, new ContactRepository(fileHandler), localizationProvider);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.IO;
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
namespace vCardEditor.Repository
|
||||
{
|
||||
@@ -60,5 +60,21 @@ namespace vCardEditor.Repository
|
||||
string[] filePaths = Directory.GetFiles(path, ext,SearchOption.TopDirectoryOnly);
|
||||
return filePaths;
|
||||
}
|
||||
|
||||
public string LoadJsonFromAssembly(string EmbeddedResourceName)
|
||||
{
|
||||
string json;
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
using (var stream = assembly.GetManifestResourceStream(EmbeddedResourceName))
|
||||
{
|
||||
if (stream == null)
|
||||
throw new FileNotFoundException($"Embedded resource '{EmbeddedResourceName}' not found.");
|
||||
|
||||
using (var reader = new StreamReader(stream))
|
||||
json = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace vCardEditor.Repository
|
||||
void WriteBytesToFile(string imageFile, byte[] image);
|
||||
string GetVcfFileName(string folderPath, string familyName);
|
||||
string GetFileNameWithExtension(string fileName, int index, string extension);
|
||||
string LoadJsonFromAssembly(string EmbeddedResourceName);
|
||||
string[] GetFiles(string path, string ext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,9 +86,11 @@
|
||||
<Compile Include="Libs\QRCoder\QRCodeData.cs" />
|
||||
<Compile Include="Libs\QRCoder\QRCodeGenerator.cs" />
|
||||
<Compile Include="Libs\QRCoder\SvgQRCode.cs" />
|
||||
<Compile Include="Libs\TinyJson\IParser.cs" />
|
||||
<Compile Include="Libs\TinyJson\JSONParser.cs" />
|
||||
<Compile Include="Libs\TinyJson\LocalizationFile.cs" />
|
||||
<Compile Include="Libs\TinyJson\LocalizationLoader.cs" />
|
||||
<Compile Include="Libs\TinyJson\TinyJsonParser.cs" />
|
||||
<Compile Include="Model\vCardPropeties.cs" />
|
||||
<Compile Include="Model\Column.cs" />
|
||||
<Compile Include="Model\FixedList.cs" />
|
||||
|
||||
48
vCardEditor_Test/JsonEntries.cs
Normal file
48
vCardEditor_Test/JsonEntries.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
namespace vCardEditor_Test
|
||||
{
|
||||
public class JsonEntries
|
||||
{
|
||||
public static string[] JsonEmtpy
|
||||
{
|
||||
get
|
||||
{
|
||||
return "".Split('\n');
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] JsonIncorrect
|
||||
{
|
||||
get
|
||||
{
|
||||
return "abcdef".Split('\n');
|
||||
}
|
||||
}
|
||||
|
||||
public static string JsonValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"{
|
||||
""version"": ""1.0"",
|
||||
""languages"": {
|
||||
""en"": {
|
||||
""name"": ""English"",
|
||||
""messages"": {
|
||||
""MSG_001"": ""Save current file before?"",
|
||||
""MSG_002"": ""File""
|
||||
}
|
||||
},
|
||||
""fr"": {
|
||||
""name"": ""Français"",
|
||||
""messages"": {
|
||||
""MSG_001"": ""Sauvegarder le fichier en cours"",
|
||||
""MSG_002"": ""Fichier""
|
||||
}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
26
vCardEditor_Test/LocalizationLoaderTest.cs
Normal file
26
vCardEditor_Test/LocalizationLoaderTest.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using vCardEditor.Libs.TinyJson;
|
||||
using vCardEditor.Repository;
|
||||
|
||||
namespace vCardEditor_Test
|
||||
{
|
||||
[TestClass]
|
||||
public class LocalizationLoaderTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void CorrectJsonLoaded_Test()
|
||||
{
|
||||
var fileHandler = Substitute.For<IFileHandler>();
|
||||
fileHandler.LoadJsonFromAssembly(Arg.Any<string>()).Returns(JsonEntries.JsonValid);
|
||||
var JsonParser = new TinyJsonParser();
|
||||
|
||||
var embeddedlang = new LocalizationLoader(JsonParser, fileHandler).LoadEmbedded();
|
||||
|
||||
Assert.IsTrue(embeddedlang.version == "1.0", "Json version incorrect");
|
||||
Assert.IsTrue(embeddedlang.languages.Count == 2, "Number of json entries invalid");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,10 +77,12 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ContactRepositoryTest.cs" />
|
||||
<Compile Include="JsonEntries.cs" />
|
||||
<Compile Include="Entries.cs" />
|
||||
<Compile Include="FixedListTest.cs" />
|
||||
<Compile Include="MainPresenterTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="LocalizationLoaderTest.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\vCardEditor\vCardEditor.csproj">
|
||||
|
||||
Reference in New Issue
Block a user