有没有办法使NSBundle本地化缓存无效,重启应用程序? [iOS的] [英] Is there a way to invalidate NSBundle localization cache, withour restarting application? [iOS]

查看:207
本文介绍了有没有办法使NSBundle本地化缓存无效,重启应用程序? [iOS的]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们可以在运行时更改Localizable.strings,它放在NSBundle
中当前,即使我们更改了它的内容, NSLocalizedString 也会返回旧的(缓存)价值。

Let's assume that we can change in runtime Localizable.strings, that is placed in NSBundle At the current moment, even if we change it's contents, NSLocalizedString would return old(cached) values.


  1. 运行应用程序

  2. 获取特定key1的LocalizableString< - value1

  3. 更改Localizable.strings key1 = value2

  4. < - 在应用程序中执行某些操作以使本地化缓存无效 - >

  5. 检查LocalizableString是否为特定key1 == value2

  1. Run Application
  2. Get LocalizableString for specific key1 <- value1
  3. Change Localizable.strings key1 = value2
  4. <-- Do something in application to invalidate Localization cache -->
  5. Check if LocalizableString for specific key1 == value2

我已经尝试过:


  • [[NSBundble mainBundle] invalidateResourceCache]

  • [UIApplication _performMemoryWarning]

  • 试图看看,如果有一些词典。用于缓存,在NSBundle的ivars中。

  • 尝试在GNUStep中实现NSBundle,但它与我们在ios 6.0中的不同

  • [[NSBundble mainBundle] invalidateResourceCache]
  • [UIApplication _performMemoryWarning]
  • Tried to see, if there's some dictionaries. used for caching, in ivars in NSBundle.
  • Tried to see, in GNUStep implementation of NSBundle, but it's different from that we have in ios 6.0

我不能做什么(根据定义):
- 我不能调酒[NSBundle localizableStringForKey:value:table]
- 我无法更改宏
- 一般来说,我不能影响任何原始项目代码,仅在步骤#4中添加内容

What I cannot do (by definition): - I cannot swizzle [NSBundle localizableStringForKey:value:table] - I cannot change macroses - In general, I cannot affect Any Original Project code, only add something at step #4

这仅用于开发目的。所以,我不需要在AppStore中发布它,所以任何私有方法或解决方案都可以。

This is only for development purposes only. So, I don't need to publish it in AppStore or something, so any private methods, or solutions are OK.

所以问题是。可能有人知道这样做的方法,或者是谁给我另一个想法怎么做呢?谢谢。

So, the question is. May be someone know the way to do it, or someone who give me another ideas how to do it? Thank you.

推荐答案


注意:此解决方案使用私有API和您向App Store提交的应用程序如果您使用此代码,我们将被拒绝。

NOTE: This solution uses private APIs and your app submissions to the App Store will be rejected if you use this code.

因此,经过一些搜索,我找到了帮助我的链接

So, after some search I've found link that helped me

如何删除NSBundle缓存

// First, we declare the function. Making it weak-linked 
// ensures the preference pane won't crash if the function 
// is removed from in a future version of Mac OS X.
extern void _CFBundleFlushBundleCaches(CFBundleRef bundle) 
  __attribute__((weak_import));

BOOL FlushBundleCache(NSBundle *prefBundle) {
    // Before calling the function, we need to check if it exists
    // since it was weak-linked.
    if (_CFBundleFlushBundleCaches != NULL) {
        NSLog(@"Flushing bundle cache with _CFBundleFlushBundleCaches");
        CFBundleRef cfBundle =
           CFBundleCreate(nil, (CFURLRef)[prefBundle bundleURL]);
        _CFBundleFlushBundleCaches(cfBundle);
        CFRelease(cfBundle);
        return YES; // Success
    }
    return NO; // Not available
}

刷新捆绑缓存后,使用新的本地化密钥。
所以现在我不需要在模拟器中重启我的应用程序,以便查看可本地化字符串的变化。

After flushing bundle cache, new localizations keys are used. So now I don't need to restart my application in simulator in order to see changes in localizable strings.

这篇关于有没有办法使NSBundle本地化缓存无效,重启应用程序? [iOS的]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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