开发者

Best way to manage text displayed to users in PHP

开发者 https://www.devze.com 2023-02-12 00:07 出处:网络
Ok for sure this has been asked and answered already but i somehow can\'t find a proper tutorial. I want to keep the text displayed to users somewhere else and to prevent my code from becoming too lar

Ok for sure this has been asked and answered already but i somehow can't find a proper tutorial. I want to keep the text displayed to users somewhere else and to prevent my code from becoming too large and unreadable. My site won't be internationalized. I just want to have some kind of file with key-value structure and get the text fro开发者_如何学Cm there. I want to keep the text in files, not in the database as some tutorials suggest. I found a solution which will work but i am not sure whether this is a good approach. I am thinking of using parse_ini_file and to keep my texts in .ini file. Is there something wrong with this approach? Could you suggest something better?


I put all language data in arrays. Its easy and also we can add multi-language support

lang/en.php

<?php 
 return array(  
   'index' => 'Homepage',
   'feedback' => 'Feedback'
   'logout' => 'Logout from profile',
 )
?>

lang/ru.php

<?php 
 return array(
   'logout' => 'Выйти из профиля',
 )
?>

Then we can load languages:

$lang = include('lang/en.php');
if(isset($_GET['lang']))
{
   $lang = array_merge($lang, include('lang/ru.php'));
}

After all it $lang will look like:

Array
(
   [index] => Homepage
   [feedback] => Feedback
   [logout] => Выйти из профиля
)

And we can very simple use it:

function __($name) {
    global $lang;
    return $lang[$name];
}

Somewhere in the site template:

...
<title><?=__('index')?></title>
</head>
<body>
<?=__('feedback')?>


why not use a plain text file with commas or some uncommon character to hold this data? you can read it and parse it into an array with

$file = file_get_contents("/path/to/file");
$lines = explode('\r', $file);
foreach($lines as $line) $message[substr($line, 0, strpos($line, ','))] = substr($line, strpos($line, ','));

then you should have an array like $messages[3] = "No soup for you!";

the file might look like:

1,The site is down.
2,Try again.
3,No soup for you!
4,Signs point to yes.

(I probably have some of the arguments misplaced in those functions - i always forget which is the needle and which the haystack.)


You can process your data in a script. In this script, you call a certain source (e.g. the ini file you suggest). Then you use a template engine. For this engine, you point towards a template file and give the template all the variables.

The template generates the html and inserts the variables at the right place. This way, you keep you php (business logic) code clean, away from the presentation (the template). Also you can manage the variables in one file (ini/xml but this can be something completely different).

For template engines, Smarty is the most known of all. There are also pure php-based template systems, just Google for them to find one that suits your needs.


I do like this:

$defaultLang = array('Home','Logout',etc)
$otherLang=array( 'ru' => array('Home_in_ru','logout_in_ru',etc);

you translate like this:

echo translate('Home');

function is:

function translate($msg) {
    if ($_GET['lang']=='en')
        return $msg;

    return $otherLang[$_GET['lang']][array_search($msg,$defaultLang)];
}

// Note the function is simplified up there

As you can see the default case deosnt' need to load anything or do any operation, the function just returns back the argument passed


i like the answer with the lang/en.php file. but instead of a file for each language, i use a file for each web page (or class, etc). this keeps file sizes lower and i create a 3D array:

`return array( "EN" => array( "title" => "Welcome - Good Morning", ...), "TG" => array( "title" => "Mabuhay - Magandang Umaga Po", ...) );'

Real easy to add new language strings too...

This makes it real easy for language translation contractors since they can see the native language in close proximity to the foreign in 1 editor,,,

0

精彩评论

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