Best way to translate month names [closed]

2019-06-24 07:42发布

问题:

I am using Codeigniter! What will be the best way to translate month names. I have a date in mysql timestamp like this: 2012-09-22 11:21:53
I can convert it to 'September 22, 2012' with php date() function. I want to rename 'September' to 'Sentýabr'. Can set_locale() function can do this. By the way it is Turkmen Language. thank you.


OK , i created some php file to check it indivudally like this:

cool.php

<?php setlocale(LC_ALL,'ru_RU');
echo strftime('%B',time());
?>

Returns September. What might be the problem.


I have

  /* Set locale to Turkmen */
  setlocale(LC_ALL, 'tk');

And

  echo strftime( '%B',time());

Result: returns September

I need it return September in my language

回答1:

You can use str_ireplace() with arrays for names in english, and equivalent for names in turkmen, in same order.

Something like this:

$nmeng = array('january', 'february', ..);
$nmtur = array('ocak', 'subat', ...);
$dt = 'September 22, 2012';
$dt = str_ireplace($nmeng, $nmtur, $dt);


回答2:

Use setlocale() and strftime ...

On linux run $ locale -a to find out if the needed locale is installed and to grab its name value string.

setlocale(LC_TIME, 'ru_RU');
echo strftime('%B %e, %Y', strtotime('2012-09-22 11:21:53'));

Edit:

If you use a windows server, then change %e to %d (%e does not work in windows).

echo strftime('%B %d, %Y', strtotime('2012-09-22 11:21:53'));

For windows, check here to see supported format.



回答3:

best way to translate month names to put them in language file and call it like read here how to use language files in codeigniter Language library you can use in you project over and over if you have dual language site.

$this->lang->line('September');

your english language file 
$lang['january'] = 'january';
$lang['february'] = 'february';
...
$lang['September'] = 'September';

your turkish language file 

$lang['january'] = 'ocak';
$lang['february'] = 'subat';
...
$lang['September'] = 'Eylül';


回答4:

Add this to the index.php file near the top:

/* Set locale to Turkmen */
setlocale(LC_ALL, 'tk');

Documentation for this is here: http://php.net/manual/en/function.setlocale.php

Also, @xdazz is correct that you should combine it with strftime instead of the date() function.

You may need to confirm that "tk" is used for Turkmen.