Translating a sentence that includes HTML using CodeIgniter language class Translating a sentence that includes HTML using CodeIgniter language class codeigniter codeigniter

Translating a sentence that includes HTML using CodeIgniter language class


EDIT: completely agree with your comment @Alex. Here's another choise

$lang['terms'] = 'I agree to the Terms of Use'$lang['terms1'] = 'Terms of Use';$link = '<a href="your_url" title="' . $lang['terms1'] . '">' . $lang['terms1'] . '</a>';$translated = str_replace($lang['terms1'],$link,$lang['terms']);echo $translated;

Now you don't depend on phrase order. For example:

$lang['terms'] = 'Ados termino eta baldintzak I'$lang['terms1'] = 'Ados termino';$link = '<a href="your_url" title="' . $lang['terms1'] . '">' . $lang['terms1'] . '</a>';$translated = str_replace($lang['terms1'],$link,$lang['terms']);echo $translated;

Another example changing order:

$lang['terms'] = 'Los términos de uso son válidos'$lang['terms1'] = 'términos de uso';$link = '<a href="your_url" title="' . $lang['terms1'] . '">' . $lang['terms1'] . '</a>';$translated = str_replace($lang['terms1'],$link,$lang['terms']);echo $translated;

PS: I can't speak basque, so I don't know if 'Ados termino' means 'Terms of use'. Anyways, I think that you can understand how I'm trying to do.


Do you mean that the link itself is different for each language? If the link is static and the same for all the languages you can just put it in your language file

$lang['terms'] = 'I agree to the <a href="http://mysite.com/terms" title="Terms of Use">terms of use</a>';

If the link will change depending on the language, you can create the link in your controller, and the use string formatting in your view to insert it into the language string.

So you would have this in your language files

$lang['terms'] = 'I agree to the <a href="%s" title="Terms of Use">terms of use</a>';

and in your view

<?php echo sprintf(lang('terms'),$link); ?>

where $link is defined in your controller and passed to the view

Hope that helps!