Skip to content

Commit 7148765

Browse files
committed
use current locale for displaying country names
1 parent 17200ab commit 7148765

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Example/MRCountryPicker/ViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ViewController: UIViewController, MRCountryPickerDelegate {
1414
countryPicker.countryPickerDelegate = self
1515
countryPicker.showPhoneNumbers = true
1616
countryPicker.setCountry("SI")
17+
countryPicker.setLocale("sl_SI")
1718
}
1819

1920
func countryPhoneCodePicker(_ picker: MRCountryPicker, didSelectCountryWithName name: String, countryCode: String, phoneCode: String, flag: UIImage) {

MRCountryPicker/Classes/SwiftCountryPicker.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct Country {
2424
open class MRCountryPicker: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {
2525

2626
var countries: [Country]!
27+
open var selectedLocale: Locale?
2728
open weak var countryPickerDelegate: MRCountryPickerDelegate?
2829
open var showPhoneNumbers: Bool = true
2930

@@ -36,10 +37,14 @@ open class MRCountryPicker: UIPickerView, UIPickerViewDelegate, UIPickerViewData
3637
super.init(coder: aDecoder)
3738
setup()
3839
}
39-
40+
4041
func setup() {
4142
countries = countryNamesByCode()
4243

44+
if let code = Locale.current.languageCode {
45+
self.selectedLocale = Locale(identifier: code)
46+
}
47+
4348
super.dataSource = self
4449
super.delegate = self
4550
}
@@ -62,6 +67,10 @@ open class MRCountryPicker: UIPickerView, UIPickerViewDelegate, UIPickerViewData
6267
}
6368
}
6469

70+
open func setLocale(_ locale: String) {
71+
self.selectedLocale = Locale(identifier: locale)
72+
}
73+
6574
open func setCountryByPhoneCode(_ phoneCode: String) {
6675
var row = 0
6776
for index in 0..<countries.count {
@@ -130,7 +139,7 @@ open class MRCountryPicker: UIPickerView, UIPickerViewDelegate, UIPickerViewData
130139
resultView = view as! SwiftCountryView
131140
}
132141

133-
resultView.setup(countries[row])
142+
resultView.setup(countries[row], locale: self.selectedLocale)
134143
if !showPhoneNumbers {
135144
resultView.countryCodeLabel.isHidden = true
136145
}

MRCountryPicker/Classes/SwiftCountryView.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SwiftCountryView: NibLoadingView {
5353
super.init(coder: aDecoder)
5454
}
5555

56-
func setup(_ country: Country) {
56+
func setup(_ country: Country, locale: Locale?) {
5757
if let flag = country.flag {
5858
flagImageView.layer.borderWidth = 0.5
5959
flagImageView.layer.borderColor = UIColor.darkGray.cgColor
@@ -62,8 +62,15 @@ class SwiftCountryView: NibLoadingView {
6262
flagImageView.image = flag
6363
}
6464

65-
countryNameLabel.text = country.name
6665
countryCodeLabel.text = country.phoneCode
66+
67+
68+
if let code = country.code,
69+
let locale = locale {
70+
countryNameLabel.text = locale.localizedString(forRegionCode: code)
71+
}else{
72+
countryNameLabel.text = country.name
73+
}
6774
}
6875

6976
}

0 commit comments

Comments
 (0)