Is it possible to create a resource file in Xcode?

For such a situation, let's say I have a 4 view controller, and each has an "Ok" button, and now I need the buttons not to say "Ok", but "Next".

I need to open each button and change the text.

Is it possible to create a file of string resources and simply provide links to the necessary resource?

Or how does it work in Xcode?

  • Do you want to do it right in Storyboard / Xib or in code? - Artem Novichkov
  • @ArtemNovichkov I do not know, it seems to me convenient from the Storyboard ? - Aleksey Timoshchenko

2 answers 2

As a result, I found a detailed article on how to do this through the plist file.

  1. Create a plist file. I have specified the name Localizable
  2. In the inspector of this file, click the Localize button.
  3. Add key value pairs as in the screenshot

enter image description here

  1. Then add this code to the project

    import foundation

    private class Localizator {

    static let sharedInstance = Localizator ()

    lazy var localizableDictionary: NSDictionary! = {if let path = NSBundle.mainBundle (). pathForResource ("Localizable", ofType: "plist") {return NSDictionary (contentsOfFile: path)} fatalError ("Localizable file NOT found")}} ()

    func localize (string: String) -> String {guard let localizedString = localizableDictionary.valueForKey (string) ?. valueForKey ("value") as? String else {assertionFailure ("Missing translation for: (string)") return ""} return localizedString}}

    extension String {var localized: String {return Localizator.sharedInstance.localize (self)}}

  2. and so you can access

    print (“Accept” .localized)

And in the log you get the value that is on the key "Accept", in this case it will be the string "Accept". Since the key name and value match

And here is the link to the original article.

https://medium.com/@dcordero/a-different-way-to-deal-with-localized-strings-in-swift-3ea0da4cd143#.7hnfq2yeu

    If you need to change only the lines that appear in the buttons or labels, then I see two solutions:

    • If you need to change the text, you can simply use Xcode to change all the lines using search and replace;
    • The second way is more correct - use NSLocalizedString and store all strings in the localization file. This will allow you to quickly change the text and easily add new localizations. Example of use:

       let button = UIButton() button.setTitle(NSLocalizedString("button_titles.ok", comment: ""), for: .normal) 

      And this is what Localizable.strings will look like:

       "button_titles.ok" = "OK"; 
    • Is it from Objective C? - Aleksey Timoshchenko
    • In Swift, you can also use - Artem Novichkov
    • But I understand that this way you can install from code? - Aleksey Timoshchenko
    • I always do localization in code, but it is also possible to add localization for Storyboard and Xib. The second method is not very convenient, because there are difficulties with the transfer of text for localization to the translator or using a third-party service. - Artem Novichkov
    • If I understand correctly, do you assign title buttons to the code? I simply have no experience, so I don’t quite understand what you say in the answer that the second method is more correct, then in the comments that the second method is not very convenient ... So if I understood correctly, then using NSLocalizedString I can make all the String I need and then on the key to use them? - Aleksey Timoshchenko