In the previous article, we mentioned that our current project has been modularized with pod. When the project is split, the resource files and language files will also be split.
Because at first I thought it was the same as image resources, without special processing, it would eventually be merged into a car file. When splitting, a language file named Localizable.strings is created directly under each module. After disassembling a module, run found that a large number of translations were lost. After careful examination, it was found that the string files in the project were not merged as imagined, but only packaged into a string file, which was embarrassing because they were used before.
NSLocalizedString(@"login_fail",@"Logon failure");
This way of translation, there is no specified string file name, if the number of changes is relatively large. It's time to sacrifice Method swizzing to solve this problem.
First step: Check the corresponding method for the macro definition of NSLocalizedString
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
Where to use it, will it affect the language problem of third-party libraries after exchange? After checking, it was found that the third-party libraries used in the project were all bundle d with their own resource files, which would not be used to load language files. Method swizzing is safe to use.
The second step is to rename the file just removed and recompile it. After decompression, it is found that there are indeed two string files in the app. This step was also successful.
The third step is to exchange methods. Code:
#import "NSBundle+ZATool.h"
#import <objc/runtime.h>
@implementation NSBundle (ZATool)
+(void)load
{
[super load];
//Systematic methods that need to be exchanged, class methods and object methods use different methods
Method fromMethod = class_getInstanceMethod([self class], @selector(localizedStringForKey:value:table:));
//The method of writing by oneself that needs to be exchanged
Method toMethod = class_getInstanceMethod([self class], @selector(ZALocalizedStringForKey:value:table:));
//Method Exchange
method_exchangeImplementations(fromMethod, toMethod);
}
#pragma mark-implementation of multilingual method
-(NSString *)ZALocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
//Language File Array
NSArray *array=@[@"CommonLocalizable",@"Localizable"];
if (!tableName || [array containsObject:tableName]) {
if (!tableName) {
tableName=@"CommonLocalizable";
}
//Note: Method exchange is mutual exchange, which is equivalent to the systematic translation method of tone.
NSString *string= [self ZALocalizedStringForKey:key value:value table:tableName];
if ([string isEqualToString:key]) {
//When the translation string cannot be retrieved, the recursive call finds it from the next translation file
NSInteger tag=[array indexOfObject:tableName];
if (tag!=array.count-1) {
tableName=array[tag+1];
//Note: Method exchange is mutual exchange, which is equivalent to our own translation method of tone.
string= [self localizedStringForKey:key value:value table:tableName];
}
}else{
}
return string;
}else
{
return [self ZALocalizedStringForKey:key value:value table:tableName];
}
}