简体   繁体   中英

UiTextView localized with xib strings file

I use base internationalization in xCode to manage multiple languages in my app.

In a xib file I've got this object :

Xrj-9E-2VK it's an UITextView

In the corresponding strings file :

"Xrj-9E-2VK.text" = "text translated in french"

But my text is still in English.

Any Suggestion ?

I've found the following workaround until Apple fixes this (serious) bug that still exists in iOS 7 :

You actually need to 'mix' the 'base localisation method' (= preferred method for Xcode 5 & iOS 7) with the 'older' method of using the 'Localizable.strings' file

1) create the file 'Localizable.strings' (File -> New -> iOS -> resource -> .strings file)

2) use Xcode to create localised versions of this file for each language you use (select the file 'Localizable.strings', in File Inspector under 'Localization' click on the selection button next to each language you use).

3) in your viewController create an IBOutlet @property for the UITextField :

@property (weak, nonatomic) IBOutlet UITextView *localizedTextView;

4) under 'viewDidLoad' add the following code :

self.localizedTextView.text = NSLocalizedString(@"textFieldKey", @"comment for the translator");

5) in each 'Localizable.strings' file add :

"textFieldKey" = "the translated text you want to be put in the textField";

That should do it !

Keep in mind that Apple will probably fix this bug somewhere in the near future, in that case the translation will be taken from the 'base localised storyboard' (the one with the object references in, like "a8N-K9-eJb.text" = "some translated text". In that case you can delete the 'Localization.strings' file, and use base localisation again for a UITextField

This still appears under Xcode 6 & IOS 8, and the previous answers don't satisfy me.

Here's my simple and quick workaround :

  1. Select the XIB and then in the file inspector
  2. Below "Localization", find the language that does not work
  3. Change "Localizable Strings" to "Interface Builder Cocoa Touch XIB"

XCODE XIB 检查器文件截图

  1. Run the app, the translation should be ok now !

You can then go back to "Localizable Strings" if you prefer... In this case please note that you must run at least once the app with the "Interface Builder Cocoa Touch XIB." option selected.

You can use NSLocalizedStringFromTable to retrieve the texts as generated by xcode's IB as follows, in viewDidLoad:

self.intro_text.text = NSLocalizedStringFromTable(@"2Vy-59-WN8.text", @"Main", @"");

Where 2Vy-59-WN8.text is the generated key you can find in the Main.strings for the UITextView.

BTW: this is an xcode/IB bug, not an iOS bug.

如果您想使用现有的 MainStoryboard.strings 文件,您可以使用 localizedStringForKey:value:table: 以编程方式分配 textview 的文本:

[[NSBundle mainBundle] localizedStringForKey:@"abc-12-3ba.text" value:@"" table:@"MainStoryboard"]

This is my Swift 2 answer:

We don't want to repeat the fix in each UIViewController, so let's start with an extension:

import UIKit

extension UIViewController {

    // if the property "Selectable" = NO it will reset the font and fontcolor when setText is used.
    // So turn Selectable ON 
    func localizeUITextViewsFromStoryboard(storyboardName: String) {
        for view in self.view.subviews {
            if let textView = view as? UITextView,
                let restorationIdentifier = textView.restorationIdentifier {
                let key = "\(restorationIdentifier).text"
                let localizedText = NSLocalizedString(key, tableName: storyboardName, comment: "")
                if localizedText != key {
                    textView.text = localizedText
                }

            }
        }
    }

}

In each UIViewController where we need this fix, we write

override func viewDidLoad() {
    super.viewDidLoad()
    self.localizeUITextViewsFromStoryboard("Main")
}

This code uses the restoration ID of the UITextViews. This means the we need to copy the Object ID to the restoration identifier in the storyboard for each UITextView.

I got this to work with Xcode 5.1.1 and iOS7.

Select the ViewController you want to localize in the left file panel. In the right panel select the File Inspector.

You will see a "Localize..." button. Tap the button. You will be presented with a drop down list of languages you can select from. Select Base and then tap "Localize".

In the right panel the Localization section will now have "Base" selected and your other languages will be listed. Select the languages you want to support. Leave the "native" language of your app unchecked.

There are definitely caching issues. I have found that changes only appear sometimes after I have deleted the app from simulator and when I delete Xcode's derived data.

File a bug with Apple. Let them know this is happening, and causing you (and me) time and frustration. This whole localization system is not there to have us fall back to NSLocalizedString..

UPDATE

The best way to actually change the text is to directly use the setter from the UITextView.

If your text is NOT formatted, simply use:

[textView setText:NSLocalizedString(key, comment)];

If your text IS formatted, you have to use:

[textView setAttributedText:[[NSMutableAttributedString alloc] initWithString:NSLocalizedString(key, comment)]];

You don't need to use the workaround anymore with the updated method.

WORKARROUND

To be able to achieve this, the easiest solution I have found is to simply add an extra UITextView with the translated text exactly at the same place as the other and hide it.

To display the proper view I do this in the viewDidLoad method of the controller:

NSString *language = [[NSLocale preferredLanguages] firstObject];
if ([language isEqualToString:FRENCH_LANGUAGE]) { //@"fr"
    UITextView.hidden = YES;
    UITextViewFR.hidden = NO;
}

Although this solution is definitely not elegant, it's certainly a good workaround when you don't have to keep a lot of translations.

That being said, using the storyboard localized string is not working. Adding an IBOutlet and setting the text attribute with a NSLocalizedString was not working too. I have looked around for a solution and it seems like no one has really been able to find a clear answer for that one.

Xcode 7 still seems buggy...

Before trying anything complicated try these 3 simple steps:

  1. Delete the app from the device (not the simulator)
  2. Product -> Clean
  3. Run on device

Good luck.

This issue still occurs in Xcode 11.5. I have decided to use UILabel instead and the localised text is being picked up as expected. This is an easy workaround when you keep static text in the view. Just remember to set maxLines to a higher number.

I finally found the reason. there was missing semicolon character one of the lines. I've putted that semicolon and problem fixed

XIB files do not have their own ".strings" files. You can generate ".strings" from XIB using ibtool, then after translation you use ibtool again to embed the translations into the localized XIB.

If you still have only one English XIB, you need to create a French XIB using Xcode, then do the ibtool process.

Take a look here: https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/Articles/LocalizingInterfaces.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM