In a database we have ISO 3166-1 alpha-2 country codes, e.g. GB for United Kingdom and AR for Argentina. Today we keep a database table with one column for these country codes and one for corresponding English country names.
We work in PHP (5.4.x) which has the intl extenion (wrapper for ICU library) so I thought I'd like to fetch the country names from this source which makes the application more maintainable and easier to internationalize as I easily can fetch country names in German, French or whatever we want in the future.
Locale::getDisplayRegion ( string $locale [, string $in_locale ] ) provides a region name from a locale.
E.g.
php > echo Locale::getDisplayRegion('en-US', 'de') . PHP_EOL;
Vereinigte Staaten
php > echo Locale::getDisplayRegion('en-US', 'en') . PHP_EOL;
United States
The problem is that I do not have the language code, only the country code, so I'm not sure how to build up a proper locale.
It seems like I can get the region names by just prefixing with 'en_' for English, e.g
php > echo Locale::getDisplayRegion('en-CH', 'en') . PHP_EOL;
Switzerland
php > echo Locale::getDisplayRegion('en-CN', 'en') . PHP_EOL;
China
php > echo Locale::getDisplayRegion('en-AR', 'en') . PHP_EOL;
Argentina
However, this feels more like a hack and I'm not sure I really can rely on that?
I was thinking if I could somehow compose a locale using the country code (region / territory depending on terminology) and any of the valid languages for the country, but it appears it doesn't work as far as I've found at least:
php > echo Locale::composeLocale( array('region' => 'CN') ) . PHP_EOL;
php >
Any recommendations on how to go about this?
I do not want to maintain my own database of country names or simply use an external PHP library for it when there's a built-in extension which seem to be a good source (ICU) already.
Thanks a lot in advance!
use und (unknown language). So 'und_' . 'AR' etc.
Related
I'm working with i18n in my website with Cakephp 4.
My routes use languages with 2 characters (fr, en, it, etc.)
// Route For example
$builder
->connect(
'/{lang}/{controller}/{action}/*',
[]
)
->setPatterns([
'lang' => 'fr|en|it'
])
->setPersist(['lang']);
As the country is not defined, when I do :
echo __(
'Bonjour {0}, votre solde au {1,date} est de {2,number,currency}',
['Olivier', \Cake\I18n\FrozenTime::now(), 1354.37]
);
The currency is ¤ instead of €.
So I've set language with country FR in my AppController :
if($this->request->getParam('lang'))
I18n::setLocale($this->request->getParam('lang') . '_FR');
The aim is to have € whatever the language.
Thus, it generates fr_FR, en_FR, it_FR.
Is it correct to do that or is it ugly ?
If someone could enlighten me...
¤ (U+00A4) is the character used to denote an unspecified currency, this is the expected behavior when not supplying a region alongside the language.
Instead of using a specific region you could use the currency keyword to specify the currency independently:
// eg en#currency=EUR
$locale = "{$this->request->getParam('lang')}#currency=EUR";
I'm not sure though if the translate behavior will work properly with it, you might have to set the locale on the table instance manually so that it doesn't contain any keywords.
See also
ICU Documentation > Locales and Resources > The Locale Concept > Keywords
Cookbook > Database Access & ORM > Behaviors > Tree
When i search using below FQ API :
https://api.foursquare.com/v2/venues/search?ll=19.346523,-99.191292&query=gimn&oauth_token=Z1NASAZ5IEUSDJAJG1VMQO5YX410DJXUKBKAHXN0FYIB15BQ&v=20150401
In browser we got the City name as "Mexico City".
But, when i used this API call in file_get_contents or curl, city name is returning as "Ciudad de México" in spanish language.
I need City name in English, Pls provide me the steps to fix this issue.
Thanks,
Suhanya.M
It's defaulting to Spanish, as it's the most popular for that locale. You can specify locale=en for English:
https://developer.foursquare.com/overview/versioning
Here's what the new link would look like; note the additional parameter at the very end:
https://api.foursquare.com/v2/venues/search?ll=19.346523,-99.191292&query=gimn&oauth_token=Z1NASAZ5IEUSDJAJG1VMQO5YX410DJXUKBKAHXN0FYIB15BQ&v=20150401&locale=en
What is the best way of returning results from a mysql database search where place names are entered in different languages? The case in question is the following:
Entries are made in a database with a location field. The location field uses google places autocomplete to attempt to ensure consistent and correctly spelled locations. However users can both enter and search the database for locations in multiple languages. An example problem case is that Sao Paulo, Brazil returns 4 results whereas Sao Paulo, Brasil returns 20 results. The correct behavior we need is that either spelling should return all 24 results.
The search query (using joomla syntax) is currently:
// Filter By buyer's destination //
$destination = $jinput->get('destination', '', 'string');
if($destination!=''){
$destination = $db->Quote('%' . $db->escape($destination, true) . '%');
$query->where('a.deliverydestination LIKE '.$destination.' AND a.status="Collecting Bids"');
$query->order('a.created DESC');
}
I need to get all countries with geonames api. I can use $geonames->genomes[2]->countryName to get country name of 2nd. But I want it auto like while() how can I use it with while() in php?
JSON IS HERE
{"geonames":[{"countryName":"Andorra","currencyCode":"EUR","fipsCode":"AN","countryCode":"AD","isoNumeric":"020","north":42.65604389629997,"capital":"Andorra la Vella","continentName":"Europe","areaInSqKm":"468.0","languages":"ca","isoAlpha3":"AND","continent":"EU","south":42.42849259876837,"east":1.7865427778319827,"geonameId":3041565,"west":1.4071867141112762,"population":"84000"},{"countryName":"United Arab Emirates","currencyCode":"AED","fipsCode":"AE","countryCode":"AE","isoNumeric":"784","north":26.08415985107422,"capital":"Abu Dhabi","continentName":"Asia","areaInSqKm":"82880.0","languages":"ar-AE,fa,en,hi,ur","isoAlpha3":"ARE","continent":"AS","south":22.633329391479492,"east":56.38166046142578,"geonameId":290557,"west":51.58332824707031,"population":"4975593"}]}
We have a CSV file that has all the countries in the world, their corresponding ISO codes, and their short and formal names in English, French, Spanish, Russian, Chinese, Arabic, and Italian. This was compiled from various government and international references:
http://www.opengeocode.org/download/countrynames.txt
References are cited at:
http://www.opengeocode.org/download.php#countrynames
You can use a for loop:
for ($i = 0; $i < $numCountries; $i++) {
echo $geonames->geonames[$i]->countryName;
}
I have a Postgre DB and one of these values are money (type, ofc).
When I do the Select using PHP, the output of that query is in this format:
Example: €1,00
I'm looking for something to change this format to this other:
Example: 1,00€.
What can I change in my query to get last one result?
Thanks!
Fastest way in PHP is to use str_replace :)
$cash = '€1,00';
$cash = str_replace('€','',$cash) . '€';
echo $cash;
But a better option is to change your DB field type to some numeric type and just to add the ' €' sign where you want it..
You may do this replace inside your DB SELECT statement but I think that its better to do that inside your PHP..
If its stored in database just like a number without currency you could just use function number_format($x,y,'.','')
where
$x is your number
y is number of numbers behind decimal separator
'.' - means decimal separator is .
'' -means there is no separator between thousands
so u just format your number in a way u want and add "euro" sign.
if its price there is a function called money format :
u must use it with locales of country the currency u want fe. :
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%.2n', $your_variable);
this means u use currency of germany. '%.2n' means your output is from $yourvariable and have 2 decimalnumbers
And to the thing of 'euro' sign after or before number. It depends of locales. I think slovak or spanish has euro sign after number.
Try it.
This should provide you what you need, and be flexible across all currencies:
$value = '$4265';
if (!is_numeric($value[0])) {
$value = substr($value, 1) . $value[0];
}