8

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

  • A way to translate weekdays and months from or into any language (provided the language is installed on the php server), is to be found at http://stackoverflow.com/questions/34465351/translating-php-weekdays-and-months/43577566#43577566 – Roemer Apr 23 '17 at 23:41
  • strftime deprected in php8 – f b Dec 16 '21 at 13:57

4 Answers4

14

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);
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27
11

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.

Francisco Luz
  • 2,775
  • 2
  • 25
  • 35
xdazz
  • 158,678
  • 38
  • 247
  • 274
2

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';
umefarooq
  • 4,540
  • 1
  • 29
  • 38
  • i have used the same thing for dual language site has arabic and english and it works really great. you have to do little effort to put translation in both files – umefarooq Sep 26 '12 at 06:23
  • how do i replace date('%F') %F to $this->lang->line('September') –  Sep 26 '12 at 06:30
  • $this->lang->line(date('%F')) will work you have to pass month name as parameter and it will return translation remember $lang key name should be same as string you are passing – umefarooq Sep 26 '12 at 06:36
0

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.

seangates
  • 1,467
  • 11
  • 28
  • I have this :echo strftime( "F d Y",strtotime($post->entry_date)); It returns just " F d Y" –  Sep 26 '12 at 05:57
  • It still returns September,not in my language –  Sep 26 '12 at 06:03
  • Have you confirmed that "tk" is the string for Turkmen? I may have it incorrect (cursory Google search). – seangates Sep 26 '12 at 06:27
  • Also, did you add setlocale() to your site's index.php file? – seangates Sep 26 '12 at 06:32
  • yes i added it to index.php file –  Sep 26 '12 at 06:35
  • Well, doing with strings (the answer you selected) is not functional if you always have to make sure the output is set to the proper locale. If you can make sure your PHP instance supports `setlocale()` then you won't ever have to worry about the translation being done for your output. – seangates Sep 26 '12 at 14:31