我可以将 uilabel 链接到 Interface Builder 中的可本地化字符串吗?

发布于 2024-12-04 15:08:10 字数 148 浏览 0 评论 0原文

已经用谷歌搜索过,但没有找到解决方案:

基本上,我设置了一个 Localized.strings ,我在代码中使用它。然而,如果我能以某种方式在我的 XIB 中引用这些值,那就太好了,这样我就可以避免为每种语言创建一个烦人的 XIB...

这可能吗?

have googled around but found no solution:

Basically, i have a Localizable.strings set up, which i'm using in my code. However, it would be really sweet if i somehow could just refer those values in my XIB's too, so that i can avoid having to create one annoying XIB per language...

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

层林尽染 2024-12-11 15:08:10

您可以实现 UILabel 的子类,并让它在从笔尖初始化时自动提取本地化值。
在 XIB 中,您只需设置标记(如果您愿意,也可以设置英文文本)并让 UILabel 从该值中提取本地化文本。

You could implement a subclass of UILabel and have it automatically pull the localized value when it's initialized from the nib.
In the XIB you would then just set the token (or english text if you prefer) and have UILabel pull the localized text from this value.

叫思念不要吵 2024-12-11 15:08:10

我向您介绍快捷方式

1。创建UILabel的子类

class LozalizedLabel : UILabel {

    @IBInspectable var keyValue: String {
        get {
            return self.text!
        }
        set(value) {
            self.text = NSLocalizedString(value, comment: "")

        }
    }
}

2.在 IB 中将类设置为 LocalizedLabel

在此处输入图像描述

3.直接在 IB 中输入 Localized.strings 中的密钥

在此处输入图像描述

瞧,现在花更少的时间来创建无用的 IBOutlet,只是为了使其成为可本地化的 UILabel。

注意
这不会使本地化字符串显示在界面生成器中,而只是显示“键”。因此,在构建 TextView 时,请记住相应地设置约束

I present to you the swifty way

1. Create a subclass of UILabel

class LozalizedLabel : UILabel {

    @IBInspectable var keyValue: String {
        get {
            return self.text!
        }
        set(value) {
            self.text = NSLocalizedString(value, comment: "")

        }
    }
}

2. Set the class to be LocalizedLabel in IB

enter image description here

3. Enter your key from Localizable.strings directly in IB

enter image description here

Voila, now spend less time on creating useless IBOutlets just for making it a localizable UILabel.

NOTE
This does not make the localized string show in the interface builder, but rather just the "key". So when building your TextView's remember to set constraints accordingly

白鸥掠海 2024-12-11 15:08:10

无法直接使用 Interface Builder 从 Localized.strings 进行设置。

您可能想要翻译 NIB 的原因是每种语言的单词长度可能有所不同。因此,您可以更改每种语言的笔尖布局。

您可以在 UIViewControllerviewDidLoad 方法中设置它们。

- (void) viewDidLoad {
   [supper viewDidLoad];

   self.someLabel.text = NSLocalizedString(@"Some Label", @"Some label on some view.");
}

There is no way to set then from the Localizable.strings directly with Interface Builder.

The reason you might want to translate the NIB is because the length of the words may vary in each language. Thus allowing you to change the layout of you nib per language.

What you could set them in the viewDidLoad method of your UIViewController.

- (void) viewDidLoad {
   [supper viewDidLoad];

   self.someLabel.text = NSLocalizedString(@"Some Label", @"Some label on some view.");
}
无远思近则忧 2024-12-11 15:08:10

NSLocalizedString 的优点是易于实现。风险在于您可能会忘记为新的 Gui 元素添加条目。

您可以使用itool从原始xib文件中转储字符串,翻译这些转储的字符串,然后将翻译后的字符串加载到other-language-xib文件中。

NSLocalizedString 相比,这样做有一些优点。

  • 所有对翻译和字符串长度的考虑仍然适用,但您只需要接触原始的 xib,其他语言可以通过脚本更新。
  • 您可以看到已翻译的元素以及转储文件中每个 UIElement 的不同可能状态。
  • 您将有更多的单个字符串文件发送给翻译人员,但更容易了解哪些部分需要翻译、哪些部分已完成。
  • GUI 对英语 xib 所做的更改将应用​​于本地化的 xib,您基本上是将原始 xib 与翻译后的字符串合并。

创建 XIB 时,将其设置为本地化(如前所述),然后针对此原始 xib 运行 itool 以提取字符串。
将字符串文件翻译成所需的语言,然后将翻译加载到本地化的 xib 中。

如果您在 google 上搜索 ibtool --generate-strings-file,您应该会找到一些教程和示例。

语法应类似于:

  #dump original xib to a $xibfile.strings file:
  ibtool --generate-strings-file Resources/English.lproj/$xibfile.strings Resources/English.lproj/$xibfile.xib
  # offline: translate the $xibfile.xib, place it into the correct lproj folder
  # then: merge translated $xibfile.strings with original xib to create localized xib
  ibtool --strings-file Resources/de.lproj/$xibfile.strings --write Resources/de.lproj/$xibfile.xib Resources/English.lproj/$xibfile.xib

Resources/English.lproj 目录中的 $xibfile.strings 将包含原始(英语)字符串。翻译后的 $xibfile.strings 必须位于相应的 xx.lproj 目录中。

如果您将构建脚本添加到 xcode,则每次构建时都会调用翻译的加载。

NSLocalizedString has the advantage of being easy to implement. Risk is that you might forget to add an entry for a new Gui element.

