//
// main.m
// 移除无用词条
//
// Created by user on 2021/12/24.
//
#import <Foundation/Foundation.h>
void findFilePath(NSMutableArray *filePath, NSString *path) {
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *directroyArray = [fm contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
for (NSString *fileName in directroyArray) {
BOOL isDirectroy = NO;
NSString *fullPath = [path stringByAppendingPathComponent:fileName];
if ([fm fileExistsAtPath:fullPath isDirectory:&isDirectroy]) {
if (isDirectroy) {
if ([fileName isEqualToString:@"Pods"]) {
continue;
}
findFilePath(filePath, fullPath);
} else {
if ([fullPath hasSuffix:@".swift"] || [fullPath hasSuffix:@".m"]) {
[filePath addObject:fullPath];
}
}
}
}
}
void lcy_searchFileWithKeys(NSMutableArray * allKeys, NSMutableArray * allContents, NSMutableArray *usedKeys, NSMutableArray *usedContents, NSMutableArray *filesPath) {
for (NSString *path in filesPath) {
NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [content componentsSeparatedByString:@"\n"];
for (NSString *line in lines) {
for (NSInteger i = 0; i < allKeys.count; i ++) {
NSString *key = allKeys[i];
NSString *content = allContents[i];
if ([line containsString:key]) {
[allKeys removeObjectAtIndex:i];
[allContents removeObjectAtIndex:i];
[usedKeys addObject:key];
[usedContents addObject:content];
break;
}
}
}
}
}
void findUnuseKey(void) {
NSString *filePath = @"/Users/.../translations/en.lproj/Localizable.strings";
NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
// 获取key
NSArray *allLines = [content componentsSeparatedByString:@"\n"];
NSMutableArray *allKeys = [NSMutableArray array];
NSMutableArray *allContents = [NSMutableArray array];
for (NSString *line in allLines) {
NSRange range = [line rangeOfString:@"\" = \""];
if (range.location == NSNotFound) {
continue;
}
NSString *first = [line componentsSeparatedByString:@"\" = \""].firstObject;
NSString *last = [line componentsSeparatedByString:@"\" = \""].lastObject;
[allKeys addObject:[NSString stringWithFormat:@"%@\"", first]];
[allContents addObject:[last substringToIndex:last.length - 2]];
}
NSMutableArray *filesPath = [NSMutableArray array];
// 获取所有文件路径
NSString *projectRoot = @"项目路径xcodeproj所在文件夹位置";
findFilePath(filesPath, projectRoot);
NSMutableArray *usedKeys = [NSMutableArray array];
NSMutableArray *usedContents = [NSMutableArray array];
// 感觉不太准,多调两遍
lcy_searchFileWithKeys(allKeys, allContents, usedKeys, usedContents, filesPath);
lcy_searchFileWithKeys(allKeys, allContents, usedKeys, usedContents, filesPath);
// 未使用的
NSMutableString *unusedKeyString = [NSMutableString string];
for (NSInteger i = 0; i < allKeys.count; i ++) {
NSString *key = allKeys[i];
NSString *content = allContents[i];
unusedKeyString = [[NSString stringWithFormat:@"%@\n\n%@ = \"%@\"", unusedKeyString, key, content] mutableCopy];
}
[unusedKeyString writeToFile:@"/Users/name/Desktop/haha/languageUnused.txt" atomically:YES];
// 用到的
NSMutableString *finalString = [NSMutableString string];
for (NSInteger i = 0; i < usedKeys.count; i ++) {
NSString *key = usedKeys[i];
NSString *content = usedContents[i];
finalString = [[NSString stringWithFormat:@"%@\n\n%@ = \"%@\"", finalString, key, content] mutableCopy];
}
[finalString writeToFile:@"/Users/name/Desktop/haha/language.txt" atomically:YES];
NSLog(@"%@", finalString);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 找到无用的key
findUnuseKey();
}
return 0;
}
iOS多语言移除无用词条
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1、下载脚本。2、在任何地方创建新文件夹,并将脚本放入文件夹内,英文命名不含空格。3、在终端cd进入文件夹给脚本上...
- html多语言的方法,百度即可获得,这里不多说了。iOS下,ajax不能用,可以看这个https://ask.dc...
- 因为新项目需要用到国际化语言包,自己就在网上借鉴了大神们的经验,自己写了一个swift版的方法,不足之处,还希望体...
- 多语言(https://www.jianshu.com/p/5f6a5d074fde[https://www.ji...