6

Storyboard

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. Does anyone have the solution to this problem? Any help would be greatly appreciated.

Community
  • 1
  • 1
user1927638
  • 1,133
  • 20
  • 42
  • possible duplicate of [Localize a view within a storyboard using "User Defined Runtime Attributes"](http://stackoverflow.com/questions/11779881/localize-a-view-within-a-storyboard-using-user-defined-runtime-attributes) (which concluded that this is a carryover from OS X and not supported on iOS). – Justin Feb 19 '14 at 15:38
  • I was really hoping that someone would have some more insight into this issue after all this time. I have read this topic and I was really hoping that that wasn't the case. – user1927638 Mar 01 '14 at 23:44
  • Note that you can still localize whole Interface Builder files like any other resource. – Justin Mar 02 '14 at 22:43
  • I'm relatively new to iOS development, but is the storyboard file considered an IB file? – user1927638 Mar 03 '14 at 03:02
  • Ah, in this case, you'll want to enable [`Use Base Internationalization`](http://stackoverflow.com/a/12731497/401774) and then create a strings file for each localization. – Justin Mar 03 '14 at 16:14

3 Answers3

2

I guess you expect the localized string to be taken from 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. Please take a look at https://stackoverflow.com/a/24527990/2876231 for a lengthier explanation.

Community
  • 1
  • 1
José González
  • 1,207
  • 12
  • 25
1

The attribute type needs to be Localizable String, and then you'd translate it in the .strings file using the following property:

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

Unfortunately, it doesn't seem to work with a named attribute, but only with the long property above.

(Based on Andrew's answer here: Localize a view within a storyboard using "User Defined Runtime Attributes")

Community
  • 1
  • 1
dAngelov
  • 830
  • 6
  • 10
0

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 -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 -i -e 's/^<userDefinedRuntimeAttribute type="string" keyPath="someTag" value=\("[^"]*"\)\/>/\1 = \1;/g' extracted-strings.txt
Erhard Dinhobl
  • 1,646
  • 18
  • 34