Permalink
Browse files

Updated ResourceBasedTextResoure

Changed so that it scanns for all resources and let you
index them using a key that has the format of
resourcefilename.resourcekey where the resourcefilename
is the name of the resx and resourcekey is the name of the
item in the resx
  • Loading branch information...
1 parent c3f0bc5 commit 2f64b2c4a7990eb95dd5ae63bd1a276a46b15b66 @thecodejunkie thecodejunkie committed Jan 1, 2013
Showing with 43 additions and 18 deletions.
  1. +43 −18 src/Nancy/Localization/ResourceBasedTextResource.cs
@@ -1,50 +1,75 @@
namespace Nancy.Localization
{
using System;
+ using System.Collections.Generic;
using System.Linq;
- using System.Reflection;
using System.Resources;
- using ViewEngines;
/// <summary>
/// Resource based implementation of <see cref="ITextResource"/>
/// </summary>
public class ResourceBasedTextResource : ITextResource
{
private readonly IAssemblyProvider assemblyProvider;
- private readonly Assembly culturedAssembly;
- private readonly ResourceManager resourceManager;
+ private readonly IDictionary<string, ResourceManager> resourceManagers;
/// <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(IAssemblyProvider assemblyProvider)
{
this.assemblyProvider = assemblyProvider;
- this.culturedAssembly = this.assemblyProvider
- .GetAssembliesToScan()
- .FirstOrDefault(x => x.GetManifestResourceNames().Any(y => y.Contains(".Resources.Text")));
+ var resources =
+ from assembly in this.assemblyProvider.GetAssembliesToScan()
+ from resourceName in assembly.GetManifestResourceNames()
+ where resourceName.EndsWith(".resources")
+ let parts = resourceName.Split(new[] { '.' })
+ let name = parts[parts.Length - 2]
+ let baseName = resourceName.Replace(".resources", string.Empty)
+ select new
+ {
+ Name = name,
+ Manager = new ResourceManager(baseName, assembly)
+ };
- if (this.culturedAssembly != null)
- {
- var baseName =
- string.Concat(culturedAssembly.GetName().Name, ".Resources.Text");
-
- this.resourceManager = new ResourceManager(baseName, culturedAssembly);
- }
+ this.resourceManagers =
+ resources.ToDictionary(x => x.Name, x => x.Manager, StringComparer.OrdinalIgnoreCase);
}
/// <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>
+ /// <param name="context">The <see cref="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 { return resourceManager == null ? null : resourceManager.GetString(key, context.Culture); }
+ get
+ {
+ var components =
+ GetKeyComponents(key);
+
+ var manager =
+ this.resourceManagers[components.Item1];
+
+ return (manager == null) ? null : manager.GetString(components.Item2, context.Culture);
+ }
+ }
+
+ private static Tuple<string, string> GetKeyComponents(string key)
+ {
+ var index =
+ key.IndexOf(".", StringComparison.InvariantCulture);
+
+ if (index == -1)
+ {
+ throw new InvalidOperationException("The text key needs to be specified in the format resourcename.resourcekey.");
+ }
+
+ return new Tuple<string, string>(
+ key.Substring(0, index),
+ key.Substring(index + 1));
}
}
}

0 comments on commit 2f64b2c

Please sign in to comment.