duanhuhong5255 2013-03-14 15:54
浏览 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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line