duanhuhong5255 2013-03-14 15:54 采纳率: 0%
浏览 37
已采纳

PHP / Smarty - 每个文件都有自己的语言文件

I am trying to get each file to have it own language file.

I am using a mix of osDate, SMF and my own code. osDate stores the language in the database, but I am not wanting this, I would like each file to have it own language file, so for example register has it own language file at lang/english/register.php.

I will be using Smarty, which is causing me the headache.

I have the below PHP code working, but don't know how to add or get the language side working.

Here is my current code.

tester1.php

<?php

if (!defined('SMARTY_DIR')) {
    include_once('init_test.php');
}

$actionArray = array(
    'register' => array('Register.php', 'Register'),
);

if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']])) {
    echo '<a href="?action=register">test</a>';
} else {
    require_once($actionArray[$_REQUEST['action']][0]);
    call_user_func($actionArray[$_REQUEST['action']][1]);

}

$t->display('index.tpl');

?>

Register.php

<?php

function Register() {
    global $t;
    $t->assign('rendered_page', $t->fetch('register.tpl'));
}

?>

index.tpl

{$rendered_page}

register.tpl

Test: {$testlang}<br>
Title: {$title}

Language file - lang/english/register.php

<?php

$lang['testlang'] = 'working';
$lang['title'] = 'This is the title';

?>

So in the example Register needs to pass the language from Register.php to display in register.tpl.

I am aware I can assign each language string in the Register.php file, but I was hoping, I would be able to just assign the who register language file and then just call it at random, without having to assign each language string in Register.php

Any code, tips welcome. I have tried Googling, but it hasn't come up with much.

  • 写回答

1条回答 默认 最新

  • dongyou1926 2013-03-15 13:46
    关注

    You shouldn't pass rendered things into Smarty - you should be passing in an array of strings to use.

    register.php

    $lang = array(
        'test' => "working",
        'title' => "This is the title", 
    );
    
    function Register() {
       global $lang;
       $t->assign('lang', $lang);
    }
    

    index.tpl

    Test: {$lang['test']}<br>
    Title: {$lang['title']}
    

    Will do what you asked.

    However - you don't want to code it like this as it will be incredibly painful to use when you inevitably need to pass in parameters to the strings.

    You should define a Smarty function to display translated text with as many variables as needed e.g.

    {translate string='Greeting' name=$user.name}
    

    Where the translate function would pull the 'Greeting' string from the list of known strings which would be defined as Hello %name%. It would then replace %name% with the users name to say Hello John etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?