Localize a view within a storyboard using “User Defined Runtime Attributes”

前端 未结 3 1085
心在旅途
心在旅途 2021-01-04 08:43

I currently have a semi-automated way to localize my views. However, today I found an interesting section in IB, which seems to suggest that I can localize my views from wit

相关标签:
3条回答
  • 2021-01-04 09:30

    For me, using iOS 6, if you generate your strings from your base localized storyboard (by having XCode generate them or using the ibtool --generate-strings-file you'll get autogenerated strings that look like this (should go in MainStoryBoard.strings, for example):

    /* Class = "IBUITextField"; b4a-O4-bNZ.ibExternalUserDefinedRuntimeAttributesLocalizableStrings[0] = "Event Name"; ObjectID = "b4a-O4-bNZ"; */
    "b4a-O4-bNZ.ibExternalUserDefinedRuntimeAttributesLocalizableStrings[0]" = "Event Name";
    

    Unfortunately, it'd be nice to have them identified by the Key Path, but at least you have the location where your user defined strings should be localized.

    0 讨论(0)
  • 2021-01-04 09:37

    The "user defined runtime attributes" are poorly documented. What I can remember from some book I read is, that UDRA was first implemented for MacOSX programming, so the Type "Localized String" could be a feature that is not fully supported for now in iOS.

    The funny thing is, that he is translating the strings in the storyboard previews (xCode 4.5.1), but later in the compiled iOS app, he is just injecting the key string.

    One solution I am thinking about right now, is to make a little helper class, that is checking the title/text strings of views on viewDidLoad for a keyword like "key", e.g. "XYControllerTitleKey", and then make NSLocalizedString-Method on that.

    UPDATE: It seems that in the meantime, there is a way to use the UDRAs:

    Storyboard/XIB and localization best practice

    And a tutorial as a result (link)

    0 讨论(0)
  • 2021-01-04 09:40

    The purpose of the "Localized String" type is to let you define a runtime attribute value that will participate in the localization process (using base localization). This is handy if for example you define a custom control, include it in the storyboard and want to assign a localizable string to one of its properties. However this only works on Mac OS, not on iOS.

    You can easily confirm this doing the following experiment: put a UILabel / NSTextField in your storyboard and set the "text" / "stringValue" property using a user defined runtime attribute. If you use "String" as the type of the attribute and generate the corresponding strings file you won't see it anywhere in the file. In contrast, if you change the type to "Localized String" and generate the strings file you will find an entry like this:

    /* Class = "IBUILabel"; wij-Kq-q92.ibExternalUserDefinedRuntimeAttributesLocalizableStrings[0] = "Localized value"; ObjectID = "wij-Kq-q92"; */
    "wij-Kq-q92.ibExternalUserDefinedRuntimeAttributesLocalizableStrings[0]" = "Localized value";
    

    Then you can localize this value in the corresponding language strings file. Again, this works on Mac OS, but not on iOS.

    0 讨论(0)
提交回复
热议问题