From 02a005f3e614e77131ce221247c489962424764d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20G=C3=A5rdenberg?= Date: Wed, 13 Sep 2017 10:37:37 +0200 Subject: [PATCH] FileResolver --- MN.L10n.sln | 5 ++++- MN.L10n/CodeCompilerModule.cs | 16 ++++++++++++---- MN.L10n/FileResolver.cs | 10 ++++++++++ MN.L10n/IFileResolver.cs | 7 +++++++ MN.L10n/Javascript/Loader.cs | 8 +++----- MN.L10n/L10n.cs | 13 ++++++++----- MN.L10n/MN.L10n.csproj | 2 +- README.md | 24 ++++++++++++------------ 8 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 MN.L10n/FileResolver.cs create mode 100644 MN.L10n/IFileResolver.cs diff --git a/MN.L10n.sln b/MN.L10n.sln index 63b858f..35c2308 100644 --- a/MN.L10n.sln +++ b/MN.L10n.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26430.16 +VisualStudioVersion = 15.0.26730.8 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MN.L10n", "MN.L10n\MN.L10n.csproj", "{C224FF20-ACA4-4F7A-81C9-477F3DDE751A}" EndProject @@ -30,4 +30,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2EE2AF3E-DFEE-4E30-987F-EFC6D423D331} + EndGlobalSection EndGlobal diff --git a/MN.L10n/CodeCompilerModule.cs b/MN.L10n/CodeCompilerModule.cs index 4adfbb5..f0f33f8 100644 --- a/MN.L10n/CodeCompilerModule.cs +++ b/MN.L10n/CodeCompilerModule.cs @@ -21,9 +21,9 @@ public void BeforeCompile(BeforeCompileContext context) { bool isBuildEnvironment = !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("is_build_environment")); - //var runPhrase = true; + var runPhrase = false; //try { runPhrase = context.Arguments.CompilationOptions.OptimizationLevel == OptimizationLevel.Debug; } catch { } - //if (!runPhrase) return; + if (!runPhrase) return; Debugger.Break(); Stopwatch stw = new Stopwatch(); stw.Start(); @@ -61,11 +61,19 @@ public void BeforeCompile(BeforeCompileContext context) Console.WriteLine("info l10n: BuildIdentifier: " + bpIdentifier); - L10n PhraseInstance = L10n.CreateInstance(new NullLanguageProvider("1"), new FileDataProvider(solutionDir)); + L10n PhraseInstance = L10n.CreateInstance(new NullLanguageProvider("1"), new FileDataProvider(solutionDir), new FileResolver()); var validExtensions = new[] { ".aspx", ".ascx", ".js", ".jsx" }; - var defaultIgnorePaths = new[] { "/.git", "/node_modules", "/.vscode", "/.idea", "/.vs", "/bin", "/obj" }; + var defaultIgnorePaths = new[] { + "/.git", "\\.git", + "/node_modules", "\\node_modules", + "/.vscode", "\\.vscode", + "/.idea", "\\.idea", + "/.vs", "\\.vs", + "/bin", "\\bin", + "/obj", "\\obj", + }; List fileList = new List(); diff --git a/MN.L10n/FileResolver.cs b/MN.L10n/FileResolver.cs new file mode 100644 index 0000000..dfd82b6 --- /dev/null +++ b/MN.L10n/FileResolver.cs @@ -0,0 +1,10 @@ +namespace MN.L10n +{ + public class FileResolver : IFileResolver + { + public bool FileExists(string file) + { + return System.IO.File.Exists(file); + } + } +} diff --git a/MN.L10n/IFileResolver.cs b/MN.L10n/IFileResolver.cs new file mode 100644 index 0000000..ab24103 --- /dev/null +++ b/MN.L10n/IFileResolver.cs @@ -0,0 +1,7 @@ +namespace MN.L10n +{ + public interface IFileResolver + { + bool FileExists(string file); + } +} diff --git a/MN.L10n/Javascript/Loader.cs b/MN.L10n/Javascript/Loader.cs index 6f7e241..eb079e6 100644 --- a/MN.L10n/Javascript/Loader.cs +++ b/MN.L10n/Javascript/Loader.cs @@ -1,16 +1,14 @@ -using System; - -namespace MN.L10n.Javascript +namespace MN.L10n.Javascript { public static class Loader { - public static string LoadL10nJavascript(string rootRelativePath, Func checkFile) + public static string LoadL10nJavascript(string rootRelativePath) { var lang = L10n.GetLanguage(); var jsExt = rootRelativePath.LastIndexOf(".js"); var newFileName = string.Format("{0}-{1}.js", rootRelativePath.Substring(0, jsExt), lang); - if (!checkFile(newFileName)) + if (!L10n.Instance.FileResolver.FileExists(newFileName)) { return rootRelativePath; } diff --git a/MN.L10n/L10n.cs b/MN.L10n/L10n.cs index a7396e1..dc5828a 100644 --- a/MN.L10n/L10n.cs +++ b/MN.L10n/L10n.cs @@ -9,13 +9,16 @@ public partial class L10n private IL10nDataProvider DataProvider { get; set; } private IL10nLanguageProvider LanguageProvider { get; set; } + internal IFileResolver FileResolver { get; set; } + internal static L10n Instance; - public static L10n CreateInstance(IL10nLanguageProvider langProvider, IL10nDataProvider dataProvider) + public static L10n CreateInstance(IL10nLanguageProvider langProvider, IL10nDataProvider dataProvider, IFileResolver fileResolver) { var l10n = dataProvider.LoadL10n(); l10n.DataProvider = dataProvider; l10n.LanguageProvider = langProvider; + l10n.FileResolver = fileResolver; Instance = l10n; return l10n; } @@ -27,19 +30,19 @@ public static L10n CreateInstance(IL10nLanguageProvider langProvider, IL10nDataP public static bool SaveDataProvider() { - if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this."); + if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this."); return Instance.DataProvider.SaveL10n(Instance); } public static string _s(string phrase, object args = null) { - if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this."); + if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this."); return Instance.__getPhrase(phrase, args); } public static string _m(string phrase, object args = null) { - if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this."); + if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this."); return Instance.ConvertFromMarkdown(Instance.__getPhrase(phrase, args)); } @@ -51,7 +54,7 @@ public string ConvertFromMarkdown(string phrase) public static string GetLanguage() { - if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this."); + if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this."); return Instance.LanguageProvider.GetLanguage(); } diff --git a/MN.L10n/MN.L10n.csproj b/MN.L10n/MN.L10n.csproj index 6dbd5e0..2d0906e 100644 --- a/MN.L10n/MN.L10n.csproj +++ b/MN.L10n/MN.L10n.csproj @@ -12,7 +12,7 @@ Translation package https://github.com/MultinetInteractive/MN.L10n git © 20XX MultiNet Interactive AB - 1.0.74 + 1.0.76 True diff --git a/README.md b/README.md index 4b84536..2c6521a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # MN.L10n [![Build status](https://ci.appveyor.com/api/projects/status/y5uh8gvxm29v90rk?svg=true)](https://ci.appveyor.com/project/itssimple/mn-l10n) Translation-thingy for all our products -You must implement your `IL10nLanguageProvider` yourself. :) (Basically just `string GetLanguage()`) +You must implement your `IL10nLanguageProvider` and a custom `IFileResolver` (for javascript) yourself. :) (Basically just `string GetLanguage()` and `bool FileExists(string file)`) There's also a custom mvc webview `MN.L10n.Mvc.L10nWebView`. @@ -13,7 +13,11 @@ using static MN.L10n.L10n; void Main() { - var l10n = MN.L10n.L10n.CreateInstance(new NullLanguageProvider("en-GB"), new FileDataProvider(@"C:\temp\phrase")); + var l10n = MN.L10n.L10n.CreateInstance( + new NullLanguageProvider("en-GB"), + new FileDataProvider(@"C:\temp\phrase"), + new FileResolver() + ); Console.WriteLine( _s("Det finns $__count$ meddelanden", @@ -64,15 +68,11 @@ DealDetails.ShowNotification( ... + MN.L10n + .Javascript + .Loader + .LoadL10nJavascript("~/path/file.js") +%>"> ... ``` @@ -87,7 +87,7 @@ protected void Application_Start(object sender, EventArgs e) #endif ViewEngines.Engines.Add(new RoslynRazorViewEngine()); ... - MN.L10n.L10n.CreateInstance(new IL10nLanguageProvider(), new FileDataProvider(@"C:\temp\phrase")); + MN.L10n.L10n.CreateInstance(new IL10nLanguageProvider(), new FileDataProvider(@"C:\temp\phrase"), new FileResolver()); ... } ```