Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow placeholders in language lines #1502

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions system/helpers/language_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,40 @@ function lang($line, $id = '')
}
}

if ( ! function_exists('lang_format'))
{
/**
* Lang Format
*
* Fetches a language variable and optionally replaces placeholders with
* actual values
*
* @param string the language line
* @param string the actual value(s) for the placeholder(s)
* @param string the placeholder string
* @return string
*/
function lang_format($line, $values = NULL, $placeholder = '?')
{
$line = lang($line);

if($values !== NULL && ($position = strpos($line, $placeholder)) !== FALSE)
{
$values = is_array($values) ? $values : array($values);
$placeholder_length = strlen($placeholder);
$index = 0;

do
{
$line = substr_replace($line, $values[$index++], $position, $placeholder_length);
$position = strpos($line, $placeholder, $position + $placeholder_length);
}
while($position !== FALSE);
}

return $line;
}
}

/* End of file language_helper.php */
/* Location: ./system/helpers/language_helper.php */
2 changes: 2 additions & 0 deletions user_guide_src/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Release Date: Not Released
- ``url_title()`` will now trim extra dashes from beginning and end.
- ``anchor_popup()`` will now fill the "href" attribute with the URL and its JS code will return false instead.
- Added JS window name support to ``anchor_popup()`` function.
- :doc:`Language Helper <helpers/language_helper>` changes include:
- added ``lang_format()`` which allows for placeholders in the line of text to be replaced with actual values.
- Added XHTML Basic 1.1 doctype to :doc:`HTML Helper <helpers/html_helper>`.
- Changed ``humanize()`` to include a second param for the separator.
- Refactored ``plural()`` and ``singular()`` to avoid double pluralization and support more words.
Expand Down
18 changes: 18 additions & 0 deletions user_guide_src/source/helpers/language_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ form label for you. Example
echo lang('language_key', 'form_item_id');
// becomes <label for="form_item_id">language_key</label>

lang_format('language line', 'placeholder value(s)', 'placeholder string')
===================================

This function returns a line of text from a loaded language file with
simplified syntax that may be more desirable for view files than calling
`$this->lang->line()`. The optional second and third parameters allows you
to replace placeholders in the line of text with actual values. Example

::

// $lang['language_key'] = 'You have ? new messages.';
echo lang_format('language_key', 10);
// outputs: You have 10 new messages.

// $lang['language_key'] = 'Are you sure you want to remove %s from %s ?';
echo lang_format('language_key', array('Item 1', 'Category 1'), '%s');
// outputs: Are you sure you want to remove Item 1 from Category 1 ?