iOS Get localized version of a string for a specific language
Asked Answered
T

2

17

I'm building an application for iOS that will be available in both English and French languages. I've read some tutorials around internationalization and I have an understanding of how it works and what I need to do.

The problem I'm having is there is a specific case where I want to load French strings for an English user.

I understand it's possible to set the language for the entire application, but that it requires the application to be restarted before it will take affect. I'd like to avoid this, and instead be able to pick to load French or English strings on demand.

Is it possible to load strings from a .strings file for a specific language programmatically?

Turner answered 20/2, 2015 at 17:7 Comment(1)
Related: link.Inquest
F
11

Yes, it is possible, but it is not that easy to accomplished.

I just have the case, where I should send one and the same name(for all languages) of a ViewController for GAI (Google Analytics for iOS).

Preconditions:

1) I use the NSBundle extension from here https://mcmap.net/q/108237/-how-to-force-nslocalizedstring-to-use-a-specific-language - maybe it is a good idea to look up there first. (It is pretty genius to be honest)

2) I am using swift 2

Here is a pretty simple code sample to illustrate my idea:

func getFrenchString(forKey key: String) -> String {
    if let currentLanguage = (NSUserDefaults.standardUserDefaults().arrayForKey(AppleLanguages)?.first as? String) {
        if currentLanguage == "fr" {
            return NSLocalizedString(key, comment: "")
        }
        else {
            //the application is not currently on `fr`
            //change application to `fr`
            NSBundle.setLanguage("fr")

            //get the localized string on `fr`
            let frString = NSLocalizedString(key, comment: "")

            //return the application to the old language
            NSBundle.setLanguage(currentLanguage)

            return frString
        }
    }

    return ""
}

Also you should have "fr.lproj" folder with localised string in your project.

Finnegan answered 26/4, 2016 at 11:39 Comment(2)
Isn't it violating the app store guidelines?Lindell
I have not had any issues submitting apps to the App Store related to localisation so far. What point of the guidelines do you have in mind? @AlecvonBarnekowFinnegan
C
13

I solved this by extending String with this method. You can get localized string for any locale you have in your app this way.

extension String {
    func localized(forLanguageCode lanCode: String) -> String {
        guard
            let bundlePath = Bundle.main.path(forResource: lanCode, ofType: "lproj"),
            let bundle = Bundle(path: bundlePath)
        else { return "" }
        
        return NSLocalizedString(
            self,
            bundle: bundle,
            value: " ",
            comment: ""
        )
    }
}

Example (get localized string for ukrainian language when system language is english):

"settings_choose_language".localized(forLanguageCode: "uk")
Cypsela answered 25/2, 2021 at 17:25 Comment(1)
You don't need the macro to fetch the string. You can also use return bundle.localizedString(forKey: self, value: nil, table: nil)Rhumb
F
11

Yes, it is possible, but it is not that easy to accomplished.

I just have the case, where I should send one and the same name(for all languages) of a ViewController for GAI (Google Analytics for iOS).

Preconditions:

1) I use the NSBundle extension from here https://mcmap.net/q/108237/-how-to-force-nslocalizedstring-to-use-a-specific-language - maybe it is a good idea to look up there first. (It is pretty genius to be honest)

2) I am using swift 2

Here is a pretty simple code sample to illustrate my idea:

func getFrenchString(forKey key: String) -> String {
    if let currentLanguage = (NSUserDefaults.standardUserDefaults().arrayForKey(AppleLanguages)?.first as? String) {
        if currentLanguage == "fr" {
            return NSLocalizedString(key, comment: "")
        }
        else {
            //the application is not currently on `fr`
            //change application to `fr`
            NSBundle.setLanguage("fr")

            //get the localized string on `fr`
            let frString = NSLocalizedString(key, comment: "")

            //return the application to the old language
            NSBundle.setLanguage(currentLanguage)

            return frString
        }
    }

    return ""
}

Also you should have "fr.lproj" folder with localised string in your project.

Finnegan answered 26/4, 2016 at 11:39 Comment(2)
Isn't it violating the app store guidelines?Lindell
I have not had any issues submitting apps to the App Store related to localisation so far. What point of the guidelines do you have in mind? @AlecvonBarnekowFinnegan

© 2022 - 2024 — McMap. All rights reserved.