Skip to content
This repository
Browse code

Changes made as per @TheCodeJunkie PR comments

  • Loading branch information...
commit 8c1cd927f279da7dfeb91fd9a6c980ce646ae9fb 1 parent e116287
Jonathan Channon jchannon authored
29 src/Nancy.Demo.Localization/Modules/HomeModule.cs
... ... @@ -1,31 +1,20 @@
1   -using System;
2   -using System.Collections.Generic;
3   -using System.Globalization;
4   -using System.Linq;
5   -using System.Threading;
6   -using System.Web;
7   -
8   -namespace Nancy.Demo.Localization
  1 +namespace Nancy.Demo.Razor.Localization.Modules
9 2 {
  3 + using System.Globalization;
  4 +
10 5 public class HomeModule : NancyModule
11 6 {
12 7 public HomeModule()
13 8 {
14   - Get["/"] = parameters =>
15   - {
16   - return View["Index"];
17   - };
  9 + Get["/"] = parameters => View["Index"];
18 10
19   - Get["/cultureview"] = parameters =>
20   - {
21   - return View["CultureView"];
22   - };
  11 + Get["/cultureview"] = parameters => View["CultureView"];
23 12
24 13 Get["/cultureviewgerman"] = parameters =>
25   - {
26   - this.Context.Culture = new CultureInfo("de-DE");
27   - return View["CultureView"];
28   - };
  14 + {
  15 + Context.Culture = new CultureInfo("de-DE");
  16 + return View["CultureView"];
  17 + };
29 18 }
30 19 }
31 20 }
4 ....Demo.Localization/Nancy.Demo.Localization.csproj → ...Localization/Nancy.Demo.Razor.Localization.csproj
@@ -11,8 +11,8 @@
11 11 <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
12 12 <OutputType>Library</OutputType>
13 13 <AppDesignerFolder>Properties</AppDesignerFolder>
14   - <RootNamespace>Nancy.Demo.Localization</RootNamespace>
15   - <AssemblyName>Nancy.Demo.Localization</AssemblyName>
  14 + <RootNamespace>Nancy.Demo.Razor.Localization</RootNamespace>
  15 + <AssemblyName>Nancy.Demo.Razor.Localization</AssemblyName>
