This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 2f64b2c
Showing
1 changed file
with
43 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |