Skip to content

Commit

Permalink
FileResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Sep 13, 2017
1 parent 7bd7eb1 commit 02a005f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 28 deletions.
5 changes: 4 additions & 1 deletion MN.L10n.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -30,4 +30,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2EE2AF3E-DFEE-4E30-987F-EFC6D423D331}
EndGlobalSection
EndGlobal
16 changes: 12 additions & 4 deletions MN.L10n/CodeCompilerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<string> fileList = new List<string>();
Expand Down
10 changes: 10 additions & 0 deletions MN.L10n/FileResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MN.L10n
{
public class FileResolver : IFileResolver
{
public bool FileExists(string file)
{
return System.IO.File.Exists(file);
}
}
}
7 changes: 7 additions & 0 deletions MN.L10n/IFileResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MN.L10n
{
public interface IFileResolver
{
bool FileExists(string file);
}
}
8 changes: 3 additions & 5 deletions MN.L10n/Javascript/Loader.cs
Original file line number Diff line number Diff line change
@@ -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<string, bool> 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;
}
Expand Down
13 changes: 8 additions & 5 deletions MN.L10n/L10n.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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));
}
Expand All @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion MN.L10n/MN.L10n.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Translation package</Description>
<PackageProjectUrl>https://github.com/MultinetInteractive/MN.L10n</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<Copyright>© 20XX MultiNet Interactive AB</Copyright>
<Version>1.0.74</Version>
<Version>1.0.76</Version>
<AutoIncrementPackageRevision>True</AutoIncrementPackageRevision>
</PropertyGroup>

Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand All @@ -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",
Expand Down Expand Up @@ -64,15 +68,11 @@ DealDetails.ShowNotification(
...
<script type="text/javascript" src="<%=
ResolveUrl(
MN.L10n.Javascript
.Loader.LoadL10nJavascript(
"~/path/file.js",
(file) =>
{
// Check if a translationfile is available, otherwise default is returned
return System.IO.File.Exists(Server.MapPath(file));
}
)%>"></script>
MN.L10n
.Javascript
.Loader
.LoadL10nJavascript("~/path/file.js")
%>"></script>
...
```

Expand All @@ -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());
...
}
```
Expand Down

0 comments on commit 02a005f

Please sign in to comment.