16 16 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
17 17 <UseIISExpress>true</UseIISExpress>
18 18 <IISExpressSSLPort />
4 src/Nancy.Demo.Localization/Resources/Text.Designer.cs
@@ -8,7 +8,7 @@
8 8 // </auto-generated>
9 9 //------------------------------------------------------------------------------
10 10
11   -namespace Nancy.Demo.Localization.Resources {
  11 +namespace Nancy.Demo.Razor.Localization.Resources {
12 12 using System;
13 13
14 14
@@ -39,7 +39,7 @@ public class Text {
39 39 public static global::System.Resources.ResourceManager ResourceManager {
40 40 get {
41 41 if (object.ReferenceEquals(resourceMan, null)) {
42   - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Nancy.Demo.Localization.Resources.Text", typeof(Text).Assembly);
  42 + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Nancy.Demo.Razor.Localization.Resources.Text", typeof(Text).Assembly);
43 43 resourceMan = temp;
44 44 }
45 45 return resourceMan;
4 src/Nancy.Tests/Unit/ViewEngines/DefaultTextResourceFixture.cs
... ... @@ -1,7 +1,7 @@
1 1 namespace Nancy.Tests.Unit.ViewEngines
2 2 {
3   - using Nancy.ViewEngines;
4 3 using Xunit;
  4 + using Nancy.Localization;
5 5
6 6 public class DefaultTextResourceFixture
7 7 {
@@ -9,7 +9,7 @@ public class DefaultTextResourceFixture
9 9 public void Should_Return_Null_If_No_Assembly_Found()
10 10 {
11 11 //Given
12   - var defaultTextResource = new DefaultTextResource();
  12 + var defaultTextResource = new ResourceBasedTextResource();
13 13 var context = new NancyContext();
14 14
15 15 //When
7 src/Nancy.ViewEngines.Razor.Tests/RazorViewEngineFixture.cs
... ... @@ -1,7 +1,4 @@
1   -using System.Threading;
2   -using Nancy.Culture;
3   -
4   -namespace Nancy.ViewEngines.Razor.Tests
  1 +namespace Nancy.ViewEngines.Razor.Tests
5 2 {
6 3 using System;
7 4 using System.Dynamic;
@@ -12,6 +9,8 @@ namespace Nancy.ViewEngines.Razor.Tests
12 9 using Xunit;
13 10 using Nancy.Tests;
14 11 using Nancy.ViewEngines.Razor.Tests.Models;
  12 + using System.Threading;
  13 + using Nancy.Localization;
15 14
16 15 public class RazorViewEngineFixture
17 16 {
2  src/Nancy.ViewEngines.Razor/RazorViewEngine.cs
@@ -11,6 +11,7 @@
11 11 using System.Web.Razor.Parser.SyntaxTree;
12 12 using Nancy.Bootstrapper;
13 13 using Nancy.Responses;
  14 + using Nancy.Localization;
14 15
15 16 /// <summary>
16 17 /// View engine for rendering razor views.
@@ -36,6 +37,7 @@ public IEnumerable<string> Extensions
36 37 /// Initializes a new instance of the <see cref="RazorViewEngine"/> class.
37 38 /// </summary>
38 39 /// <param name="configuration">The <see cref="IRazorConfiguration"/> that should be used by the engine.</param>
  40 + /// <param name="textResource">The <see cref="ITextResource"/> that should be used by the engine.</param>
39 41 public RazorViewEngine(IRazorConfiguration configuration, ITextResource textResource)
40 42 {
41 43 this.viewRenderers = new List<IRazorViewRenderer>
8 src/Nancy.ViewEngines.Razor/TextResourceFinder.cs
... ... @@ -1,6 +1,7 @@
1 1 namespace Nancy.ViewEngines.Razor
2 2 {
3 3 using System.Dynamic;
  4 + using Nancy.Localization;
4 5
5 6 /// <summary>
6 7 /// Returns text from an implemented ITextResource
@@ -10,6 +11,11 @@ public class TextResourceFinder : DynamicObject
10 11 private readonly ITextResource textResource;
11 12 private readonly NancyContext context;
12 13
  14 + /// <summary>
  15 + /// Initializes a new instance of the <see cref="TextResourceFinder"/> class.
  16 + /// </summary>
  17 + /// <param name="textResource">The <see cref="ITextResource"/> that should be used by the TextResourceFinder</param>
  18 + /// <param name="context">The <see cref="NancyContext"/> that should be used by the TextResourceFinder</param>
13 19 public TextResourceFinder(ITextResource textResource, NancyContext context)
14 20 {
15 21 this.textResource = textResource;
@@ -21,7 +27,7 @@ public TextResourceFinder(ITextResource textResource, NancyContext context)
21 27 /// </summary>
22 28 /// <param name="binder">GetMemberBinder with dynamic text key</param>
23 29 /// <param name="result">Text item</param>
24   - /// <returns></returns>
  30 + /// <returns>Returns a value or a non existing value from the <see cref="ITextResource"/> implementation</returns>
25 31 public override bool TryGetMember(GetMemberBinder binder, out object result)
26 32 {
27 33 result = this.textResource[binder.Name, this.context];
2  src/Nancy.sln
@@ -103,7 +103,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Authentication.S
103 103 EndProject
104 104 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Authentication.Stateless", "Nancy.Demo.Authentication.Stateless\Nancy.Demo.Authentication.Stateless.csproj", "{BAE74CD5-57C2-40E3-8F7A-EDE5721C2ACC}"
105 105 EndProject
106   -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Localization", "Nancy.Demo.Localization\Nancy.Demo.Localization.csproj", "{396F0BCE-5B51-4B6A-931E-312880C24725}"
  106 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Razor.Localization", "Nancy.Demo.Localization\Nancy.Demo.Razor.Localization.csproj", "{396F0BCE-5B51-4B6A-931E-312880C24725}"
107 107 EndProject
108 108 Global
109 109 GlobalSection(SolutionConfigurationPlatforms) = preSolution
3  src/Nancy/Bootstrapper/NancyInternalConfiguration.cs
@@ -15,6 +15,7 @@ namespace Nancy.Bootstrapper
15 15 using Security;
16 16 using Nancy.Validation;
17 17 using Nancy.Culture;
  18 + using Nancy.Localization;
18 19
19 20 /// <summary>
20 21 /// Configuration class for Nancy's internals.
@@ -90,7 +91,7 @@ public static NancyInternalConfiguration Default
90 91 RouteSegmentExtractor = typeof(DefaultRouteSegmentExtractor),
91 92 RouteDescriptionProvider = typeof(DefaultRouteDescriptionProvider),
92 93 CultureService = typeof(DefaultCultureService),
93   - DefaultTextResource = typeof(DefaultTextResource),
  94 + DefaultTextResource = typeof(ResourceBasedTextResource),
94 95 };
95 96 }
96 97 }
10 src/Nancy/Localization/ITextResource.cs
... ... @@ -0,0 +1,10 @@
  1 +namespace Nancy.Localization
  2 +{
  3 + /// <summary>
  4 + /// Used to return string values
  5 + /// </summary>
  6 + public interface ITextResource
  7 + {
  8 + string this[string key, NancyContext context] { get; }
  9 + }
  10 +}
51 src/Nancy/Localization/ResourceBasedTextResource.cs
... ... @@ -0,0 +1,51 @@
  1 +namespace Nancy.Localization
  2 +{
  3 + using System;
  4 + using System.Linq;
  5 + using System.Reflection;
  6 + using System.Resources;
  7 +
  8 + /// <summary>
  9 + /// Resx implementation of ITextResource
  10 + /// </summary>
  11 + public class ResourceBasedTextResource : ITextResource
  12 + {
  13 + private readonly Assembly culturedAssembly;
  14 + private readonly ResourceManager resourceManager;
  15 +
  16 + /// <summary>
  17 + /// Initializes a new instance of <see cref="ResourceBasedTextResource"/> to read strings from *.resx files
  18 + /// </summary>
  19 + /// <remarks>Looks for *.resx files in a Resources folder with files called Text.resx as default or Text.CultureName.resx eg/ Text.en-GB.resx</remarks>
  20 + public ResourceBasedTextResource()
  21 + {
  22 + var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  23 +
  24 + culturedAssembly = assemblies.FirstOrDefault(x => x.GetManifestResourceNames().Any(y => y.Contains(".Resources.Text")));
  25 + if (culturedAssembly != null)
  26 + {
  27 + resourceManager = new ResourceManager(culturedAssembly.GetName().Name + ".Resources.Text",
  28 + culturedAssembly);
  29 + }
  30 + }
  31 +
  32 + /// <summary>
  33 + /// Used to return a string value from *.resx files
  34 + /// </summary>
  35 + /// <param name="key">The key to look for in the resource file</param>
  36 + /// <param name="context">The NancyContext used to determine the culture for returning culture specific values</param>
  37 + /// <returns>Returns a string value from culture specific or default file or null if key does not exist as determined by <see cref="ResourceManager"/> </returns>
  38 + public string this[string key, NancyContext context]
  39 + {
  40 + get
  41 + {
  42 + if (resourceManager == null)
  43 + {
  44 + return null;
  45 + }
  46 +
  47 + return resourceManager.GetString(key, context.Culture);
  48 + }
  49 + }
  50 + }
  51 +}
4 src/Nancy/Nancy.csproj
@@ -271,7 +271,7 @@
271 271 <Compile Include="ViewEngines\DefaultRenderContextFactory.cs" />
272 272 <Compile Include="ViewEngines\DefaultResourceAssemblyProvider.cs" />
273 273 <Compile Include="ViewEngines\DefaultResourceReader.cs" />
274   - <Compile Include="ViewEngines\DefaultTextResource.cs" />
  274 + <Compile Include="Localization\ResourceBasedTextResource.cs" />
275 275 <Compile Include="ViewEngines\DefaultViewCache.cs" />
276 276 <Compile Include="ViewEngines\DefaultViewLocationCache.cs" />
277 277 <Compile Include="ViewEngines\DefaultViewRenderer.cs" />
@@ -374,7 +374,7 @@
374 374 <Compile Include="ViewEngines\IRenderContextFactory.cs" />
375 375 <Compile Include="ViewEngines\IResourceAssemblyProvider.cs" />
376 376 <Compile Include="ViewEngines\IResourceReader.cs" />
377   - <Compile Include="ViewEngines\ITextResource.cs" />
  377 + <Compile Include="Localization\ITextResource.cs" />
378 378 <Compile Include="ViewEngines\IViewCache.cs" />
379 379 <Compile Include="ViewEngines\IViewEngine.cs" />
380 380 <Compile Include="ViewEngines\IViewLocationCache.cs" />
36 src/Nancy/ViewEngines/DefaultTextResource.cs
... ... @@ -1,36 +0,0 @@
1   -namespace Nancy.ViewEngines
2   -{
3   - using System;
4   - using System.Linq;
5   - using System.Reflection;
6   - using System.Resources;
7   -
8   - public class DefaultTextResource : ITextResource
9   - {
10   - private readonly Assembly culturedAssembly;
11   - private readonly ResourceManager resourceManager;
12   -
13   - public DefaultTextResource()
14   - {
15   - var assemblies = AppDomain.CurrentDomain.GetAssemblies();
16   -
17   - culturedAssembly = assemblies.FirstOrDefault(x => x.GetManifestResourceNames().Any(y => y.Contains(".Resources.Text")));
18   - if (culturedAssembly != null)
19   - {
20   - resourceManager = new ResourceManager(culturedAssembly.GetName().Name + ".Resources.Text",
21   - culturedAssembly);
22   - }
23   - }
24   -
25   - public string this[string key, NancyContext context]
26   - {
27   - get
28   - {
29   - if (resourceManager == null)
30   - return null;
31   -
32   - return resourceManager.GetString(key, context.Culture);
33   - }
34   - }
35   - }
36   -}
7 src/Nancy/ViewEngines/ITextResource.cs
... ... @@ -1,7 +0,0 @@
1   -namespace Nancy.ViewEngines
2   -{
3   - public interface ITextResource
4   - {
5   - string this[string key, NancyContext context] { get; }
6   - }
7   -}

0 comments on commit 8c1cd92

Please sign in to comment.
Something went wrong with that request. Please try again.