开发者

Translating a sentence that includes HTML using CodeIgniter language class

How can I use HTML in language variables returned from the CI lamnguage class. To give you an example;

Normally I add tranlsated text link this: In lang file:

$lang['login'] = 'Login';

In view:

<?= lang('login'); ?>

However some of my translated text needs to include links, for example:

"I agree to the <a href="" title="Terms of Use">terms of use</a>开发者_开发知识库"

How can I translate this, the link might not appear in the same place in the sentence in different languages and my lang file will be translated by non technical staff. So i can't just dump the link HTML into the lang file.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