From 2797e437324b093cb6955b5b06e8a2250e31c7a0 Mon Sep 17 00:00:00 2001 From: dulgan Date: Wed, 23 Jan 2019 17:18:56 +0100 Subject: [PATCH] Use current locale to localize countries names --- .../CountryPickerViewController.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CountryPickerView/CountryPickerViewController.swift b/CountryPickerView/CountryPickerViewController.swift index d27b6de..2ab1eca 100644 --- a/CountryPickerView/CountryPickerViewController.swift +++ b/CountryPickerView/CountryPickerViewController.swift @@ -52,14 +52,14 @@ extension CountryPickerViewController { var header = Set() countriesArray.forEach{ - let name = $0.name + let name = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name header.insert(String(name[name.startIndex])) } var data = [String: [Country]]() countriesArray.forEach({ - let name = $0.name + let name = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name let index = String(name[name.startIndex]) var dictValue = data[index] ?? [Country]() dictValue.append($0) @@ -144,8 +144,9 @@ extension CountryPickerViewController { let country = isSearchMode ? searchResults[indexPath.row] : countries[sectionsTitles[indexPath.section]]![indexPath.row] - - let name = dataSource.showPhoneCodeInList ? "\(country.name) (\(country.phoneCode))" : country.name + + let countryName = Locale.current.localizedString(forRegionCode: country.code) ?? country.name + let name = dataSource.showPhoneCodeInList ? "\(countryName) (\(country.phoneCode))" : countryName cell.imageView?.image = country.flag cell.flgSize = dataSource.cellImageViewSize @@ -232,7 +233,10 @@ extension CountryPickerViewController: UISearchResultsUpdating { indexArray = array } - searchResults.append(contentsOf: indexArray.filter({ $0.name.lowercased().hasPrefix(text.lowercased()) })) + searchResults.append(contentsOf: indexArray.filter({ + let countryName = Locale.current.localizedString(forRegionCode: $0.code) ?? $0.name + return countryName.lowercased().hasPrefix(text.lowercased()) + })) } tableView.reloadData() }