我正在开发一个需要本地化和国际化的 Web 应用程序。我突然想到我可以使用依赖注入(inject)框架来做到这一点。假设我声明了一个接口(interface) ILocalResources(在本示例中使用 C#,但这并不重要):
interface ILocalResources {
public string OkString { get; }
public string CancelString { get; }
public string WrongPasswordString { get; }
...
}
并创建此接口(interface)的实现,为我需要支持的每种语言创建一个。然后,我将设置我的 DI 框架以静态或动态地实例化正确的实现(例如,基于请求浏览器的首选语言)。
有什么理由我不应该为这类事情使用 DI 框架吗?我发现自己唯一的反对意见是它可能有点矫枉过正,但如果我在我的网络应用程序中使用 DI 框架,我还不如将它用于国际化?
最佳答案
构建 DI 框架是为了进行依赖注入(inject),而本地化可能只是您的服务之一,因此在这种情况下,没有理由不使用 DI 框架 IMO。也许我们应该开始讨论提供的 ILocalResources
界面。虽然我支持编译时支持,但我不确定提供的接口(interface)是否会对您有所帮助,因为该接口(interface)可能是您系统中变化最大的类型。并使用该接口(interface)实现它的类型/类型。也许你应该采用不同的设计。
当我们查看大多数本地化框架/提供商/工厂(或其他)时,它们都是基于字符串的。因此,请考虑以下设计:
public interface ILocalResources
{
string GetStringResource(string key);
string GetStringResource(string key, CultureInfo culture);
}
这将允许您将键和文化添加到底层消息数据存储中,而无需更改界面。缺点当然是您永远不应该更改 key ,因为那可能是 hell 。
另一种方法可能是抽象基类型:
public abstract class LocalResources
{
public string OkMessage { get { return this.GetString("OK"); } }
public string CancelMessage { get { return this.GetString("Cancel"); } }
...
protected abstract string GetStringResource(string key,
CultureInfo culture);
private string GetString(string key)
{
Culture culture = CultureInfo.CurrentCulture;
string resource = GetStringResource(key, culture);
// When the resource is not found, fall back to the neutral culture.
while (resource == null && culture != CultureInfo.InvariantCulture)
{
culture = culture.Parent;
resource = this.GetStringResource(key, culture);
}
if (resource == null) throw new KeyNotFoundException(key);
return resource;
}
}
这种类型的实现可能如下所示:
public sealed class SqlLocalResources : LocalResources
{
protected override string GetStringResource(string key,
CultureInfo culture)
{
using (var db = new LocalResourcesContext())
{
return (
from resource in db.StringResources
where resource.Culture == culture.Name
where resource.Key == key
select resource.Value).FirstOrDefault();
}
}
}
这种方法两全其美,因为 key 不会分散在应用程序中,并且只需在一个地方完成添加新属性。使用您的 favorite DI 库,您可以像这样注册一个实现:
container.RegisterSingleton<LocalResources>(new SqlLocalResources());
自从
LocalResources
type 有一个抽象方法来完成所有工作,很容易创建一个装饰器来添加缓存以防止从数据库请求相同的数据:public sealed class CachedLocalResources : LocalResources
{
private readonly Dictionary<CultureInfo, Dictionary<string, string>> cache =
new Dictionary<CultureInfo, Dictionary<string, string>>();
private readonly LocalResources decoratee;
public CachedLocalResources(LocalResources decoratee) { this.decoratee = decoratee; }
protected override string GetStringResource(string key, CultureInfo culture) {
lock (this.cache) {
string res;
var cultureCache = this.GetCultureCache(culture);
if (!cultureCache.TryGetValue(key, out res)) {
cultureCache[key] = res= this.decoratee.GetStringResource(key, culture);
}
return res;
}
}
private Dictionary<string, string> GetCultureCache(CultureInfo culture) {
Dictionary<string, string> cultureCache;
if (!this.cache.TryGetValue(culture, out cultureCache)) {
this.cache[culture] = cultureCache = new Dictionary<string, string>();
}
return cultureCache;
}
}
您可以按如下方式应用装饰器:
container.RegisterSingleton<LocalResources>(
new CachedLocalResources(new SqlLocalResources()));
请注意,这个装饰器会无限期地缓存字符串资源,这可能会导致内存泄漏,因此您希望将字符串包装在
WeakReference
中。实例或有某种过期超时。但这个想法是,您可以应用缓存而无需更改任何现有的实现。我希望这有帮助。
关于language-agnostic - 使用 DI 框架进行本地化 - 好主意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4646848/