I've got a project that is hugely over budget and I'm trying to find the quickest possible solution to this. Basically, I have a multi-lingual site that is outputting dates in the following formats in English:
- Jan 01
- January 01, 2011
Obviously there are 12 options with each month, however I only need to translate months, not days or days of the week. Also, since I'm echoing these dates initially from PHP, the resulting HTML is 100% predictable, there will only be 24 possible returned values - 12 for Jan, Feb, Mar and 12 for January, February, March... etc.
I've already been told about the PHP strotime()
and setlocale()
. I know it might be slightly janky, but is there a way to just take the PHP strings which are being spit out and translate them directly after the fact using jQuery. What I would do is wrap the script in a PHP conditional that checks for if (ICL_LANGUAGE_CODE == 'fr') {}
If there is a VERY simple way to do this through PHP I'm open to it but so far I've gotten responses from the community but they are all too time-consuming and in many cases fail to consider other variables I'm not explaining here for brevity's sake.
Also, for clarity's sake, I will say that I AM using jQuery and it's already loaded on the site.
EDIT
I'm not concerned about the date syntax for this, for example, it's possible for me to just do开发者_JAVA技巧 this: if (English) { date('F j, Y');} else (FR) { date('j').' '.date('F').', '.date('Y') }
So, my only problem is translating the month names. Also, this is for "upcoming events" so I'm NOT trying to echo the current date/time, but a future one with a PHP timestamp.
Try this:
Inside of mons.txt:
EN, Jan, Feb, Mar, Apr, May, June, July, Aug, Sept, Oct, Nov, Dec
FR, janv, fevr, mars, avril, mai, juin, juil, aout, sept, oct, nov, dec
DE, Jan, Feb, Marz, Apr, Mai, Juni, Juli, Aug, Sept, Okt, Nov, Dez
IT, genn, febbr, mar, apr, magg, giugno, luglio, ag, sett, ott, nov, dic
ES, enero, feb, marzo, abr, mayo, jun, jul, agosto, set, oct, nov, dic
NL, Jan, Feb, Mar, Apr, Mei, Juni, Juli, Aug, Sept, Oct, Nov, Dec
Inside of months.txt:
EN, January, February, March, April, May, June, July, August, September, October, November, December
FR, janvier, fevrier, mars, avril, mai, juin, juillet, aout, septembre, octobre, novembre, decembre
DE, Januar, Februar, Marz, April, Mai, Juni, Juli, August, September, Oktober, November, Dezember
IT, gennaio, febbraio, marzo, aprile, maggio, giugno, luglio, agosto, settembre, ottobre, novembre, dicembre
ES, enero, febrero, marzo, abril, mayo, junio, julio, agosto, septiembre, octubre, noviembre, diciembre
NL, Januari, Februari, Maart, April, Mei, Juni, Juli, Augustus, September, Oktober, November, December
Inside of days.txt:
EN, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
FR, lundi, mardi, mercredi, jeudi, vendredi, samedi, dimanche
DE, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag, Sonntag
IT, lunedi, martedi, mercoledi, giovedi, venerdi, sabato, domenica
ES, lunes, martes, miercoles, jueves, viernes, sabado, domingo
NL, Maandag, Dinsdag, Woensdag, Donderdag, Vrijdag, Zaterdag, Zondag
To translate:
$date = date('l, M n, Y', strtotime('December 13th, 2012'));
$date = translate($date, 'IT');
var_dump($date);
function tmap($maps = array()) {
$tmap = array();
$defaultMaps = array('mons', 'months', 'ordinals', 'days', 'formats');
$maps += $defaultMaps;
foreach ($maps as $map) {
$mapfile = "$map.txt";
if (file_exists($mapfile)) {
$file = file($mapfile);
foreach ($file as $line) {
$translations = str_getcsv($line);
$locale = array_shift($translations);
$tmap[$map][$locale] = $translations;
}
}
}
return $tmap;
}
function translate($string, $to, $classes=array(), $base='EN', $tmap=array()) {
if (empty($tmap)) $tmap = tmap();
if (is_string($classes)) $classes = array($classes);
if (empty($classes)) {
$classes = array_keys($tmap);
}
foreach ($classes as $class) {
$string = str_ireplace($tmap[$class][$base], $tmap[$class][$to], $string);
}
return $string;
}
To parse or display try it http://code.google.com/p/datejs/ Handling localization and have many types of configuration setup.
精彩评论