简体   繁体   中英

Multilingual support using gettext with codeigniter, best practice?

I know how to create .po files and how to generate .mo files and then use them for translation on my Codeigniter powered site. However, I'm not quite sure on how to change language from the site GUI. I want to stick to codeigniter's default url calling schema: www.domain.com/controllername/method/param1/param2.

Calling the server like this is a no-brainer: www.domain.com/controllername?lang=en

Doing that for every controller using the default url schema, requires me to implement the same method in every controller, just to pass the lang parameter to the setlocale() function and then bind to my .po domain name. Feels awkward...

ANy ideas how you guys work with gettext in codeigniter? And yes, I do want to work with gettext.

I just make a library which can auto translate all text between {t} and {/t} in the view, I posted here in case some one want to use it instead calling the gettext function in the view:

http://www.chuongduong.net/page/15/codeigniter-gettext-with-smarty-or-parser-template-without-php-code-in-view.html

The view code might be:

<html>
<head>
<title>{blog_title}</title>
</head>
<body>

<h3>{blog_heading}</h3>
{blog_entries}

<h5>{t}Title is{/t}  {title}</h5>

<p>{t 1="<b>" 2="</b>"}Click here %1to see%2 me{/t}{body}</p>

<p>{t 1="{id}" 2="author"}The id is: %1 wrote by %2{/t}</p>

<p>{t 1="<a href=\"link here\">" 2="</a>"}Please lick on me%2{/t}</p>

{/blog_entries}

</body>

</html>

I check for the user's language hooking the detection at "post_controller_constructor", and I set it somewhere globally available (eg changing at runtime the language in the config file). A controller has just to use the value.

The language is detected with the following check in fallback

  1. it has been set via a GET parameter (eg ?lang=en) ?
  2. it was set in a cookie ?
  3. what is the browser suggested language ?
  4. use the default language configured

If a language is not supported, however, use the default. Set or refresh the cookie with the new data.

So you usually do not need to use the GET parameter, evenctuallly just one time if the user clicks somewhere to change language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM