Multi-language support using gettext with codeigniter, best practice?

I know how to create .po files and how to generate .mo files and then use them to translate on my Codeigniter website. However, I'm not quite sure how to change the language from the graphical interface of the site. I want to stick to the default codeigniter url calling scheme: www.domain.com/controllername/method/param1/param2.

Server call like this, no problem: www.domain.com/controllername?lang=en

Doing this for each controller using the default URL scheme requires me to implement the same method in each controller, just to pass the lang parameter to setlocale () and then bind it to my .po domain name. Feels awkward ...

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

+4
source share
2 answers

I'm just creating a library that can automatically translate all text between {t} and {/ t} in a view, I placed it here, in case someone wants to use it instead of 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

View code can 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> 
+2
source

I verify that the user language has hooked the detection in post_controller_constructor and I set it somewhere on a global scale (for example, changing the language in the configuration file at run time). The controller should just use the value.

Language detected with next check in backup

  • was it set using the GET parameter (e.g.? lang = en)?
  • was it set in cookie?
  • What is the language offered by the browser?
  • use the default language

If the language is not supported, use the default value. Set or update the cookie with the new data.

This way, you usually don’t need to use the GET parameter, evenly just once, if the user clicks somewhere to change the language

+1
source

Source: https://habr.com/ru/post/1305268/


All Articles