NSLocalizedString shows raw key instead of loading a string from another language
Asked Answered
F

2

7

I'm having trouble using NSLocalizedString(key, comment: "") to load strings from Localizable.strings when the key is missing for the current language. NSLocalizedString returns a raw key

For instance, when string is present for English localization, but is missing for Russian:

"config.updating" = "Update in progress...";

Calling NSLocalizedString when iOS language set to Russian returns "config.updating"

NSLocalizedString("config.updating", comment: "") // "config.updating"

Shouldn't NSLocalizedString access the "AppleLanguages" key in NSUserDefaults to determine what the user's settings are and pick some other string?

Foumart answered 26/11, 2014 at 9:3 Comment(0)
W
2

No, 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.

Walleyed answered 26/11, 2014 at 9:11 Comment(4)
So what is the use of base localization file?Notation
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 valuesWalleyed
Okay, then how to handle the situation, where I have Localizable files for Base(English), English, Japanese, but in Japanese file I haven't added any key-value strings. When I set the device language to Japanese it shows "key" only but it should show strings from Base(English) file?Notation
No, because you have a Japanese localisation. The base file is only used where there isn't a localisation for that language, not when there is a missing key in the localised file. Where there is a localisation but the key is missing, the key value is shownWalleyed
P
0

You can probably create a Build Phase run script where you compare the keys of your translation between your base (say its English) with Russian and then if there is any difference you can either stop the build (exit 1 after echo "error:...") and show a build error with the missing keys as output or you can just show them as error and not stop the build.

1) In your Build Phase Run Script:

MISSING_KEYS_TRANSLATIONS=$(diff <($SRCROOT/tools/localization/check_missing_keys_in_translations.sh en) <($SRCROOT/tools/localization/check_missing_keys_in_translations.sh ru))
if [ "$MISSING_KEYS_TRANSLATIONS" ]; then
echo "error: $MISSING_KEYS_TRANSLATIONS"
fi

2) I have created in my project root folder a folder path like this tools/localization/ where I have put a bash script check_missing_keys_in_translations.sh:

#!/bin/sh
plutil -convert json 'ProjectRootFolder/Resources/Localization/'"$1"'.lproj/Localizable.strings' -o - | ruby -r json -e 'puts JSON.parse(STDIN.read).keys.sort'

3) Don't forget to make your script executable:

chmod a+x check_missing_keys_in_translations.sh

Phipps answered 27/2, 2019 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.