如何将多个变量放在Codeigniter的语言文件中 [英] How to put multiple variable in a language file of Codeigniter

查看:83
本文介绍了如何将多个变量放在Codeigniter的语言文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到下面的代码,替换一个语言文件中的一个变量,但我希望它能够做多个。 %1,%2,%3等,而不只是一个%s。我尝试调整它来计算每一个变量在替换,但有些不能让它工作

I came across the following code with replaces one variable in a language file however I would like it to be able to do multiple e.g. %1, %2, %3, etc... and not just one %s. I tried tweaking it to count each variable in the line the do the replace but some how not getting it to work

我的_helper

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('line_with_arguments'))
{
    function line_with_arguments($line, $swap)
    {
        return str_replace('%s', $swap, $line);
    }
}

我的控制器

<?php
class Home extends CI_Controller
{
    public function index()
    {
        $this->lang->load('test', 'english');
        $this->load->helper('i18n');

        echo line_with_arguments($this->lang->line('test'), 'Matt');
    }
}

我的lang文件:

<?php $lang['test'] = 'Hello %s';


推荐答案

使用 vsprintf()

// Method 1: pass an array
function my_lang($line, $args = array())
{
  $CI =& get_instance();
  $lang = $CI->lang->line($line);
  // $lang = '%s %s were %s';// this would be the language line
  return vsprintf($lang, $args);
}

// Outputs "3 users were deleted"
echo my_lang('users.delete_user', array(3, 'users', 'deleted'));

// Method 2: read the arguments
function my_lang2()
{
  $CI =& get_instance();
  $args = func_get_args();
  $line = array_shift($args);
  $lang = $CI->lang->line($line);
  //  $lang = '%s %s were %s';// this would be the language line
  return vsprintf($lang, $args);
}

// Outputs "3 users were deleted"
echo my_lang2('users.delete_user', 3, 'users', 'deleted');

使用函数的第一个参数传递行索引,从CI获取正确的行,传递一个数组作为第二个参数(method1)或其余的参数作为每个变量(method2)。有关格式设置,请参阅 sprintf()上的文档: http ://www.php.net/manual/en/function.sprintf.php

Use the first argument of the function to pass the line index, get the correct line from CI, and pass an array as the second param (method1) or the rest of the arguments as each variable (method2). See the docs on sprintf() for formatting: http://www.php.net/manual/en/function.sprintf.php

CI的本机 lang() 函数使用第二个参数传递HTML表单元素 id ,并将创建一个< label> 标签,而不是一个很好的使用这个功能,如果你问我。如果不使用标签功能,最好创建 my_language_helper.php 并覆盖 lang()

CI's native lang() function uses the second param to pass an HTML form element id and will create a <label> tag instead - not a great use of this function if you ask me. If you don't use the label feature, it might be a good idea to create a my_language_helper.php and overwrite the lang() function to do this stuff natively, instead of writing a new function.

这里是我的实际 lang()函数看起来像,我不需要< label> 选项,所以我重写了第二个参数接受一个字符串或变量数组: / p>

Here is what my actual lang() function looks like, I don't need the <label> option so I overwrote the second param to accept a string or an array of variables instead:

// application/helpers/my_language_helper.php
function lang($line, $vars = array())
{
    $CI =& get_instance();
    $line = $CI->lang->line($line);

    if ($vars)
    {
        $line = vsprintf($line, (array) $vars);
    }

    return $line;
}

这样一个小小的,容易改变的好处,我希望它是默认 - 我从不使用 lang()输出< label> 标记, 。

Such an small, easy change for this benefit, I wish it were the default - I never use lang() to output a <label> tag, but need to pass variables to it frequently.

这篇关于如何将多个变量放在Codeigniter的语言文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