2

I've implemented multilingual in my app and having multiple Localizable.strings files created.

enter image description here

I have all strings key and value in Localizable.strings (Base) file, as its default file read from all other languages if perticular string missing in specific language string file e.g. Localizable.strings (Japanese) and some strings are missing in Japanese file so I expecting to get string translation from Base.

But it doesn't happen, thats my problem.

Sunil Targe
  • 7,251
  • 5
  • 49
  • 80

3 Answers3

5

I faced the same issue and applied this solution:

  1. Create new default String localize file for English (DefaultEnglish.strings) E.g: "hello_key" = "Hello"
  2. Add other language support to Localizable.strings file E.g: Japanese string: "hello_key" = "こんにちは"

enter image description here

  1. Create String extension

    extension String {
    
    func localizedStringKey(bundle _: Bundle = .main, tableName: String = "Localizable") -> String {
        return NSLocalizedString(self, tableName: tableName, value: "\(NSLocalizedString(self, tableName: "DefaultEnglish", bundle: .main, value: self, comment: ""))", comment: "")
    }     }
    
  2. and set string "hello_key".localizedStringKey()

Harshal Wani
  • 2,249
  • 2
  • 26
  • 41
  • Btw, it's better to check value, returned by NSLocalizedString, and if we don't have such key - we should take some actions, for example - write a log message. – Torello May 31 '19 at 05:18
2

No, The base localisation file is used when there is no specific localised version of the file available. E.g. if your app has a base localisation of English and you add French localisation, languages other than French will get the English values.

The documentation for NSLocalizedString(key,comment) is pretty clear -

The initial value for key in the strings file will be key. Use the NSLocalizedStringWithDefaultValue macro to specify another value for key.

What else would you expect it to return? The code simply looks up the key in a dictionary. It has no idea what message is associated with the key, let alone how to translate that message into Russian.

ViruMax
  • 1,216
  • 3
  • 16
  • 41
1

When localising an an app, there's no need to use a Base Localizable.strings file.

Localise your strings as follows…

NSLocalizedString("DISMISS_BUTTON", value: "Press To Dismiss", comment: "Button used to dismiss the payment screen")

Then key (the first parameter) is used to look up in a Localizable.strings for a given language. If it's not found, it will use the value parameter in the string as a fallback.

If you use Xcode's Export For Localization…, the xliff file will include the keys from all your NSLocalizedStrings

The only time you'll need a Base Localizable.strings is for keys that don't appear as a string in your source, .e.g.

let localisableName = "LAYOUT_NAME_" + name.uppercased()
NSLocalizedString(localisableName)

In this case, you should add

"LAYOUT_NAME_WHATEVER" = "Whatever";

to your base file, and it will also be included in the generated xliff

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160