Localization not loading in some cases

Hi,
here is a curious behavior hapening to my app.
Here is the use case.
  • App base lang is english.

  • Other translation is french

  • On a view controller, I have this:  self.introTextView.text = "DogProfileEditionVC-IntroTextView".localize()

  • On corresponding language files I have these:

Code Block
"DogProfileEditionVC-IntroTextView" = "french text";
"DogProfileEditionVC-IntroTextView" = "english text";



Use case on a freshly opened or built app:
  • Load viewcontroller. Text is correctly displayed in language.

  • dismiss view controller.

  • Open view controller again: text loaded is neither from the translation file, but from the storyboard.

So on first loading after app was closed, opened, or built, the view controller shows correct text. On subsequent display, it ignores translation files and only load text from storyboard.

This happens only one one view controller, and 3 texts. This doesn't happen on some text on the same VC.

Why this strange behavior? Why does it localize only on first presentation of the view controller, and not on subsequent opening unless I close app in between?

(i tried to reinstall app, clean build folder)

Here are the string extensions used to localize:


Code Block
// Add method to Strings for easily triggering the localization process.
extension String {
  func localize() -> String {
    return NSLocalizedString(
      self,
      tableName: "Localizable",
      bundle: .main,
      value: self,
      comment: self)
  }
   
  public func localize(with arguments: [CVarArg]) -> String {
    return String(format: self.localize(), locale: nil, arguments: arguments)
  }
}


Answered by Claude31 in 674964022
You could put it in viewWillAppear, to be sure it's done before any display.

Is it a UITextView you localise ?
If so, there is an old bug: UITextView text are not localized from Localizable.Strings with the text as key.
Need to call with the item ID as (table is Main.strings):
        helpTextView.text = NSLocalizedString("8Py-Vn-Jax.text", tableName: "Main", comment: "")  

https://stackoverflow.com/questions/28596877/localization-of-ios-storyboard-uitextview-texts-are-not-localized
It appears to be because my population of the ui text was in viewDidLoad, which doesn't run each time the view controller is modally presented.
It works if I put the ui population code in viewDidAppear, but is it the best place to put it?
Accepted Answer
You could put it in viewWillAppear, to be sure it's done before any display.

Is it a UITextView you localise ?
If so, there is an old bug: UITextView text are not localized from Localizable.Strings with the text as key.
Need to call with the item ID as (table is Main.strings):
        helpTextView.text = NSLocalizedString("8Py-Vn-Jax.text", tableName: "Main", comment: "")  

https://stackoverflow.com/questions/28596877/localization-of-ios-storyboard-uitextview-texts-are-not-localized
viewWillAppear worked best thanks.
Localization not loading in some cases
 
 
Q