Search code examples
ioscocoanslocalizedstring

How to obtain NSLocalizedString for specific locale, or how to get non-localized string


Is there a way of getting the localized string for a particular locale, other than the current locale?

Something like a reverse lookup, i.e. I have the localized value, I want to get to the base localized version or even the key.

myLocalizedString.getLocalizedString( locale : NSLocale )

I am well aware of the fact that this isn't super clean, but I don't need explanations as to why it's not a good idea. I'm just in a situation where it seems the least bad.


Solution

  • You can simply call this method by sending language code & key string.

    func localizestring(for languageCode : String , keyString : String) -> String {
        //language code like --> en/ef/da
        let path = Bundle.main.path(forResource: languageCode, ofType: "lproj")
        let bundle = Bundle(path: path!)
        return NSLocalizedString(keyString , tableName: nil, bundle: bundle!, value: "", comment: "")
    }