Localizing the Cut|Copy|Paste menu on iOS

2019-01-07 19:35发布

Im having some issues localizing a danish app ive made. (The language, not the pastry)

I have set the CFBundleDevelopmentRegion to da_DK for danish in my info.plist, but the popup appearing for text input is still in english, even on phones running the danish OS.

enter image description here

How in Jobs name can i change this ?

The test device is a non-jailbroken iPhone 4S running iOS 5.1 with Danish as its iOS setting, and a danish itunes account associated.

I do not use .xibs for designs. all interfaces are programmed as viewcontrollers.

6条回答
做自己的国王
2楼-- · 2019-01-07 19:52

If you can't get it working the official way, as provided by @vikingosegundo, you can do this with some creative engineering (Creative as in, oh my god that is dangerous). I discovered this method when I accidentally overrode [NSBundle localizedStringForKey:value:tableName:].

1) Add a category to NSBundle with the following methods:

#import <objc/runtime.h>

+ (void) load  {
    Method original, swizzled;

    original = class_getInstanceMethod(self, @selector(localizedStringForKey:value:table:));
    swizzled = class_getInstanceMethod(self, @selector(swizzled_localizedStringForKey:value:table:));
    method_exchangeImplementations(original, swizzled);
}

- (NSString*) swizzled_localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {

    NSLog(@"Key: %@. Value: %@", key, value);

    return [self swizzled_localizedStringForKey: key value:value table:tableName];
}

2) Where I simply log the key/value, you want to put an if ([key isEqualToString: xxx] ) block. In there, you want to catch (at least some of) the following key values: Cut, Copy[Menu], Select, Select All, Paste, Delete[Menu], Replace..., Define, Speak, Pause. These are the default values that can appear there.

3) When you have caught the value you can look up in a custom table or use hardcoded values. If you look up in a custom table make sure you have a catch in your swizzled method to avoid infinite looping in your custom table.

NB: Why do you need to swizzle? Because this over-rides all Apple text for you app. You will still want the defaults for all the other strings, so you need to swizzle to get the defaults for the strings you aren't interested in.

Good luck. Paul

查看更多
我只想做你的唯一
3楼-- · 2019-01-07 19:55

You must localize your app in Danish to make the standard UI elements appear in that language. This is to avoid having a UI with mixed languages.

If you don't use xibs, you'd usually do this by adding a Localizable.strings file to your project. In Xcode's "Add File" dialog, you can use the "Strings File" template (under "Resources") for this.

To actually localize the strings file, open the file inspector ( 1) and click the + button in the "Localization" section. You'll end up with the file being displayed as a group in the project navigator, with a sub-entry for each language.

The strings file has the format:

"Label_Text" = "Smørrebrød";

(don't forget the semicolon)

To use localized strings in your code, you can use the NSLocalizedString macro like this:

myLabel.text = NSLocalizedString(@"Label_Text", nil);

(The second parameter is for a comment. This can be useful if you use the genstrings tool to extract localizable strings from your code and give the resulting file to a professional translator.)

If you use the English strings as keys, you can leave the English version of Localizable.strings empty (but don't delete it).

Having a Localizable.strings file in the language that the user has selected will also cause standard UI elements, such as the editing menu, photo picker, and so forth, to appear in that language.

查看更多
再贱就再见
4楼-- · 2019-01-07 19:57

In the Xcode's file tree (Project Navigator) select your project. in the right hand pane select your project again. select Info and add your language.

configure i18n


I created a sample project, this is the result:

enter image description here

查看更多
劫难
5楼-- · 2019-01-07 19:58

Search if your .xib is localized (you'll find it in the inspector on the right panel) if so go to your Project/Target-Settings press the +-Sign and select "Duplicate English to Danish" or something which means the same (I can't check the right item at the moment)

Btw it's called iPhone 4S.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-07 19:59

You can do this directly in the info.plist. Something like this:

<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleLocalizations</key>
<array>
  <string>en</string>
  <string>de</string>
  <string>es</string>
  <string>ja</string>
</array>    
查看更多
走好不送
7楼-- · 2019-01-07 20:04

Try adding/setting the "Localized resources can be mixed" flag in Info.plist to YES.

查看更多
登录 后发表回答