Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the language for a tableview with separate tableviewcell files #43

Closed
jrumol opened this issue Oct 15, 2018 · 9 comments
Closed
Labels

Comments

@jrumol
Copy link

jrumol commented Oct 15, 2018

Hi, thanks for your work.

I am trying to translate an app that is built with a tableviewcontroller and each row has a separate tableviewcell file, and each tableviewcell has a collectionviewcell. This is to present a view like NetFlix or the AppStore: collectionviews embedded in tableviews.

The problem with the translation comes when I want to translate in the HomeTableViewController file. Because the strings to translate are in the TableViewCell file, that has no viewdidload or viewwillappear.

Any help would be very much appreciated.

@andresilvagomez
Copy link
Owner

andresilvagomez commented Oct 16, 2018

@jrumol Hi, thanks four your comments, I really appreciate it and I try to keep it updated soon possible.

About your questions, you don't need viewdidload or viewwillappear specific, you can do it in wherever moment, maybe you can do it in awakeFromNib like

override func awakeFromNib() {
    super.awakeFromNib()
    "yourstring".localized
}

or you can create a method and call it in your table view controller,

I hope this answer can help you, let me know more comments about that.

@andresilvagomez
Copy link
Owner

@jrumol some news?

@jrumol
Copy link
Author

jrumol commented Oct 19, 2018

Hola Andrés,

Muchísimas gracias por tu interés. Esta semana he estado muy ocupado y no he tenido tiempo de dedicarle a la aplicación. En cuanto pueda te digo.

El problema que se me presenta con respecto a lo que me dices de ponerlo en awakeFromNib es que así se traducen los String que están fuera del tableview, es decir, el título y poca cosa más. Casi todo lo que hay que traducir está dentro de las funciones "cellforrowatindexpath" y en "viewforheaderatindexpath". Y... ¿como le dices que cada vez que aparezca el controlador renueve todos esos valores? Porque la función tableview.reloaddata() no funciona.

Perdón por el español... pero viendo tu nombre pensé que me entenderías en español.

@jrumol
Copy link
Author

jrumol commented Oct 20, 2018

Hi again Andrés,

I had an hour to try it with my app and your Pod.

One question: I am using Xcode 10 and when I write the "Observer" using the code in the Readme file;

