Swift framework localization

I am trying to localize Swift framework and added a file Localizable.Strings and that file is included in the target but the app that consumes this framework is showing the key not the actual string.

NSLocalizedString("string_key", comment: "Actual String")

I think issue is related to bundle. Do I need to add bundle parameter to NSLocalizedString? If so what is the bundle parameter value? If bundle is not required then what must be the issue

  • Could you show a small extract of your localisation file ? comment: "Actual String" is just a comment, never used at runtime.

  • Yes, comment parameter is for the development purpose. In the app UI, I see text as "string_key"

    Localizable.Strings contents will look like below

    */    Localizable.strings */

    "string_key" = "username";

    "string_key1" = "password";

    "string_key2" = "login";**

Add a Comment

Accepted Reply

Bundle was the problem. Default bundle is Bundle.main which is App's bundle not the framework bundle. Passing the frameworks bundle fixed the issue.

I used Bundle(identifier:) to get the framework's bundle. So the method will look like below.

NSLocalizedString("string_key", bundle: Bundle(identifier: "com.framework.bundle") ?? Bundle.main, comment: "Actual String")

Replies

Have a look here :

https://stackoverflow.com/questions/56917337/cannot-localize-my-swift-5-framework-localization-import-does-nothing

Try to call the extended NSLocalizedString API.

func NSLocalizedString(_ key: String, tableName: String? = nil, bundle: Bundle= Bundle.main, value: String = "", comment: String) -> String

Bundle was the problem. Default bundle is Bundle.main which is App's bundle not the framework bundle. Passing the frameworks bundle fixed the issue.

I used Bundle(identifier:) to get the framework's bundle. So the method will look like below.

NSLocalizedString("string_key", bundle: Bundle(identifier: "com.framework.bundle") ?? Bundle.main, comment: "Actual String")