/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
namespace Everest.Library.Resource{
/// <summary>
///
/// </summary>
public static class ResourceWrapper
{
const string DefaultResourceBaseName = "Resources";
static string DefaultResourceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "localize");
static IDictionary<string, ResourceManager> resourceManagers = new Dictionary<string, ResourceManager>();
/// <summary>
/// Gets the string.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="assemblyName">Name of the assembly.</param>
/// <returns></returns>
public static string GetString(string name, string assemblyName)
{
return GetString(name, null);
}
/// <summary>
/// Gets the string.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="assemblyName">Name of the assembly.</param>
/// <param name="culture">The culture.</param>
/// <returns></returns>
public static string GetString(string name, string assemblyName, CultureInfo culture)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (culture == null)
{
culture = CultureInfo.CurrentUICulture;
}
ResourceManager resourceManager = GetResourceManager(assemblyName);
return resourceManager.GetString(name, culture);
}
public static ResourceManager GetResourceManager(string assemblyName)
{
ResourceManager resourceManager = null;
if (resourceManagers.ContainsKey(assemblyName))
{
resourceManager = resourceManagers[assemblyName];
return resourceManager;
}
if (resourceManager == null)
{
lock (resourceManagers)
{
if (!resourceManagers.ContainsKey(assemblyName))
{
Assembly assembly = ResolveAssembly(assemblyName);
resourceManager = new EverestResourceManager(assemblyName + "." + DefaultResourceBaseName, assembly, DefaultResourceDir);
resourceManagers.Add(assemblyName, resourceManager);
}
}
}
return resourceManager;
}
public static Assembly ResolveAssembly(string assemblyName)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
if (assembly.GlobalAssemblyCache == false && assembly.FullName.ToLower().Contains(assemblyName.ToLower() + ","))
{
return assembly;
}
}
return null;
}
}
}
|