PHP 核心 date() 格式的语言翻译

发布于 2024-10-16 18:40:27 字数 625 浏览 3 评论 0原文

好的,我有一个经常使用的大型且复杂的“即将发生的事件”PHP 模块,我目前正在一个多语言(法语/英语)网站上工作,我需要翻译我的 PHP 日期的语言。

我不想做的是在函数本身中嵌套大量条件 PHP。有人可以帮我想出一系列条件语句来检查是否

<?php if (ICL_LANGUAGE_CODE == 'fr') { } ?>

为真,然后在函数处理后简单地更改月份变量。请原谅我的无知,我只是在语法方面有点慢。有人可以告诉我检查特定输出是否得到回显然后翻译它会是什么样子。以下内容不起作用

<?php if (ICL_LANGUAGE_CODE == 'fr') { February == Fevrier } ?>

编辑:

我考虑过使用本地化框架,但是我正在 WordPress 上开发此网站并使用 WPML(wordpress 多语言插件)。这是一个真正的救星。我对于添加复杂的框架犹豫不决。这个是轻量级的吗?我宁愿只翻译我所期望的字符串。唯一的格式是 - Jan 01 // January 1, 2011 ... 日期名称不会出现在网站上,并且不需要其他字符串翻译。

OK, so I have a large and complicated "upcoming events" PHP module that I work with frequently, I'm currently working on a multi-lingual (French / English) site and I need to translate the language of my PHP dates.

What I DO NOT WANT TO DO is nest a ton of conditional PHP within the function itself. Can someone help me to come up with a series of conditionals statements that check if

<?php if (ICL_LANGUAGE_CODE == 'fr') { } ?>

is true and then simply change the month variables after the function has processed. Forgive my ingorance, I'm just a bit slow when it comes to syntax. Can someone tell me what it would look like to check if a specific output is echoed and then translate it. The following is not working

<?php if (ICL_LANGUAGE_CODE == 'fr') { February == Fevrier } ?>

EDIT:

I've considered using a localization framework, however I'm developing this site on WordPress and using the WPML (wordpress multi-lingual plugin). It's been a real life-saver. I'm hesitant to add complex frameworks. Is this one lightweight? I'd prefer to just translate the strings that I know to expect. The only formats are - Jan 01 // January 1, 2011 ... day names don't appear on the site and other string translations would be unnecessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画中仙 2024-10-23 18:40:27

使用字符串创建数组,并使用每种受支持语言的键:

$months = array(
    1=>array(
        'fr'=>'Janvier',
        'en'=>'January'
    ),
    2=>array(
        'fr'=>'Février',
        'en'=>'February'
    ),
    3=>array(
        'fr'=>'Mars',
        'en'=>'March'
    ) // and so on...
);

$days = array(
    1=>array(
        'en'=>'Monday',
        'fr'=>'Lundi'
    ),
    2=>array(
        'en'=>'Tuesday',
        'fr'=>'Mardi'
    ) // and so on...
);

然后您可以像这样访问字符串:

$days[$day_of_week][ICL_LANGUAGE_CODE]
$days[$month][ICL_LANGUAGE_CODE]

...等等。

Create arrays with your strings, with keys for each supported language:

$months = array(
    1=>array(
        'fr'=>'Janvier',
        'en'=>'January'
    ),
    2=>array(
        'fr'=>'Février',
        'en'=>'February'
    ),
    3=>array(
        'fr'=>'Mars',
        'en'=>'March'
    ) // and so on...
);

$days = array(
    1=>array(
        'en'=>'Monday',
        'fr'=>'Lundi'
    ),
    2=>array(
        'en'=>'Tuesday',
        'fr'=>'Mardi'
    ) // and so on...
);

Then you can access the strings like this:

$days[$day_of_week][ICL_LANGUAGE_CODE]
$days[$month][ICL_LANGUAGE_CODE]

...and so on.

℉服软 2024-10-23 18:40:27

您上面得到的建议都是有效的并且效果很好。

WPML 包含一个字符串翻译工具,它也可以做到这一点并保持代码整洁。优点是您的代码将包含一种语言的文本,并且翻译将在 WPML 的字符串翻译编辑器中完成。

如果您选择添加更多语言,则无需更改代码中的任何内容。只需在 WPML 中添加更多翻译即可。

看看这里:
http://wpml.org/documentation/ support/translation-for-texts-by-other-plugins-and-themes/

您需要使用 *icl_register_string* 来注册可翻译字符串。然后输出的时候,通过*icl_t*。此机制与 GetText 类似,不同之处在于它同时支持静态和动态文本。

如果您这样做,我建议您为字符串选择一个新的上下文字段。这将使您可以轻松地在字符串翻译编辑器中找到它们。

WPML 缓存结果并将具有相同上下文的所有字符串一起加载,因此您不会感到任何性能受到影响。

The suggestions you got above are all valid and would work fine.

WPML includes a String Translation facility which can also do this and keep the code clean. The advantage would be that your code will include texts in one language and the translation will be done in WPML's String Translation editor.

If you ever choose to add more languages, you don't need to change anything in the code. Just add more translations in WPML.

Have a look here:
http://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/

You'll need to use *icl_register_string* to register translatable strings. Then, when outputting, pass through *icl_t*. This mechanism is similar to GetText, with the exception that it supports both static and dynamic texts.

