如何获得本地化取消,完成等?

Ram*_*mis 43 localization objective-c ios

UIBarButtonItem具有取消,完成和其他一些标识符.它们显示为用户的文本.如果用户更改语言,则例如"取消"按钮将自动翻译.作为开发人员,您不需要为此按钮提供本地化字符串.这意味着取消,完成和其他字符串已经本地化并与OS结合在一起.

这是一种以编程方式获取此字符串的方法吗?

我不想在本地化文件中添加其他字符串.如果可以访问那么它会非常好.

Ste*_*ner 25

这是我为了获得System UIKit字符串而创建的一个小宏:

#define UIKitLocalizedString(key) [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] localizedStringForKey:key value:@"" table:nil]
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

UIKitLocalizedString(@"Search");
UIKitLocalizedString(@"Done");
UIKitLocalizedString(@"Cancel");
...
Run Code Online (Sandbox Code Playgroud)

  • 做一个没有大气味的正常功能 (10认同)
  • 这不再起作用了,对吧?UIKit 捆绑包不再包含 Localized.string 文件。事实上,在搜索运行时模拟器文件后,我什至根本无法识别这些字符串所在的位置。 (4认同)
  • 有什么办法可以得到所有的钥匙吗? (2认同)

Gre*_*mbs 10

一个(可以肯定的)可以轻松实现这一点的方法是直接使用Apple的框架包本地化:

要查看它们提供的内容,请通过Finder打开以下目录:

/Applications/Xcode/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks

在您的情况下,您将随后打开./UIKit.framework/English.lproj/Localizable.strings(在TextMate中).在这里,您可以看到Apple用于"打印","确定","开启","关闭"等各种翻译.真正的神奇之处在于,您可以将大约35种不同的语言翻译复制到您的拥有Localizable.strings文件,免费.

如果您非常无耻,并且不介意将应用程序的未来稳定性置于问题中,那么您可以跳过整个"复制到您自己的Localizable.strings"过程并以编程方式直接进入源代码:

NSBundle *uiKitBundle = [NSBundle bundleWithIdentifier:@"com.apple.UIKit"];
NSString *onText = uiKitBundle ? [uiKitBundle localizedStringForKey:@"Yes" value:nil table:nil] : @"YES";
NSString *offText = uiKitBundle ? [uiKitBundle localizedStringForKey:@"No" value:nil table:nil] : @"NO";
Run Code Online (Sandbox Code Playgroud)

警告:我绝不建议您在要提交到App Store的应用程序中以编程方式实际访问这些本地化资源.我只是说明了一个我见过的特定实现,它解决了你原来的问题.


Sta*_* P. 6

受到此问题回答(由Stephan Heilner 提供)和对iPhone/iOS 的回答(由bdunagan 提供)的鼓励:如何获取我的应用程序本地化的所有语言的本地化字符串列表?

目标-C

NSString * LocalizedString(NSString *key) {
    return [[NSBundle mainBundle] localizedStringForKey:key];
}

@interface NSBundle(Localization)
- (NSString *)localizedStringForKey:(NSString *)key;
- (NSDictionary<NSString *, NSString *> *)localizationTable;
@end

@implementation NSBundle(Localization)
- (NSDictionary<NSString *, NSString *> *)localizationTable {
    NSString *path = [self pathForResource:@"Localizable" ofType:@"strings"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    id obj = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
    if (error && obj == nil) {
        @throw error;
        return nil;
    }
    if ([obj isKindOfClass:NSDictionary.class]) {
        return obj;
    }    
    @throw NSInternalInconsistencyException;
    return nil;
}

- (NSString *)localizedStringForKey:(NSString *)key {
    return [self localizedStringForKey:key value:nil table:nil];
}
@end
Run Code Online (Sandbox Code Playgroud)

斯威夫特 5

public extension String {
    func localized(_ bundle: Bundle = .main) -> String {
        bundle.localize(self)
    }        
    var localized: String {
        return localized()
    }
}

extension Bundle {
    static var UIKit: Bundle {
        Self(for: UIApplication.self)
    }
    func localize(_ key: String, table: String? = nil) -> String {
        self.localizedString(forKey: key, value: nil, table: nil)
    }
    var localizableStrings: [String: String]? {
        guard let fileURL = url(forResource: "Localizable", withExtension: "strings") else {
            return nil
        }
        do {
            let data = try Data(contentsOf: fileURL)
            let plist = try PropertyListSerialization.propertyList(from: data, format: .none)
            return plist as? [String: String]
        } catch {
            print(error)
        }
        return nil
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

"Photo Library".localized(.UIKit)
Run Code Online (Sandbox Code Playgroud)

获取 UIKit 本地化字符串的所有键:

Bundle.UIKit.localizableStrings?.keys//.map { $0 }
Run Code Online (Sandbox Code Playgroud)