My multilingual website with only basic php (without zend_translate, gettext, etc…) will I have problems in the future?

懵懂的女人 提交于 2019-12-02 04:44:50
Gordon

First off all, it will not work with your code. You would have to use

define('GREETING', 'Hello World').

Check the PHP manual for define.

Second, using contants for this is a horrible idea. You are littering the global namespace with tons of constants and risk constant nameclashing. See the Userland Naming Guide.

If you do not want to use Zend_Translate (you don't have to use the entire framework for this) and cannot use gettext, I suggest you use arrays for storing the translations, e.g. something like this:

$lang = array(
    'greeting'  => 'Hello World'
    'something' => 'else'
);

and then you can use it like this in your template:

<h1><?php echo $lang['greeting'] ?></h1>

This way, you only have to make sure, $lang is not already defined in the global scope.

Some people prefer to use the default language instead of translation ids, e.g. they prefer to write

<h1><?php echo t('Hello World') ?></h1>

where t would function mapping the input string to the output string. The translation array would have to contain the full sentences then and map these to the other languages, e.g.

$lang = array(
    'Hello World' => 'Hola Mundo'
);

But of course, you could just access this with $lang['Hello World'] as well. It just gets awkward for long strings. Many translation functions allow you to pass in additional params though, to allow for something like this:

$lang = array(
    'currentTime' => 'The current time is %s'
);

<h1><?php echo t('currentTime', date('H:i:s')) ?></h1>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!