NotificationCenter.default.addObserver(self, selector: #selector(translateSettings), name: NSNotification.Name(LanguageChangeNotification), object: nil)

I get an error because the expression "LanguageChangeNotification" doesn't exist. And suggest to fix it with the expression "localizeChangeNotification". I changed it and it worked ok.

I added in the Appdelegate -> didFinishLaunchingWithOptions the following line:

Localize.update(fileName: "Localizable")

Because I have the strings translation in the file Localizable.strings, although I also have a Main.strings file attached to the Main.storyboard.

The tableview I am using has static cells, so I insert the headers and the footers for each section in the Main.storyboard, that is also localised. And add the localisation strings to the Main.strings file for each language.

This is my code:

`import UIKit
import Localize

class SettingsTableViewController: UITableViewController {

@IBOutlet weak var buyFullVersion: UIButton!
@IBOutlet weak var restorePurchase: UIButton!

@IBOutlet weak var italianLabel: UILabel!
@IBOutlet weak var frenchLabel: UILabel!
@IBOutlet weak var deutschLabel: UILabel!

@IBOutlet weak var englishButton: UIButton!
@IBOutlet weak var spanishButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    
}

override func viewWillAppear(_ animated: Bool) {
    
    self.navigationController?.setNavigationBarHidden(false, animated: true)
    NotificationCenter.default.addObserver(self, selector: #selector(translateSettings), name: NSNotification.Name(localizeChangeNotification), object: nil)
    translateSettings()
    self.tableView.reloadData()

}

// MARK: - Table view data source

// MARK: - Changing the language:

@IBAction func changeToEnglish(_ sender: Any) {
    Localize.update(language: "en")
}

@IBAction func changeToSpanish(_ sender: Any) {
    Localize.update(language: "es")
}

// Setting the purchases.
@IBAction func buyFullVersion(_ sender: Any) {
}

@IBAction func restorePurchase(_ sender: Any) {
}

@objc func translateSettings() {
    
    // Sets the view title.
    self.title = "settingsTitle".localize()
    // Sets the header section title:
    tableView.reloadSectionIndexTitles()
    // Sets the buttons title:
    englishButton.setTitle("en".localize(), for: .normal)
    spanishButton.setTitle("es".localize(), for: .normal)
    
    italianLabel.text = "it".localize()
    frenchLabel.text = "fr".localize()
    deutschLabel.text = "de".localize()
    buyFullVersion.setTitle("buyFullVersion".localize(), for: .normal)
    restorePurchase.setTitle("restorePurchase".localize(), for: .normal)
    
}

}`

When changing between Spanish and English, only the view title, the buttons and the labels are changed. However, the section headers and footers doesn't change.

The function tableView.reloadData() doesn't work and also tableView.reloadSectionIndexTitles().

@jrumol
Copy link
Author

jrumol commented Oct 20, 2018

Hi again,

I managed to make it work. The key was to include in the function "translateSettings" the line self.reloadData()

The cose is as follows:

`
import UIKit
import Localize

class SettingsTableViewController: UITableViewController {

@IBOutlet weak var languageSelectedFlag: UIImageView!
@IBOutlet weak var languageSelectedButton: UIButton!
@IBOutlet weak var buyFullVersion: UIButton!
@IBOutlet weak var restorePurchase: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    
}

override func viewWillAppear(_ animated: Bool) {
    
    self.navigationController?.setNavigationBarHidden(false, animated: true)
    translateSettings()        
    
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 0:
        return "languageSection".localize()
    case 1:
        return "purchasesSection".localize()
    case 2:
        return "helpSection".localize()
    default:
        return ""
    }
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    switch section {
    case 0:
        return "languageFooter".localize()
    default:
        return ""
    }
}

// MARK: - Changing the language by going to the LanguageSelectionView:

@IBAction func goToLanguageSelection(_ sender: Any) {
    
    performSegue(withIdentifier: "goToLanguages", sender: nil)
    
}

// Setting the purchases.
@IBAction func buyFullVersion(_ sender: Any) {
}

@IBAction func restorePurchase(_ sender: Any) {
}

@objc func translateSettings() {
    
    // Sets the view title.
    self.title = "settingsTitle".localize()
    // Sets the button title and image:
    languageSelectedFlag.image = UIImage(named: Localize.currentLanguage)
    languageSelectedButton.setTitle(Localize.currentLanguage.localize(), for: .normal)

    buyFullVersion.setTitle("buyFullVersion".localize(), for: .normal)
    restorePurchase.setTitle("restorePurchase".localize(), for: .normal)
    
    // Updates the header and footer section titles:
    self.tableView.reloadData()

}

}
`

However, I still don't know how to translate the tableview with collectionViews embedded. If I put it in "awakeFromNib" it doesn't work.

@andresilvagomez
Copy link
Owner

@jrumol Hii, yes I speak Spanish also, I'm checking this issue right now, I'll back soon

@andresilvagomez
Copy link
Owner

@jrumol Hii, thanks for that feedback, yes the documentation is wrong, I updated it and put an example in the example project, the key to listen is localizeChangeNotification,

NotificationCenter.default.addObserver(
    self,
    selector: #selector(localize),
    name: NSNotification.Name(localizeChangeNotification),
    object: nil
 )

let me know if it works for you, and yes I speak Spanish :) If you feel better write me in Spanish :) also,

Thanks and have a good day

@andresilvagomez
Copy link
Owner

@jrumol Hii, any update?

@jrumol
Copy link
Author

jrumol commented Oct 26, 2018

Hi again Andrés,

I haven't had enough time to solve the problem. However, I found something. If I write the section headers dinamically in the function viewForHeaderInSection, like this:

sectionHeader.text = catalog.allSections[section].name.localize()

It doesn't work and doesn't update unless you restart the App. But if I write the following within viewForHeaderInSection:

switch section in { case 0: sectionHeader.text = "firstSection".localize() case 1: sectionHeader.text = "seconSection".localize() case 2: sectionHeader.text = "thirdSection".localize() ... }

It updates everytime I show this view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants