Using translated version, getting back with function to english countryname for the right country_id
Summit - June 11, 2009 - 14:42
| Project: | Country codes API |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
Hi,
Great the dutch translation is there: http://drupal.org/node/382828
It is great that on screen the dutch country names are shown.
Great it is possible to get from countryname to countryid (http://drupal.org/node/487296)
But...to use location and to get to country_id's I need to get back from the dutch to the exact english iso country name.
How can I get the Exact english countryname using also in general the dutch translation?
Is there a function for this in the api? Is there another method please?
Thanks a lot in advance for your reply!
Greetings, Martijn

#1
My usecase is the following:
My default language is dutch. And the different country-possibilities will be in dutch "Amerika", "Verenigde staten", "Verenigde staten van Amerika"
they will be changed to the dutch correct translation "Verenigde Staten".
But then to get to the correct country code the english translation "United States" need to be get.
How to get from a dutch to the english message? While my default language is set to dutch and the .po file for country codes api is loaded. How to get to the english version then?
greetings, Martijn
#2
Perhaps with t()?
#3
COuld you explain it with code please? May be I am stupid, but t() is from english to the default language isn't it?
While I want to have the English equivalent, while my default language is set to dutch..?
Thanks for your quick reply!
Greetings, Martijn
#4
I've never done this before, so can't provide the code for you. How about this http://drupal.org/node/152968 - first link on google when I type in drupal translate string!... looks promising no?
#5
Hi, Yes I have seen this post, and lot's of others, but no answer yet.
On this post I can set the $langcode to english: http://drupal.org/node/364841, but how to go further..
Localizer is not on D6,
EDIT may be t() has the answer. it seems looking at http://drupal.org/node/82499 t($string, $vars, $langcode) could work..
greetings, Martijn
#6
Can you use the locale() function?
<?php/**
* Provides interface translation services.
*
* This function is called from t() to translate a string if needed.
*
* @param $string
* A string to look up translation for. If omitted, all the
* cached strings will be returned in all languages already
* used on the page.
* @param $langcode
* Language code to use for the lookup.
* @param $reset
* Set to TRUE to reset the in-memory cache.
*/
function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
...
?>
EDIT: Actually, I guess this works from English strings only too :(
#7
may be t() has the answer. it seems looking at http://drupal.org/node/82499 t($string, $vars, $langcode) could work..will investigate further..
#8
i think it should be possible using t(), don't know how yet..see: http://drupalcontrib.org/api/function/t/6
$langcode Optional language code to translate to a language other than what is used to display the page.#9
Actually, I don't think so... from the t() documentation page:
#10
My usecase.
1) I want first all sort of posiibilities be placed back to the original Dutch translation of the country_name. Say for instanse "Verenigde Staten" <= the dutch translation of "United States".
2) Then I want to get to the english original for this particular country, while the Iso-countrycodes are matched with the english equivalent.
Wouldn't something like this not work for 2):
<?php$dutch_country = "Verenigde Staten"';
$english_country = t($dutch_country,'en') ;
$iso2 = db_result(db_query("select iso2 from {countries_api_countries} where name = '%s", $english_country));
?>
#11
I don't think so since the first parameter to t() is supposed to be an English string. However, that aside there are still a couple of other errors in your code and it would actually be either
<?php$dutch_country = "Verenigde Staten";
$english_country = t($dutch_country,'en');
$iso2 = db_result(db_query("select iso2 from {countries_api_countries} where printable_name = '%s", $english_country);
?>
note: I'm using printable_name rather than name in the lookup since in countries_api_countries:
name = 'UNITED KINGDOM'
printable_name = 'United Kingdom'
ALSO, another problem with your code, assuming that t() would do the job (which it wont) - it's the third parameter of t() that is the lang code... so it would be
t($dutch_country, NULL, 'en');. Or better still, I think you would use locale() directly as inlocale($dutch_country, 'en');.However, since both t() and locale() can only translate from an English string its irrelevant!
#12
So back to the drawing board..how to translate from another language than english, back to english...?
#13
How about doing a manual lookup in the {locales_target} and {locales_source) tables:
<?php$dutch_country = "Verenigde Staten";
$english_country = db_result(dbquery("SELECT source FROM {locales_source} ls JOIN {locales_target} lt ON ls.lid = lt.lid WHERE translation = '%s'"), $dutch_country);
$iso2 = db_result(db_query("select iso2 from {countries_api_countries} where printable_name = '%s", $english_country);
?>
#14
That could may be work. Will try to put the whole code together these days...thanks for all your support till now!
#15
oh, and that should probably include the language code in the lookup too:
<?php$dutch_country = "Verenigde Staten";
$language = 'nl'
$english_country = db_result(db_query("SELECT source FROM {locales_source} ls JOIN {locales_target} lt ON ls.lid = lt.lid WHERE lt.language = '%s' AND lt.translation = '%s'"), $language, $dutch_country);
?>
#16
So for the whole usecase:
1) I want first all sort of posiibilities be placed back to the original Dutch translation of the country_name. Say for instanse "Verenigde Staten" <= the dutch translation of "United States".
2) Then I want to get to the english original for this particular country, while the Iso-countrycodes are matched with the english equivalent.
<?php$dutch_possibilities= array(strtolower("america"), strtolower("verenigde staten"), strtolower("verenigde staten van america"));
$dutch_country= str_replace($dutch_possibilities, "verenigde staten", strtolower($countryname));
$language = 'nl'
$english_country = db_result(db_query("SELECT source FROM {locales_source} ls JOIN {locales_target} lt ON ls.lid = lt.lid WHERE lt.language = '%s' AND lt.translation = '%s'"), $language, $dutch_country);
$iso2 = db_result(db_query("select iso2 from {countries_api_countries} where printable_name = '%s", $english_country);
?>
#17
Shouldn't it not be
<?php$dutch_possibilities= array(strtolower("america"), strtolower("verenigde staten"), strtolower("verenigde staten van america"));
$dutch_country= str_replace($dutch_possibilities, "verenigde staten", strtolower($dutch_country));
$language = 'nl'
$english_country = db_result(db_query("SELECT source FROM {locales_source} ls JOIN {locales_target} lt ON ls.lid = lt.lid WHERE lt.language = '%s' AND lt.translation = '%s'"), $language, $dutch_country);
$iso2 = db_result(db_query("select iso2 from {countries_api_countries} where printable_name = '%s", $english_country);
?>
so dutch_country in str_replace instead of $countryname?
#18
Hi Tom, closing this issue, because I think the further coding is not related to country codes api immidiately.
whole issue is within feedapi_mapper: http://drupal.org/node/489680
Thanks a lot for your help until now!
greetings, Martijn