简体   繁体   English

具有 Interface Builder 用户定义的运行时属性的本地化字符串

[英]Localized String with Interface Builder User Defined Runtime Attributes

故事板

I am currently trying to create a localized accessibilityLabel in the storyboard (I am trying to avoid doing it programatically).我目前正在尝试在故事板中创建一个本地化的可访问性标签(我试图避免以编程方式进行)。 It seems that whenever I use the Localized String option, the accessibilityLabels ends up being set to the localized string key that I have provided rather than the string itself.似乎每当我使用本地化字符串选项时,accessibilityLabels 最终都会被设置为我提供的本地化字符串键,而不是字符串本身。 Does anyone have the solution to this problem?有没有人有解决这个问题的方法? Any help would be greatly appreciated.任何帮助将不胜感激。

I guess you expect the localized string to be taken from Localizable.strings.我猜您希望从 Localizable.strings 中获取本地化字符串。 The "Localized String" type doesn't work this way, it's just a marker to indicate that the value of the user defined runtime attribute will participate in the localization process when you use base localization. “Localized String” 类型不会以这种方式工作,它只是一个标记,表明当您使用基本本地化时,用户定义的运行时属性的值将参与本地化过程。 Please take a look at https://stackoverflow.com/a/24527990/2876231 for a lengthier explanation.请查看https://stackoverflow.com/a/24527990/2876231以获得更详细的解释。

The attribute type needs to be Localizable String , and then you'd translate it in the .strings file using the following property:属性类型需要是Localizable String ,然后您将使用以下属性在.strings文件中翻译它:

"KLc-fp-ZVK.ibExternalUserDefinedRuntimeAttributesLocalizableStrings[0]" = "¡Hola!";

Unfortunately, it doesn't seem to work with a named attribute, but only with the long property above.不幸的是,它似乎不适用于命名属性,而只能使用上面的 long 属性。

(Based on Andrew's answer here: Localize a view within a storyboard using "User Defined Runtime Attributes" ) (基于 Andrew 在此处的回答: Localize a view in a storyboard using "User Defined Runtime Attributes"

I did the customization of an attribute with the simple solution of localizing the attribute by code:我使用通过代码本地化属性的简单解决方案对属性进行了自定义:

private struct AssociatedKeys {
    static var someTagKey = "someTag"
}

@IBInspectable var someTag: String? {
    get {
       return NSLocalizedString(
            objc_getAssociatedObject(self, &AssociatedKeys.someTagsKey) as? String ?? "", comment: "")
    }
    set {
        if let newValue = newValue {
            objc_setAssociatedObject(
                self,
                &AssociatedKeys.someTagsKey,
                newValue as NSString?,
                objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
            )
        }
    }
}

And after that you can easily extract all the strings from the xib and storyboard files with an egrep:之后,您可以使用 egrep 从 xib 和故事板文件中轻松提取所有字符串:

egrep -ohr --include="*.xib" --include="*.storyboard" '<userDefinedRuntimeAttribute type="string" keyPath="someTag" value="[^"]+"/>' . >> extracted-strings.txt

And after that eliminate the tags in the strings file by the following sed and then you have to plain strings file for xcode ready:然后通过以下 sed 消除字符串文件中的标签,然后您必须为 xcode 准备纯字符串文件:

sed -i -e 's/^<userDefinedRuntimeAttribute type="string" keyPath="someTag" value=\("[^"]*"\)\/>/\1 = \1;/g' extracted-strings.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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