dragon19720808 2013-09-19 15:27
浏览 49
已采纳

CodeIgniter:为不同的语言创建一个html模板

I have a little issue with that subject.

Imagine, I need to create a template with some variables:

<div class="main_text">
    We would like to tell you that ...blah-blah...
    <?php $username ?>
    and after that we want to blah blah... 
    <?php $first_variable ?>
    and
    <?php $second_variable ?> etc.
<div>

I can see two ways for making this working with different languages:

  1. Create a template for each language. It is faster, but makes more code.
  2. Create language constants in a codeigniter way, for each part of string, like this:

    $lang['WE_WOULD_LIKE'] = 'We would like to tell you that ...blah-blah...';
    $lang['AND_AFTER_THAT'] = 'and after that we want to blah blah...';
    

And then populate it while parsing the template. This way is also not the best, too much small constants.

Maybe somebody knows a better way? Please show me the way.

  • 写回答

3条回答 默认 最新

  • dorpbn1027 2013-09-19 15:35
    关注

    Codeigniter actually has a built in language class.

    Language files are typically stored in your system/language directory. Alternately you can create a folder called language inside your application folder and store them there. CodeIgniter will look first in your application/language directory. If the directory does not exist or the specified language is not located there CI will instead look in your global system/language folder.

    Usage would be like the following once the desired language is set:

    $this->lang->line('language_key');
    

    Update: To pass variable's to the language variables you can use PHP's sprintf:

    $lang['WE_WOULD_LIKE'] = "We would like to tell you that %1$s and %2$s";
    

    And then to get that variable:

    $message = sprintf($this->lang->line('WE_WOULD_LIKE'), $first, $second);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据