优先覆盖首选字符串本地化以进行测试 [英] Overriding preferred strings localization on the fly for testing

查看:109
本文介绍了优先覆盖首选字符串本地化以进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iPhone模拟器中的设置应用程序切换语言是一种测试本地化调整的PITA方式。我试图找出一种方法来在我的应用程序中使用调试设置即时切换本地化(en / fr / es / etc),而不重新启动应用程序。

Using the Settings app in the iPhone simulator to switch languages is a PITA way of testing localization tweaks. I'm trying to figure out a way to switch localizations (en/fr/es/etc) on the fly in my app with a debug setting without restarting the app.

NSBundle提供从任意本地化请求本地化资源的方法,例如

NSBundle provides ways of requesting localized resources from an arbitrary localization, e.g.

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName

但我发现没有等效的字符串管理子系统。它来自这些 问题,您可以覆盖注册的默认值,这听起来愚蠢,需要重新启动应用程序。

But I find no equivalent for the string management subsystem. It sounds from these questions that you can override the registered defaults, which sounds goofy and requires an app restart.

这里有没有其他策略?如果我把NSLocalizedString与调试版本中的东西做了自己的工作,加载给定的 .strings 文件的最短路径是什么?有没有编程方式访问这些字符串表,而不去解析它们自己生成?

Are there any other strategies here? If I wrapped NSLocalizedString with something that in debug builds did its own work, what's the shortest path to loading a given .strings file? Is there a programmatic way to access those strings tables without going and parsing them raw myself?

谢谢。

推荐答案

我发现的唯一方法是使用此答案。你可以在知道本地化覆盖的函数中包装 NSLocalizedString(),并根据是否设置来选择如何获取它的字符串。当要覆盖时,可以从本地化目录创建一个子包,然后从该包中拉出字符串。这是它的要点:

The only way to do this that I've figured out is by using the subtle trick suggested by this answer. You can wrap NSLocalizedString() in a function that knows about a localization "override", and chooses how to get its strings based on whether that is set. When you want to override, you can create a "sub bundle" from the localization's directory, and then pull string from that bundle. Here's the gist of it:

if (CurrentLocalization != nil) {
    NSBundle * locBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:CurrentLocalization ofType:@"lproj"]];
    return [locBundle localizedStringForKey:key value:nil table:nil];
} else {
    return NSLocalizedString(key, @"");
}    

等。

这篇关于优先覆盖首选字符串本地化以进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