I'm new with Core Data, and I don't know how to proceed to do what I want.
Actually, I would like to make a model with countries and country codes.
BUT: the countries are localized (3 languages for now).
So, here is my model at this time:
Entity: Country
Attributes:
- name_en (String)
- name_nl (String)
- name_fr (string)
Now, I would like associate country codes for each country.
Example:
- Belgium => BE
- United States => USA
- France => FR
etc. but it has to work in French and Dutch too.
I don't know how to link this country codes to name_en, name_nl and name_fr.
Important: The countries are at the same index in French, English or Dutch:
Belgium (en) => index 3
België (nl) => index 3
Belgique (fr) => index 3
Hope I've been clear ;)
Thank you guys.
I would advise to use the NSLocale APIs to cover most of this logic. That would greatly simplify your data structure and instead put the complexity where it belongs - and where Apple can do the heavy lifting for you.
The model would be as simple as this: Entity Country with a string attribute countryCode, which is the string used by Apple to identify each country. These are strings like
en_US
fr_BE
nl_BE
nl_NL
fr_FR
If you do not want duplicate entries for the same country with different language region, you could split them into Country and Language and construct the identifier yourself.
You can generate all the rest with Apple's APIs. E.g., the country code
Country *belgium; // assume localeIdentifier "fr_BE" for French
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:belgium.localeIdentifier];
NSString *belgiumCountryCode = [locale objectForKey:NSLocaleCountryCode];
// --> "BE"
NSString *nameOfBelgiumInFrench =
[locale displayNameForKey:NSLocaleCountryCode value:belgiumCountryCode];
// --> "Belgique"
NSLocale *usLocale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
NSString nameOfBelgiumInEnglish =
[usLocale displayNameForKey:NSLocaleCountryCode value:belgiumCountryCode];
// --> "Belgium"
You can put those cumbersome methods into convenience accessors of your managed object subclasses. Theoretically you could also hard-code them, but I would strongly recommend against it.
One last thing: if you are planning on using Core Data, forget about indexes.
Related
let filter = GMSAutocompleteFilter()
filter.type = GMSPlacesAutocompleteTypeFilter.city
filter.country = "USA" || "CA"
how to get 2 country results in google autocomplete
If you check country property in GMSAutocompleteFilter class then you will found that it is string so I think you can't set multiple country in your filter. If it's allowing multiple country then it should be array or any collection instead of string! and in documentation also they haven't mentioned for multiple countries in filter! So, I think you can't set more than one country at a time in one filter!
It's currently not possible. I would recommend using the web API, where you can send up to 5 ISO country codes in your query (see components under "optional parameters":
https://developers.google.com/places/web-service/autocomplete
It's possible to do using following syntax
filter.country = "CA|country:US"
reference link here.
I have an iOS app where I want to include the user's country (as defined by region) in a text string, e.g. “In Portugal this is …”. For most country names this is easy:
let countryCode = locale.objectForKey(NSLocaleCountryCode) as! String
let countryName = locale.displayNameForKey(NSLocaleCountryCode,
value: countryCode)!
let text = "In \(countryName) this is …"
However, for some country names, the name should be prefixed by “the”, for example “the Netherlands” and “the United States”. Is this information available in NSLocale somehow, or are there other libraries that can help with this?
I'd like to check if the country name provided belongs to Europe.
Does anyone know of a list of european countries that can be used in a Ruby project?
I'd like to do something like this:
spain = Country.named('Spain')
spain.parent # => 'Europe'
japan = Country.named('Japan')
japan.parent # => 'Asia'
https://github.com/jim/carmen/ lets me list subregions of a country, but not a country's parent.
Try that gem if you really need whole gem for this. You should be able to do:
Country.find_all_countries_by_region('Europe')
to get countries in Europe.
Check out the Wikipedia article for a complete list. http://en.wikipedia.org/wiki/List_of_European_countries
In an app, I am displaying the details of the contacts using ABRecordRef. I am using the keys kABPersonAddressCityKey, kABPersonAddressStateKey, kABPersonAddressZIPKey, kABPersonAddressCountryKey. Everything works fine. But I don't know in which format to display the addresses. What I mean is, if you see the Contacts app, the addresses are displayed in a particular format for different countries. Some examples,
US
Street
City State ZIP
Country
India
Street
Province
City PIN
Country
Australia
Street
Suburb State ZIP
Country
Now I don't know how to find the format for different countries.
1.Is there any way to find the address format based on country/country codes?
2.Is there a way we can get the fully formatted address using a single function, like we use ABRecordCopyCompositeName() to get the full name?
From iOS 9.0 / macOS 10.11 / watchOS 2.0 on you should use CNPostalAddressFormatter instead:
The CNPostalAddressFormatter class formats the postal address in a contact. This class handles international formatting of postal addresses.
Below code is in Swift 3 but it is trivial to convert it to Objc
let postalAddress = CNMutablePostalAddress()
postalAddress.street = street
postalAddress.postalCode = zipCode
postalAddress.city = city
postalAddress.state = state
postalAddress.country = country
postalAddress.isoCountryCode = countryCode
let formattedAddress = CNPostalAddressFormatter.string(from: postalAddress, style: .mailingAddress)
Make sure you set the ISO country code property, this is used to determine the format of the address.
Example:
postalAddress.street = "Main Street 1"
postalAddress.postalCode = "67067"
postalAddress.city = "Ludwigshafen"
postalAddress.state = "Rhineland-Palatinate"
postalAddress.country = "Germany"
postalAddress.isoCountryCode = "DE"
leads to this
Main Street 1
67067 Ludwigshafen
Germany
whereas
postalAddress.isoCountryCode = "US"
leads to
Main Street 1
Ludwigshafen Rhineland-Palatinate 67067
Germany
Try this link
ABCreateStringWithAddressDictionary
It looks like you need to use: ABCreateStringWithAddressDictionary that will return:
Returns a formatted address from an address property. (From Apple)
Good luck
For my application(Ruby on Rails) i have country select box for the signup page. These countries are localized into different language. But i couldnt find a way to sort them, based on the language in which its localized. At present i have sorted it out based on english only. Is there a way to sort the country names based on the locale? i.e the order of the countries should change(ascending order) according to the language its localised.
Thanks..
You can make a custom String comparison method, based on a given alphabet, something like this (works in Ruby 1.9):
class String
# compares two strings based on a given alphabet
def cmp_loc(other, alphabet)
order = Hash[alphabet.each_char.with_index.to_a]
self.chars.zip(other.chars) do |c1, c2|
cc = (order[c1] || -1) <=> (order[c2] || -1)
return cc unless cc == 0
end
return self.size <=> other.size
end
end
class Array
# sorts an array of strings based on a given alphabet
def sort_loc(alphabet)
self.sort{|s1, s2| s1.cmp_loc(s2, alphabet)}
end
end
array_to_sort = ['abc', 'abd', 'bcd', 'bcde', 'bde']
ALPHABETS = {
:language_foo => 'abcdef',
:language_bar => 'fedcba'
}
p array_to_sort.sort_loc(ALPHABETS[:language_foo])
#=>["abc", "abd", "bcd", "bcde", "bde"]
p array_to_sort.sort_loc(ALPHABETS[:language_bar])
#=>["bde", "bcd", "bcde", "abd", "abc"]
And then provide alphabetical orders for every language you want to support.
Sometime ago twitter released a library which can nicely take care of it in Ruby for multiple language and it actually works https://github.com/twitter/twitter-cldr-rb#sorting-collation . It is also very nice that they provided a higher level way for sorting as well as low-level which can just compare two string for given locale. This allowed me to get rid of git://github.com/k3rni/ffi-locale.git which I was using so far for sorting string in a locale-aware way.
Maybe you can translate all and sort it after this translation.