Abedalkareem / LanguageManager-iOS

A Language manager to handle changing app language without restarting the app. It supports iOS and tvOS.
MIT License
396 stars 67 forks source link

Not all string being localized. #48

Closed MrRonanX closed 3 years ago

MrRonanX commented 4 years ago

Hi! I'm doing the localization for my app and here I've faced the problem. Somewhy my strings have randomly being translated. Some of them are still in English while others are in Ukrainian. Here how I change language:

var availableLanguages = Bundle.main.localizations
        availableLanguages.removeLast()
        let alert = UIAlertController(title: nil, message: "Switch Language", preferredStyle: .actionSheet)
        for language in availableLanguages {
            let languageAction = UIAlertAction(title: language, style: .default, handler: {
                (alert: UIAlertAction!) -> Void in
                print("\(language) is set")
                switch language {
                case "en": self.setLanguage(_:.en);
                case "uk": self.setLanguage(_:.uk);
                default: fatalError()
                }
            })
            alert.addAction(languageAction)
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: {
            (alert: UIAlertAction) -> Void in
        })
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }
private func setLanguage(_ language: Languages) {
        LanguageManager.shared.setLanguage(
            language: language,
            for: nil,
            viewControllerFactory: { (title) -> UIViewController in
                print(title ?? "")
                // get the storyboard.
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                // instantiate the view controller that you want to show after changing the language.
                return storyboard.instantiateInitialViewController()!
        }) { view in
            // do custom animation
            view.transform = CGAffineTransform(scaleX: 2, y: 2)
            view.alpha = 0
        }
    }

Also, I store all my strings in a String file for convenience. There are more than 100 of them, let me show you an example:

let addNewPlayer = NSLocalizedString("Enter title", comment: "Enter title and take photo").localiz()
let error = NSLocalizedString("Error", comment: "Error occurred").localiz()
let pressPlus = NSLocalizedString("Press Plus", comment: "Press plus to add new player").localiz()
Abedalkareem commented 4 years ago

Hi, can you try to call localize directly on the strings? let addNewPlayer = "Enter title".localiz() let error = "Error".localiz() let pressPlus = "Press Plus".localiz()