强制本地化图像或图像资源 [英] Force localize image or image assets

查看:161
本文介绍了强制本地化图像或图像资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中,如何强制使用NSLocalizedString特定语言

我可以通过此方法强制进行本地化。

I can force localise by this method.

@interface NSBundle (Language)
+(void)setLanguage:(NSString*)language;
@end



实施



Implementation

#import <objc/runtime.h>

static const char _bundle=0;

@interface BundleEx : NSBundle

@end

@implementation BundleEx
-(NSString*)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
    NSBundle* bundle=objc_getAssociatedObject(self, &_bundle);
    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super                            localizedStringForKey:key value:value table:tableName];
}

@end

@implementation NSBundle (Language)
+(void)setLanguage:(NSString*)language
{
  static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^
   {
      object_setClass([NSBundle mainBundle],[BundleEx class]);
   });
    objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:    [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil,  OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end

之后,我需要本地化图像和结果,我喜欢这个。
本地化资产目录

After that, I need to localise image and as a result, I do like this. Localize Asset Catalogs

问题是当我以编程方式更改本地化时,它永远不会更改图像。

Problem is when I change localisation programmatically, it never change image.

 [NSBundle setLanguage:@"en"];

只有我的字符串被更改。我该怎么办?我已尝试过像这样的资产中的普通图像和图像。但除非我真的在设置中更改手机语言,否则它不起作用。但我需要以编程方式更改/强制本地化,并且需要更改图像(不仅仅是文本)。我可以知道怎么办?

Only my string are changed. How shall I do? I have tried with both normal image and image in assets like this. But it doesn't work unless I really change my phone language in setting. But I need to change/force localisation programmatically and image need to be changed (not only text). May I know how to do?

推荐答案

我知道这是一个老问题,但仍在回答以供将来参考。

I know that is an old question but still answering for future reference.

确保在main.m中设置应用语言,它就像图像的魅力一样。

Make sure that you set your app language in the main.m, it works like a charm for the images.

OBS:不使用xcassets。

OBS : Not using xcassets.

像这样:

int main(int argc, char * argv[])
{
    @autoreleasepool
    {
        NSString *language = <Get Your Language from somewhere> // I user NSUserDefaults for that

        if (language == nil)
        {
            // Gets the device current language
            language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];
        }

        [NSBundle setLanguage:language];

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([YourAppDelegate class]));
    }
}

此外,我在BundleEx实现上有这个方法。本地化图像不需要它,但我需要它。例如,我有.html模板。

Additionally I have this method on the BundleEx implementation. It is not needed for the localized images but I needed it for .html templates I had, for instance.

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext
{
    NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);

    NSString *pathName = [bundle pathForResource:name ofType:ext];
    return pathName ?: [super pathForResource:name ofType:ext];
}

这篇关于强制本地化图像或图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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