[ios] How to localise a string inside the iOS info.plist file?

As you might know the iOS 8 requires NSLocationWhenInUseUsageDescription key for using user's location. I have added this key and some general information into my info plist. enter image description here

How can I use translation string inside the plist file ?

-- Update --

I already have a Localizable string. I'm just wondering that can I use something like NSLocalizedString(MYSTRING,nil) inside the plist string. I know that I can create multiple file of info.plist for localisation but I was wondering there might be an easier way.

This question is related to ios xcode plist beta

The answer is


Tips

  1. Remember that the iOS Simulator exploits by default your system language. Please change the language (and region) in the iOS Simulator Setting too in order to test your translations.

  2. The localisation string (see Apple docs here) should be

    NSLocationWhenInUseUsageDescription = "Description of this";
    

    and not (with quote "...")

    "NSLocationWhenInUseUsageDescription" = "Description of this";
    

All the above did not work for me (XCode 7.3) so I read Apple reference on how to do, and it is much simpler than described above. According to Apple:

Localized values are not stored in the Info.plist file itself. Instead, you store the values for a particular localization in a strings file with the name InfoPlist.strings. You place this file in the same language-specific project directory that you use to store other resources for the same localization.

Accordingly, I created a string file named InfoPlist.strings and placed it in the xx.lproj folder of the "xx" language (and added it to the project using File->Add Files to ...). That's it. No need for the key "Localized resources can be mixed" = YES, and no need for InfoPlist.strings in base.lproj or en.lproj.

The application uses the Info.plist key-value as the default value if it can not find a key in the language specific file. Thus, I put my English value in the Info.plist file and the translated one in the language specific file, tested and everything works.

In particular, there is no need to localize the InfoPlist.strings (which creates a version of the file in the base.lproj, en.lroj, and xx.lproj), and in my case going that way did not work.


When using InfoPlist.strings file (in XCode it should be placed next to Info.plist file - real file location can be anywhere within the project probably

View in XCode

) be careful to use the key's short name for the translation.

I was trying to use Privacy - Camera Usage Description, but the working key is NSCameraUsageDescription


If something is not working make sure you added:

"Localized resources can be mixed" = YES

into the info.plist. In my case the InfoPlist.strings files were just ignored.


For anyone experiencing the problem of the info.plist not being included when trying to add localizations, like in Xcode 9.

You need make the info.plist localiazble by going into it and clicking on the localize button in the file inspector, as shown below.

The info.plist will then be included in the file resources for when you go to add new Localizations.

enter image description here


In my case everything was set up correctly but still the InfoPlist.strings file was not found.

The only thing that really worked was, to remove and add the InfoPlist.strings files again to the project.


In my case the localization not worked cause of '-' symbol in the name. Example: "aero-Info.plist" And localized files: "aero-InfoPlist.strings" and "aeroInfoPlist.strings" did not work.


As RGML say, you can create an InfoPlist.strings, localize it then add your key and the value like this: "NSLocationWhenInUseUsageDescription" = "Help To locate me!";

It will add the key to your info.plist for the specified language.


I would highly recommend reading Apple's guides, and viewing the WWDC resources listed here: Internationalization and Localization Topics

To specifically answer your question, when you add a new language to your projectenter image description here, you get an opportunity to choose what InfoPlist files to include (if you have multiple targets, you'll have multiple Info plist files). All you need to do to get the following screen is hit the + under Localizations and choose a new language to add support for. enter image description here

Once you've added, it will create the necessary string files in the appropriate lproj directories for the given language.

--EDIT--

Just to be clear, iOS will swap out the string for your Plist file based upon the user's currently selected language using the plist entry's key as the key in the localized strings file.


Step by step localize Info.plist:

  1. Find in the Xcode the folder Resources (is placed in root)
  2. Select the folder Resources
  3. Then press the main menu File->New->File...
  4. Select in section "Resource" Strings File and press Next
  5. Then in Save As field write InfoPlist ONLY ("I" capital and "P" capital)
  6. Then press Create
  7. Then select the file InfoPlist.strings that created in Resources folder and press in the right menu the button "Localize"
  8. Then you Select the Project from the Project Navigator and select the The project from project list
  9. In the info tab at the bottom you can as many as language you want (There is in section Localizations)
  10. The language you can see in Resources Folder
  11. To localize the values ("key") from info.plist file you can open with a text editor and get all the keys you want to localize
  12. You write any key as the example in any InfoPlist.strings like the above example

"NSLocationAlwaysAndWhenInUseUsageDescription"="blabla";

"NSLocationAlwaysUsageDescription"="blabla2";

That's all work and you have localize your info.plist file!


In addition to the accepted answer (the project is on Flutter but it's basically the same as native):

I do have folders Base.lproj, en.lproj, xx.kproj. etc. with InfoPlist.strings in each. This file has lines like this (no quotes around the key and with a semicolon at the end):

NSLocationWhenInUseUsageDescription = "My explanation why I need this";

Check that you have your languages in your YourProject > Info: enter image description here

Also, check the project.pbxproj file, it is in XXX.xcodeproj/project.pbxproj: it should have all your languages in codes (en, fr, etc.)

enter image description here

But even then it didn't work. Finally, I noticed the CFBundleLocalizations key in the Info.plist file. (to open it as raw key-values in XCode - right mouse button on the Info.plist file -> Open As -> Source Code) Make sure that the values in array are codes rather than complete words, for example fr instead of French etc.

<key>CFBundleLocalizations</key>
<array>
    <string>en</string>
    <string>ru</string>
    <string>lv</string>
</array>

And double-check that your device is set to the language you're testing. Cheers

P.S. "Development Language" doesn't affect your issue, don't bother changing it.


Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to xcode

Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64 iPhone is not available. Please reconnect the device Make a VStack fill the width of the screen in SwiftUI error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65 The iOS Simulator deployment targets is set to 7.0, but the range of supported deployment target version for this platform is 8.0 to 12.1 Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Xcode 10: A valid provisioning profile for this executable was not found Xcode 10, Command CodeSign failed with a nonzero exit code

Examples related to plist

NSCameraUsageDescription in iOS 10.0 runtime crash? iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash How to localise a string inside the iOS info.plist file?

Examples related to beta

Swift 3 - Comparing Date objects How to localise a string inside the iOS info.plist file? Android Studio Gradle project "Unable to start the daemon process /initialization of VM"