开发者

PHP: Using a string table / resource table for changing brand and i18n

开发者 https://www.devze.com 2023-01-23 17:39 出处:网络
It\'s common on many platforms to, instead of embedding strings directly in the code, make a resource table or string table, which lists all the messages, and have the code reference them.This makes i

It's common on many platforms to, instead of embedding strings directly in the code, make a resource table or string table, which lists all the messages, and have the code reference them. This makes it easy to translate the app, or have a non-developer change the message wording.

What's the recommended way to do this in PHP? Are there any good, simple standard solutions, or is it role-your-own? A simple table is easy - my concern is that the PHP files I have use PHP, H开发者_运维百科TML, and JavaScript all together, and it's not always easy to know where you are...


personally I would not use define's, as stated by Gordon but i would do something like so:

class Language
{
    var $language;
    var $storage = array();

    public function __construct($language)
    {
        $this->language = $language;
        $this->load();
    }

    private function load()
    {
        $location = '/path/to/' . $this->language . '.php';

        if(file_exists($location))
        {
             require_once $location;
             if(isset($lang))
             {
                 $this->storage = (object)$lang;
                 unset($lang);
             }
        }
    }

    public function __get($root)
    {
        return isset($this->storage[$root]) ? $this->storage[$root] : null;
    }
}

So the above would be a very basic language object, and the language file would look like so:

/path/to/english.php

$lang = array(
    'user' => array(
        'welcome' => 'Welcome %s',
        'logout'  => 'Logout',
    )
    /*...*/
);

You should have multiple files for various locales but you should modify your class so that if a key does not exists within German then it should default to the English local as that's the primary

So the usage would be like so.

$lang = 'english'; //logic behind this to detect the browser or user data.

$Language = new Language($lang);

echo sprintf($Language->user->welcome,"RobertPitt"); // Welcome RobertPitt


i18n/l10n dictionaries are simple associative arrays (key-value pairs) and can be accomplished using regular PHP code, .ini files, or database tables (even NoSQL). If the dictionaries aren't that large, your best bet would be PHP files. If the user base are non-coders, consider .ini files. If you foresee the dictionary phrases growing over time (and might not fit available PHP memory), the DB option is the best choice. Take your pick.


I've done this in the past with a simple PHP file full of define()d strings.

(english.php)

<?php
define ('MSG_OK_LOGIN', 'You have successfully logged in. Welcome back');
define ('MSG_ERR_LOGIN', 'Unable to log you in. ')
define ('MSG_ERR_LOGIN_USERNAME', MSG_ERR_LOGIN . 'Please register before attempting to log in');
define ('MSG_ERR_LOGIN_PASSWORD', MSG_ERR_LOGIN . 'Please check that you have typed your password correctly, and that your caps lock key is off. ');
// ...
?>

Then in your login page,

<?php
require ('path/to/your/config.php'); // A configuration for the software.  Assume it contains a define ('CFG_LANG', 'english'); line in it somewhere
require ('path/to/language/files/' . CFG_LANG . '.php');
// ...
?>


I'd recommend you use ICU resources:

See:

  • The ResourceBundle class
  • ICU Resource Management
0

精彩评论

暂无评论...
验证码 换一张
取 消