ios 如何本地化取消、完成等?

3zwtqj6y  于 2023-05-19  发布在  iOS
关注(0)|答案(8)|浏览(191)

UIBarButtonItem有类似Cancel、Done和其他一些标识符。它们以文本形式显示给用户。如果用户更改语言,则例如“取消”按钮将自动翻译。作为开发人员,您不需要为此按钮提供本地化字符串。这意味着取消,完成和其他字符串已经本地化,并与操作系统一起。
这里有一种方法可以通过编程方式获得这个字符串吗?
我不想向本地化文件添加其他字符串。如果可以访问,那就太好了。

xytpbqjk

xytpbqjk1#

下面是我创建的一个小宏,用于获取系统UIKit字符串:

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

这样使用它:

UIKitLocalizedString(@"Search");
UIKitLocalizedString(@"Done");
UIKitLocalizedString(@"Cancel");
...
trnvg8h3

trnvg8h32#

一种(公认的有问题的)轻松实现这一点的方法是直接使用Apple的框架包本地化:
要查看他们提供的内容,请通过Finder打开以下目录:
/Applications/Xcode/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks
在本例中,随后将打开./UIKit.framework/English.lproj/Localizable.strings(在TextMate中)。在这里,你可以看到苹果公司使用的各种各样的翻译,如“打印”,“确定”,“开”,“关”等。真实的神奇的是,有大约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";
  • 警告:* 我绝不建议您在打算提交到App Store的应用程序中以编程方式访问这些本地化资源。我只是举例说明了一个特定的实现,我看到它解决了你最初的问题。
w46czmvw

w46czmvw3#

鼓励Answer to this Question(通过Stephan Heilner)和Answer(通过bdunagan)用于iPhone/iOS: How can I get a list of localized strings in all the languages my app is localized in?
Objective-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

Swift 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
    }
}

使用方法:

"Photo Library".localized(.UIKit)

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

Bundle.UIKit.localizableStrings?.keys//.map { $0 }
vbopmzt1

vbopmzt14#

虽然这可能不是你想要的,但Mac App Store中有一个名为“System Strings”的商业应用程序,声称它提供了超过53000个标准本地化字符串的集合。它于2012年11月2日发布。我与这个应用程序或作者没有任何关系。URL为https://itunes.apple.com/us/app/systemstrings/id570467776

lymnna71

lymnna715#

听起来你想问的是苹果是否提供了一种访问预翻译字符串字典的方法。我认为苹果为这样的事情提供的任何东西都将位于他们的文档中:国际化编程主题
为了回答你的问题,我不相信他们提供了一个已知翻译的字典/列表。要么你必须在你的Localizable.strings资源文件中定义它们,要么像其他人在评论中所说的那样,从UIBarButtonItem中提取标题(我个人会使用资源文件)。

ljsrvy3e

ljsrvy3e6#

为什么不对你的故事板使用基础本地化?它将为您本地化。

mqkwyuun

mqkwyuun7#

iOS 16中的工作版本。以前的那些似乎不再起作用了

+(NSString*) uiKitLocalizedStringForKey:(NSString*)key {
    NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
    if (!uikitBundle) {
        // Never seen this happen, but might as well guard for it
        return key;
    }
    return [uikitBundle localizedStringForKey:key value:key table:nil];
}
t9eec4r0

t9eec4r08#

你可以用

NSLocalizedString(@"Cancel", @"Cancel")

相关问题