You can use itool to dump the string from the original xib file, translate these dumped strings and then load the translated string into the other-language-xib file.

There are some advantages doing it this way compared to NSLocalizedString.

  • All the consideration for translation and string length still apply but you need to touch only the original xib, the other languages can be updated by script.
  • You see the elements already translated and the different possible states for each UIElement from the dumped file.
  • You will have more single strings files to send to your translator but it is easier to keep an overview as to what part is in need of translation, what part is done.
  • GUI Changes done to the english xib are applied to the localized xib, you are basically merging the original xib with the translated strings.

When you create a XIB you set it to being localized (as before), then run itool against this original xib to extract the strings.
Translate the strings file into the desired language then load the translation into the localized xib.

If you google for ibtool --generate-strings-file you should find a few tutorials and examples.

The syntax should be similar to:

  #dump original xib to a $xibfile.strings file:
  ibtool --generate-strings-file Resources/English.lproj/$xibfile.strings Resources/English.lproj/$xibfile.xib
  # offline: translate the $xibfile.xib, place it into the correct lproj folder
  # then: merge translated $xibfile.strings with original xib to create localized xib
  ibtool --strings-file Resources/de.lproj/$xibfile.strings --write Resources/de.lproj/$xibfile.xib Resources/English.lproj/$xibfile.xib

$xibfile.strings in the Resources/English.lproj directory will contain the original (English language) strings. The translated $xibfile.strings must go in the appropriate xx.lproj directory.

If you add a buildscript to xcode then the loading of the translation will be called at each build.

凉栀 2024-12-11 15:08:10

IB 无法使用 Localized.strings 中的字符串,但还有另一个选项,称为基本国际化。

简而言之:您可以创建附加的单独的 .strings 文件到每种所需语言的 xib/storyboard。在结果中,您将获得 xib 及其针对每种语言的自己的翻译。所以它可以复制你的一些值
Localized.strings,但是比通过代码设置或者复制xib文件更清晰的方式。

请参阅有关 Base 国际化的 Apple 文档。

IB cannot use strings from Localizable.strings, but there is another option called Base Internationalization.

In short: you can create separate .strings file attached to the xib/storyboard for every required language. In the result you will have xib with it's own translations for each language. So it can duplicate some of the values that you have in
Localizable.strings, but it's more clear way than setting through the code or copying xib files.

Please refer to the Apple Documentation about Base Internationalization.

流年里的时光 2024-12-11 15:08:10

另一种快捷方式,但带有扩展:

extension UILabel {
    static var localizedKey:UInt8 = 0

    @IBInspectable public var localizationKey: String? {
        set {
            objc_setAssociatedObject(self, &UILabel.localizedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
        get {
            return objc_getAssociatedObject(self, &UILabel.localizedKey) as? String
        }
    }

    open override func awakeFromNib() {
        super.awakeFromNib()
        if let localizationKey = self.localizationKey {
            self.text = NSLocalizedString(localizationKey, comment: "")
        }
    }
}

然后您将能够直接从 IB 编写本地化密钥,并且它将在运行时进行本地化。

Another swifty way, but with extension :

extension UILabel {
    static var localizedKey:UInt8 = 0

    @IBInspectable public var localizationKey: String? {
        set {
            objc_setAssociatedObject(self, &UILabel.localizedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
        }
        get {
            return objc_getAssociatedObject(self, &UILabel.localizedKey) as? String
        }
    }

    open override func awakeFromNib() {
        super.awakeFromNib()
        if let localizationKey = self.localizationKey {
            self.text = NSLocalizedString(localizationKey, comment: "")
        }
    }
}

You will then be able to write the localization key directly from IB, and it will be localized at runtime.

北风几吹夏 2024-12-11 15:08:10

您可以在代码中执行此操作:

    label.text = NSLocalizedString(@"key", @"comment");

但我认为除了本地化整个笔尖之外,没有其他方法可以仅使用笔尖来完成此操作。我过去肯定没有找到其他方法。

You can do this in code:

    label.text = NSLocalizedString(@"key", @"comment");

But I don't think there is a way to do it with just nibs other than localizing the entire nib. I've certainly not found another way in the past.

肩上的翅膀 2024-12-11 15:08:10

这是我的版本。希望有帮助。

extension UILabel {

    /// The IBInspectable helps us to set localizable text in IB
    @IBInspectable var localizableText: String? {
        get { return text }
        set(value) { text = value?.localized() }
    }


    /// The IBInspectable helps us to set localizable text in IB
    @IBInspectable var localizableUppercaseText: String? {
        get { return text }
        set(value) { text = value?.localized().uppercased() }
    }
}

extension String {

    func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
        return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
    }

}

This is my version. Hope it helps.

extension UILabel {

    /// The IBInspectable helps us to set localizable text in IB
    @IBInspectable var localizableText: String? {
        get { return text }
        set(value) { text = value?.localized() }
    }


    /// The IBInspectable helps us to set localizable text in IB
    @IBInspectable var localizableUppercaseText: String? {
        get { return text }
        set(value) { text = value?.localized().uppercased() }
    }
}

extension String {

    func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
        return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
    }

}
¢蛋碎的人ぎ生 2024-12-11 15:08:10

Apple 支持的本地化 .xib 文件的方法。这允许您本地化每种语言/国家/地区的文本和布局。

在此处输入图像描述

The Apple-supported way to the localize the .xib file. This allow you to localize the text and layout for each language/country.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文