Skip to content

Commit 02a005f

Browse files
author
Chris Gårdenberg
committed
FileResolver
1 parent 7bd7eb1 commit 02a005f

8 files changed

Lines changed: 57 additions & 28 deletions

File tree

MN.L10n.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26430.16
4+
VisualStudioVersion = 15.0.26730.8
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MN.L10n", "MN.L10n\MN.L10n.csproj", "{C224FF20-ACA4-4F7A-81C9-477F3DDE751A}"
77
EndProject
@@ -30,4 +30,7 @@ Global
3030
GlobalSection(SolutionProperties) = preSolution
3131
HideSolutionNode = FALSE
3232
EndGlobalSection
33+
GlobalSection(ExtensibilityGlobals) = postSolution
34+
SolutionGuid = {2EE2AF3E-DFEE-4E30-987F-EFC6D423D331}
35+
EndGlobalSection
3336
EndGlobal

MN.L10n/CodeCompilerModule.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public void BeforeCompile(BeforeCompileContext context)
2121
{
2222
bool isBuildEnvironment = !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("is_build_environment"));
2323

24-
//var runPhrase = true;
24+
var runPhrase = false;
2525
//try { runPhrase = context.Arguments.CompilationOptions.OptimizationLevel == OptimizationLevel.Debug; } catch { }
26-
//if (!runPhrase) return;
26+
if (!runPhrase) return;
2727
Debugger.Break();
2828
Stopwatch stw = new Stopwatch();
2929
stw.Start();
@@ -61,11 +61,19 @@ public void BeforeCompile(BeforeCompileContext context)
6161

6262
Console.WriteLine("info l10n: BuildIdentifier: " + bpIdentifier);
6363

64-
L10n PhraseInstance = L10n.CreateInstance(new NullLanguageProvider("1"), new FileDataProvider(solutionDir));
64+
L10n PhraseInstance = L10n.CreateInstance(new NullLanguageProvider("1"), new FileDataProvider(solutionDir), new FileResolver());
6565

6666
var validExtensions = new[] { ".aspx", ".ascx", ".js", ".jsx" };
6767

68-
var defaultIgnorePaths = new[] { "/.git", "/node_modules", "/.vscode", "/.idea", "/.vs", "/bin", "/obj" };
68+
var defaultIgnorePaths = new[] {
69+
"/.git", "\\.git",
70+
"/node_modules", "\\node_modules",
71+
"/.vscode", "\\.vscode",
72+
"/.idea", "\\.idea",
73+
"/.vs", "\\.vs",
74+
"/bin", "\\bin",
75+
"/obj", "\\obj",
76+
};
6977

7078

7179
List<string> fileList = new List<string>();

MN.L10n/FileResolver.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace MN.L10n
2+
{
3+
public class FileResolver : IFileResolver
4+
{
5+
public bool FileExists(string file)
6+
{
7+
return System.IO.File.Exists(file);
8+
}
9+
}
10+
}

MN.L10n/IFileResolver.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace MN.L10n
2+
{
3+
public interface IFileResolver
4+
{
5+
bool FileExists(string file);
6+
}
7+
}

MN.L10n/Javascript/Loader.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using System;
2-
3-
namespace MN.L10n.Javascript
1+
namespace MN.L10n.Javascript
42
{
53
public static class Loader
64
{
7-
public static string LoadL10nJavascript(string rootRelativePath, Func<string, bool> checkFile)
5+
public static string LoadL10nJavascript(string rootRelativePath)
86
{
97
var lang = L10n.GetLanguage();
108
var jsExt = rootRelativePath.LastIndexOf(".js");
119

1210
var newFileName = string.Format("{0}-{1}.js", rootRelativePath.Substring(0, jsExt), lang);
13-
if (!checkFile(newFileName))
11+
if (!L10n.Instance.FileResolver.FileExists(newFileName))
1412
{
1513
return rootRelativePath;
1614
}

MN.L10n/L10n.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ public partial class L10n
99
private IL10nDataProvider DataProvider { get; set; }
1010
private IL10nLanguageProvider LanguageProvider { get; set; }
1111

12+
internal IFileResolver FileResolver { get; set; }
13+
1214
internal static L10n Instance;
1315

14-
public static L10n CreateInstance(IL10nLanguageProvider langProvider, IL10nDataProvider dataProvider)
16+
public static L10n CreateInstance(IL10nLanguageProvider langProvider, IL10nDataProvider dataProvider, IFileResolver fileResolver)
1517
{
1618
var l10n = dataProvider.LoadL10n();
1719
l10n.DataProvider = dataProvider;
1820
l10n.LanguageProvider = langProvider;
21+
l10n.FileResolver = fileResolver;
1922
Instance = l10n;
2023
return l10n;
2124
}
@@ -27,19 +30,19 @@ public static L10n CreateInstance(IL10nLanguageProvider langProvider, IL10nDataP
2730

2831
public static bool SaveDataProvider()
2932
{
30-
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this.");
33+
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this.");
3134
return Instance.DataProvider.SaveL10n(Instance);
3235
}
3336

3437
public static string _s(string phrase, object args = null)
3538
{
36-
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this.");
39+
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this.");
3740
return Instance.__getPhrase(phrase, args);
3841
}
3942

4043
public static string _m(string phrase, object args = null)
4144
{
42-
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this.");
45+
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this.");
4346

4447
return Instance.ConvertFromMarkdown(Instance.__getPhrase(phrase, args));
4548
}
@@ -51,7 +54,7 @@ public string ConvertFromMarkdown(string phrase)
5154

5255
public static string GetLanguage()
5356
{
54-
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider) to create an instance before using this.");
57+
if (Instance == null) throw new Exception("You must use L10n.CreateInstance(langProvider, dataProvider, fileResolver) to create an instance before using this.");
5558
return Instance.LanguageProvider.GetLanguage();
5659
}
5760

MN.L10n/MN.L10n.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Translation package</Description>
1212
<PackageProjectUrl>https://github.com/MultinetInteractive/MN.L10n</PackageProjectUrl>
1313
<RepositoryType>git</RepositoryType>
1414
<Copyright>© 20XX MultiNet Interactive AB</Copyright>
15-
<Version>1.0.74</Version>
15+
<Version>1.0.76</Version>
1616
<AutoIncrementPackageRevision>True</AutoIncrementPackageRevision>
1717
</PropertyGroup>
1818

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# MN.L10n [![Build status](https://ci.appveyor.com/api/projects/status/y5uh8gvxm29v90rk?svg=true)](https://ci.appveyor.com/project/itssimple/mn-l10n)
22
Translation-thingy for all our products
33

4-
You must implement your `IL10nLanguageProvider` yourself. :) (Basically just `string GetLanguage()`)
4+
You must implement your `IL10nLanguageProvider` and a custom `IFileResolver` (for javascript) yourself. :) (Basically just `string GetLanguage()` and `bool FileExists(string file)`)
55

66
There's also a custom mvc webview `MN.L10n.Mvc.L10nWebView`.
77

@@ -13,7 +13,11 @@ using static MN.L10n.L10n;
1313

1414
void Main()
1515
{
16-
var l10n = MN.L10n.L10n.CreateInstance(new NullLanguageProvider("en-GB"), new FileDataProvider(@"C:\temp\phrase"));
16+
var l10n = MN.L10n.L10n.CreateInstance(
17+
new NullLanguageProvider("en-GB"),
18+
new FileDataProvider(@"C:\temp\phrase"),
19+
new FileResolver()
20+
);
1721

1822
Console.WriteLine(
1923
_s("Det finns $__count$ meddelanden",
@@ -64,15 +68,11 @@ DealDetails.ShowNotification(
6468
...
6569
<script type="text/javascript" src="<%=
6670
ResolveUrl(
67-
MN.L10n.Javascript
68-
.Loader.LoadL10nJavascript(
69-
"~/path/file.js",
70-
(file) =>
71-
{
72-
// Check if a translationfile is available, otherwise default is returned
73-
return System.IO.File.Exists(Server.MapPath(file));
74-
}
75-
)%>"></script>
71+
MN.L10n
72+
.Javascript
73+
.Loader
74+
.LoadL10nJavascript("~/path/file.js")
75+
%>"></script>
7676
...
7777
```
7878

@@ -87,7 +87,7 @@ protected void Application_Start(object sender, EventArgs e)
8787
#endif
8888
ViewEngines.Engines.Add(new RoslynRazorViewEngine());
8989
...
90-
MN.L10n.L10n.CreateInstance(new IL10nLanguageProvider(), new FileDataProvider(@"C:\temp\phrase"));
90+
MN.L10n.L10n.CreateInstance(new IL10nLanguageProvider(), new FileDataProvider(@"C:\temp\phrase"), new FileResolver());
9191
...
9292
}
9393
```

0 commit comments

Comments
 (0)