Permalink
Browse files

Changes made as per @TheCodeJunkie PR comments

  • Loading branch information...
1 parent e116287 commit 8c1cd927f279da7dfeb91fd9a6c980ce646ae9fb @jchannon jchannon committed Dec 27, 2012
View
29 src/Nancy.Demo.Localization/Modules/HomeModule.cs
@@ -1,31 +1,20 @@
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
-using System.Threading;
-using System.Web;
-
-namespace Nancy.Demo.Localization
+namespace Nancy.Demo.Razor.Localization.Modules
{
+ using System.Globalization;
+
public class HomeModule : NancyModule
{
public HomeModule()
{
- Get["/"] = parameters =>
- {
- return View["Index"];
- };
+ Get["/"] = parameters => View["Index"];
- Get["/cultureview"] = parameters =>
- {
- return View["CultureView"];
- };
+ Get["/cultureview"] = parameters => View["CultureView"];
Get["/cultureviewgerman"] = parameters =>
- {
- this.Context.Culture = new CultureInfo("de-DE");
- return View["CultureView"];
- };
+ {
+ Context.Culture = new CultureInfo("de-DE");
+ return View["CultureView"];
+ };
}
}
}
View
4 ...calization/Nancy.Demo.Localization.csproj → ...tion/Nancy.Demo.Razor.Localization.csproj
@@ -11,8 +11,8 @@
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>Nancy.Demo.Localization</RootNamespace>
- <AssemblyName>Nancy.Demo.Localization</AssemblyName>
+ <RootNamespace>Nancy.Demo.Razor.Localization</RootNamespace>
+ <AssemblyName>Nancy.Demo.Razor.Localization</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
View
4 src/Nancy.Demo.Localization/Resources/Text.Designer.cs
@@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------
-namespace Nancy.Demo.Localization.Resources {
+namespace Nancy.Demo.Razor.Localization.Resources {
using System;
@@ -39,7 +39,7 @@ public class Text {
public static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
- global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Nancy.Demo.Localization.Resources.Text", typeof(Text).Assembly);
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Nancy.Demo.Razor.Localization.Resources.Text", typeof(Text).Assembly);
resourceMan = temp;
}
return resourceMan;
View
4 src/Nancy.Tests/Unit/ViewEngines/DefaultTextResourceFixture.cs
@@ -1,15 +1,15 @@
namespace Nancy.Tests.Unit.ViewEngines
{
- using Nancy.ViewEngines;
using Xunit;
+ using Nancy.Localization;
public class DefaultTextResourceFixture
{
[Fact]
public void Should_Return_Null_If_No_Assembly_Found()
{
//Given
- var defaultTextResource = new DefaultTextResource();
+ var defaultTextResource = new ResourceBasedTextResource();
var context = new NancyContext();
//When
View
7 src/Nancy.ViewEngines.Razor.Tests/RazorViewEngineFixture.cs
@@ -1,7 +1,4 @@
-using System.Threading;
-using Nancy.Culture;
-
-namespace Nancy.ViewEngines.Razor.Tests
+namespace Nancy.ViewEngines.Razor.Tests
{
using System;
using System.Dynamic;
@@ -12,6 +9,8 @@ namespace Nancy.ViewEngines.Razor.Tests
using Xunit;
using Nancy.Tests;
using Nancy.ViewEngines.Razor.Tests.Models;
+ using System.Threading;
+ using Nancy.Localization;
public class RazorViewEngineFixture
{
View
2 src/Nancy.ViewEngines.Razor/RazorViewEngine.cs
@@ -11,6 +11,7 @@
using System.Web.Razor.Parser.SyntaxTree;
using Nancy.Bootstrapper;
using Nancy.Responses;
+ using Nancy.Localization;
/// <summary>
/// View engine for rendering razor views.
@@ -36,6 +37,7 @@ public IEnumerable<string> Extensions
/// Initializes a new instance of the <see cref="RazorViewEngine"/> class.
/// </summary>
/// <param name="configuration">The <see cref="IRazorConfiguration"/> that should be used by the engine.</param>
+ /// <param name="textResource">The <see cref="ITextResource"/> that should be used by the engine.</param>
public RazorViewEngine(IRazorConfiguration configuration, ITextResource textResource)
{
this.viewRenderers = new List<IRazorViewRenderer>
View
8 src/Nancy.ViewEngines.Razor/TextResourceFinder.cs
@@ -1,6 +1,7 @@
namespace Nancy.ViewEngines.Razor
{
using System.Dynamic;
+ using Nancy.Localization;
/// <summary>
/// Returns text from an implemented ITextResource
@@ -10,6 +11,11 @@ public class TextResourceFinder : DynamicObject
private readonly ITextResource textResource;
private readonly NancyContext context;
+ /// <summary>
+ /// Initializes a new instance of the <see cref="TextResourceFinder"/> class.
+ /// </summary>
+ /// <param name="textResource">The <see cref="ITextResource"/> that should be used by the TextResourceFinder</param>
+ /// <param name="context">The <see cref="NancyContext"/> that should be used by the TextResourceFinder</param>
public TextResourceFinder(ITextResource textResource, NancyContext context)
{
this.textResource = textResource;
@@ -21,7 +27,7 @@ public TextResourceFinder(ITextResource textResource, NancyContext context)
/// </summary>
/// <param name="binder">GetMemberBinder with dynamic text key</param>
/// <param name="result">Text item</param>
- /// <returns></returns>
+ /// <returns>Returns a value or a non existing value from the <see cref="ITextResource"/> implementation</returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = this.textResource[binder.Name, this.context];
View
2 src/Nancy.sln
@@ -103,7 +103,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Authentication.S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Authentication.Stateless", "Nancy.Demo.Authentication.Stateless\Nancy.Demo.Authentication.Stateless.csproj", "{BAE74CD5-57C2-40E3-8F7A-EDE5721C2ACC}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Localization", "Nancy.Demo.Localization\Nancy.Demo.Localization.csproj", "{396F0BCE-5B51-4B6A-931E-312880C24725}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nancy.Demo.Razor.Localization", "Nancy.Demo.Localization\Nancy.Demo.Razor.Localization.csproj", "{396F0BCE-5B51-4B6A-931E-312880C24725}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
View
3 src/Nancy/Bootstrapper/NancyInternalConfiguration.cs
@@ -15,6 +15,7 @@ namespace Nancy.Bootstrapper
using Security;
using Nancy.Validation;
using Nancy.Culture;
+ using Nancy.Localization;
/// <summary>
/// Configuration class for Nancy's internals.
@@ -90,7 +91,7 @@ public static NancyInternalConfiguration Default
RouteSegmentExtractor = typeof(DefaultRouteSegmentExtractor),
RouteDescriptionProvider = typeof(DefaultRouteDescriptionProvider),
CultureService = typeof(DefaultCultureService),
- DefaultTextResource = typeof(DefaultTextResource),
+ DefaultTextResource = typeof(ResourceBasedTextResource),
};
}
}
View
10 src/Nancy/Localization/ITextResource.cs
@@ -0,0 +1,10 @@
+namespace Nancy.Localization
+{
+ /// <summary>
+ /// Used to return string values
+ /// </summary>
+ public interface ITextResource
+ {
+ string this[string key, NancyContext context] { get; }
+ }
+}
View
51 src/Nancy/Localization/ResourceBasedTextResource.cs
@@ -0,0 +1,51 @@
+namespace Nancy.Localization
+{
+ using System;
+ using System.Linq;
+ using System.Reflection;
+ using System.Resources;
+
+ /// <summary>
+ /// Resx implementation of ITextResource
+ /// </summary>
+ public class ResourceBasedTextResource : ITextResource
+ {
+ private readonly Assembly culturedAssembly;
+ private readonly ResourceManager resourceManager;
+
+ /// <summary>
+ /// Initializes a new instance of <see cref="ResourceBasedTextResource"/> to read strings from *.resx files
+ /// </summary>
+ /// <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>
+ public ResourceBasedTextResource()
+ {
+ var assemblies = AppDomain.CurrentDomain.GetAssemblies();
+
+ culturedAssembly = assemblies.FirstOrDefault(x => x.GetManifestResourceNames().Any(y => y.Contains(".Resources.Text")));
+ if (culturedAssembly != null)
+ {
+ resourceManager = new ResourceManager(culturedAssembly.GetName().Name + ".Resources.Text",
+ culturedAssembly);
+ }
+ }
+
+ /// <summary>
+ /// Used to return a string value from *.resx files
+ /// </summary>
+ /// <param name="key">The key to look for in the resource file</param>
+ /// <param name="context">The NancyContext used to determine the culture for returning culture specific values</param>
+ /// <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>
+ public string this[string key, NancyContext context]
+ {
+ get
+ {
+ if (resourceManager == null)
+ {
+ return null;
+ }
+
+ return resourceManager.GetString(key, context.Culture);
+ }
+ }
+ }
+}
View
4 src/Nancy/Nancy.csproj
@@ -271,7 +271,7 @@
<Compile Include="ViewEngines\DefaultRenderContextFactory.cs" />
<Compile Include="ViewEngines\DefaultResourceAssemblyProvider.cs" />
<Compile Include="ViewEngines\DefaultResourceReader.cs" />
- <Compile Include="ViewEngines\DefaultTextResource.cs" />
+ <Compile Include="Localization\ResourceBasedTextResource.cs" />
<Compile Include="ViewEngines\DefaultViewCache.cs" />
<Compile Include="ViewEngines\DefaultViewLocationCache.cs" />
<Compile Include="ViewEngines\DefaultViewRenderer.cs" />
@@ -374,7 +374,7 @@
<Compile Include="ViewEngines\IRenderContextFactory.cs" />
<Compile Include="ViewEngines\IResourceAssemblyProvider.cs" />
<Compile Include="ViewEngines\IResourceReader.cs" />
- <Compile Include="ViewEngines\ITextResource.cs" />
+ <Compile Include="Localization\ITextResource.cs" />
<Compile Include="ViewEngines\IViewCache.cs" />
<Compile Include="ViewEngines\IViewEngine.cs" />
<Compile Include="ViewEngines\IViewLocationCache.cs" />
View
36 src/Nancy/ViewEngines/DefaultTextResource.cs
@@ -1,36 +0,0 @@
-namespace Nancy.ViewEngines
-{
- using System;
- using System.Linq;
- using System.Reflection;
- using System.Resources;
-
- public class DefaultTextResource : ITextResource
- {
- private readonly Assembly culturedAssembly;
- private readonly ResourceManager resourceManager;
-
- public DefaultTextResource()
- {
- var assemblies = AppDomain.CurrentDomain.GetAssemblies();
-
- culturedAssembly = assemblies.FirstOrDefault(x => x.GetManifestResourceNames().Any(y => y.Contains(".Resources.Text")));
- if (culturedAssembly != null)
- {
- resourceManager = new ResourceManager(culturedAssembly.GetName().Name + ".Resources.Text",
- culturedAssembly);
- }
- }
-
- public string this[string key, NancyContext context]
- {
- get
- {
- if (resourceManager == null)
- return null;
-
- return resourceManager.GetString(key, context.Culture);
- }
- }
- }
-}
View
7 src/Nancy/ViewEngines/ITextResource.cs
@@ -1,7 +0,0 @@
-namespace Nancy.ViewEngines
-{
- public interface ITextResource
- {
- string this[string key, NancyContext context] { get; }
- }
-}

0 comments on commit 8c1cd92

Please sign in to comment.