php 翻译月份名称的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12595073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to translate month names
提问by xdazz
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 'Sentyabr'. Can set_locale() function can do this. By the way it is Turkmen Language.
thank you.
我正在使用 Codeigniter!翻译月份名称的最佳方法是什么。我在 mysql 时间戳中有一个这样的日期:
2012-09-22 11:21:53
我可以'September 22, 2012'使用 phpdate()函数将其转换为。我想重命名'September'为'Sentyabr'. 可以 set_locale() 函数可以做到这一点。顺便说一下Turkmen Language。谢谢你。
OK , i created some php file to check it indivudally like this:
好的,我创建了一些 php 文件来单独检查它,如下所示:
cool.php
酷.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
我需要用我的语言在九月返回
回答by CoursesWeb
You can use str_ireplace()with arrays for names in english, and equivalent for names in turkmen, in same order.
您可以将str_ireplace()数组用于英文名称,也可以用于土库曼文名称的等效数组,顺序相同。
Something like this:
像这样的东西:
$nmeng = array('january', 'february', ..);
$nmtur = array('ocak', 'subat', ...);
$dt = 'September 22, 2012';
$dt = str_ireplace($nmeng, $nmtur, $dt);
回答by xdazz
Use setlocale()and strftime...
使用setlocale()和strftime...
On linux run $ locale -ato find out if the needed locale is installed and to grab its name value string.
在 linux 上运行$ locale -a以找出是否安装了所需的语言环境并获取其名称值字符串。
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 %eto %d(%edoes not work in windows).
如果您使用 Windows 服务器,则更%e改为%d(%e在 Windows 中不起作用)。
echo strftime('%B %d, %Y', strtotime('2012-09-22 11:21:53'));
For windows, check here to see supported format.
对于 Windows,请在此处查看支持的格式。
回答by umefarooq
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 libraryyou can use in you project over and over if you have dual language site.
翻译月份名称以将它们放入语言文件中的最佳方法,并像阅读此处一样调用它如何使用 codeigniter语言库中的语言文件,如果您有双语言站点,您可以在项目中反复使用。
$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';
回答by seangates
Add this to the index.php file near the top:
将此添加到靠近顶部的 index.php 文件中:
/* Set locale to Turkmen */
setlocale(LC_ALL, 'tk');
Documentation for this is here: http://php.net/manual/en/function.setlocale.php
文档在这里:http: //php.net/manual/en/function.setlocale.php
Also, @xdazz is correct that you should combine it with strftimeinstead of the date() function.
此外,@xdazz 是正确的,您应该将它与strftime而不是 date() 函数结合使用。
You may need to confirm that "tk" is used for Turkmen.
您可能需要确认“tk”用于土库曼语。

