Giter Club home page Giter Club logo

Comments (9)

andresilvagomez avatar andresilvagomez commented on August 29, 2024 1

@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.

from localize.

andresilvagomez avatar andresilvagomez commented on August 29, 2024

@jrumol some news?

from localize.

jrumol avatar jrumol commented on August 29, 2024

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.

from localize.

jrumol avatar jrumol commented on August 29, 2024

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().

from localize.

jrumol avatar jrumol commented on August 29, 2024

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.

from localize.

andresilvagomez avatar andresilvagomez commented on August 29, 2024

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

from localize.

andresilvagomez avatar andresilvagomez commented on August 29, 2024

@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

from localize.

andresilvagomez avatar andresilvagomez commented on August 29, 2024

@jrumol Hii, any update?

from localize.

jrumol avatar jrumol commented on August 29, 2024

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.

from localize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.