Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:23:24+00:00 2026-05-25T02:23:24+00:00

I came across the following code with replaces one variable in a language file

  • 0

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

my _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);
    }
}

my controller

<?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');
    }
}

my lang file :

<?php $lang['test'] = 'Hello %s';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:23:25+00:00Added an answer on May 25, 2026 at 2:23 am

    Try something like this using 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');
    

    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’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.

    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;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following code recently and would like to optimize it: Public
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I have a question regarding the following code snippet I came across in one
I'm learning LINQ and came across the following code in one of the books:
I came across the following code while debugging. I was able to get the
I've been looking at this question and came across the following code in one
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following code in a code base I am working on:
I have came across the following code, and being a C beginner, I came
I just came across the following code: for(Iterator<String> iterator = stuff.iterator(); iterator.hasNext();) { ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.