If you do this, I suggest that you choose a new context field for your strings. This would make it easy to locate them in the strings translation editor.

WPML caches results and loads all strings with the same context together, so you're not going to feel any performance hit by this.

驱逐舰岛风号 2024-10-23 18:40:27

这是我在 WordPress 中用来快速将日期翻译为法语的方法:

<?php setlocale(LC_TIME, "fr_FR"); ?>      
<?php echo utf8_encode(strftime("%e %B, %G", strtotime(the_date('','','',FALSE)))); ?>  

This is what I use to quickly translate date to french in wordpress:

<?php setlocale(LC_TIME, "fr_FR"); ?>      
<?php echo utf8_encode(strftime("%e %B, %G", strtotime(the_date('','','',FALSE)))); ?>  
小…楫夜泊 2024-10-23 18:40:27

如果你的项目负载不高,可以尝试这种方法,它足够灵活:

function t_date($format, $date=FALSE, $lang=LANG)
{
    $months['January'] = array('uz'=>'Yanvar');
    $months['February'] = array('uz'=>'Fevral');
    $months['March'] = array('uz'=>'Mart');
    $months['April'] = array('uz'=>'Aprel');
    $months['May'] = array('uz'=>'May');
    $months['June'] = array('uz'=>'Iyun');
    $months['July'] = array('uz'=>'Iyul');
    $months['August'] = array('uz'=>'Avgust');
    $months['September'] = array('uz'=>'Sentabr');
    $months['October'] = array('uz'=>'Oktabr');
    $months['November'] = array('uz'=>'Noyabr');
    $months['December'] = array('uz'=>'Dekabr');

    $days['Monday'] = array('uz'=>'Dushanba');
    $days['Tuesday'] = array('uz'=>'Seshanba');
    $days['Wednesday'] = array('uz'=>'Chorshanba');
    $days['Thursday'] = array('uz'=>'Payshanba');
    $days['Friday'] = array('uz'=>'Juma');
    $days['Saturday'] = array('uz'=>'Shanba');
    $days['Sunday'] = array('uz'=>'Yakshanba');

    if ($date)
        $date = date($format, strtotime($date));
    else
        $date = date($format, time());

    foreach ($months as $key => $val)
        $date = str_replace($key, $val[$lang], $date);
    foreach ($days as $key => $val)
        $date = str_replace($key, $val[$lang], $date);

    return $date;
}

Try this approach if your project is not highload, which is enough flexible:

function t_date($format, $date=FALSE, $lang=LANG)
{
    $months['January'] = array('uz'=>'Yanvar');
    $months['February'] = array('uz'=>'Fevral');
    $months['March'] = array('uz'=>'Mart');
    $months['April'] = array('uz'=>'Aprel');
    $months['May'] = array('uz'=>'May');
    $months['June'] = array('uz'=>'Iyun');
    $months['July'] = array('uz'=>'Iyul');
    $months['August'] = array('uz'=>'Avgust');
    $months['September'] = array('uz'=>'Sentabr');
    $months['October'] = array('uz'=>'Oktabr');
    $months['November'] = array('uz'=>'Noyabr');
    $months['December'] = array('uz'=>'Dekabr');

    $days['Monday'] = array('uz'=>'Dushanba');
    $days['Tuesday'] = array('uz'=>'Seshanba');
    $days['Wednesday'] = array('uz'=>'Chorshanba');
    $days['Thursday'] = array('uz'=>'Payshanba');
    $days['Friday'] = array('uz'=>'Juma');
    $days['Saturday'] = array('uz'=>'Shanba');
    $days['Sunday'] = array('uz'=>'Yakshanba');

    if ($date)
        $date = date($format, strtotime($date));
    else
        $date = date($format, time());

    foreach ($months as $key => $val)
        $date = str_replace($key, $val[$lang], $date);
    foreach ($days as $key => $val)
        $date = str_replace($key, $val[$lang], $date);

    return $date;
}
dawn曙光 2024-10-23 18:40:27

将 php 语言更改为 wp

if (ICL_LANGUAGE_CODE == 'fr') {
 setlocale(LC_ALL, 'fr_FR');
} else if (ICL_LANGUAGE_CODE == 'de') {
 setlocale(LC_ALL, 'de_DE');
} else if (ICL_LANGUAGE_CODE == 'it') {
 setlocale(LC_ALL, 'it_IT');
} else if (ICL_LANGUAGE_CODE == 'en') {
 setlocale(LC_ALL, 'en_EN');
}

时间字符串

$translated_date = strftime("%e %B %Y",strtotime($post->post_date));

安全编码

$translated_date = utf8_encode($translated_date);

Changing php language to wp

if (ICL_LANGUAGE_CODE == 'fr') {
 setlocale(LC_ALL, 'fr_FR');
} else if (ICL_LANGUAGE_CODE == 'de') {
 setlocale(LC_ALL, 'de_DE');
} else if (ICL_LANGUAGE_CODE == 'it') {
 setlocale(LC_ALL, 'it_IT');
} else if (ICL_LANGUAGE_CODE == 'en') {
 setlocale(LC_ALL, 'en_EN');
}

String from time

$translated_date = strftime("%e %B %Y",strtotime($post->post_date));

Safe encoding

$translated_date = utf8_encode($translated_date);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文