MVC and dynamic language change

Asked

Viewed 369 times

1

I have a MVC that I implement in my projects in PHP. This supports multiple languages and by default gets the active/initial language from the browser.

However there are projects that require the functionality of modifying the language with a simple click.

The solution I understand best is by the URL or I mean:

p/auto language url: http://meuprojecto.com/[controller]/[action]

url p/force language/locale: http://meuprojecto.com/pt-PT/[controller]/[action].

As you can see, I use the LOCALE standard to obtain the language... Being that I implemented using the hyphen or underscore. This way whenever the framework detects this type of URL it modifies for the project the LOCALE initially obtained from the browser.

Having my solution, I would like to evaluate other ways of solving this problem.

My question is: what other mechanisms do you know within implementations in PHP solved differently? I have been thinking about inserting support for the presence of a tag ISO 639-1 to identify the language but I think I’ll be complicating, or not?

  • 2

    It is unrelated to MVC.. But regardless, ISO3166-Alpha2 refers to country codes. I recommend using ISO codes for languages instead of countries, because how to determine, for example, the language of a bilingual country?

  • @Danielomine did not understand why it has no relation with MVC. Thanks for the correction of the ISO because I was wrong and I corrected it. To say that by language it sounds good but LOCALE is actually more specific because I can identify pt-PT or pt-BR, en-GB or en-US for example. What for some implementations makes all the difference.

  • @Danielomine however my doubt, in adopting the ISO is why there are actually more comprehensive implementations that do not require as much specificity. and a url http://exemplo.com/pt. is much simpler than http://exemplo.com/pt-PT

  • ok, I imagined this. The problem of using only the language code is for the case of different countries. The code "pt" is Portuguese, however, the Portuguese of Brazil is different from Portuguese. About MVC, it is indifferent whether it uses MVC or not.. so it has no relation.

  • locale code is based on the [language code]-[country code] structure both in 2 digits.. However, there are some exceptions such as "az-Cyrl-AZ", for example.

  • 1

    As stated above, this is unrelated to the use of MVC. If I understand your problem, you can use some internationalization libraries (recommend to php-i18n).

  • I know what an MVC is and I’ve placed the selection within the component I built to control the outputs with respect to the language within the component I created and that’s why I connected them. My question is simple if you know other ways to define a language besides the one I implemented. @Williamurbano thanks for your suggestion I will investigate.

  • @chambelix : Try to store the locale in your session through some URL that the user will access and that language will be set. This way there will be no need to include the locale in your URL.

Show 3 more comments

3 answers

2

The most elegant scheme I’ve ever found for PHP is in Cakephp, in the module i18n.

Basically, the whole system is defined with internationalization functions like this:

__("MinhaString")

"Minhastring" is understood as a key to Resource. All keys can be extracted using an automatic procedure like this.

After that, the files can be translated with the help of a tool like the Poedit.

  • 1

    thanks a lot for your time...I did not know and went to see. Funny that use the same logic in the folders based on Locale. Except that I do not use Poedit I use files with array in php. But I can give a deeper analysis.

  • @chambelix Yes, the idea is just to give you a north even.

0

You’d be creating another medium that would make sense. But, still it would be an extra job and for anyone else and for SEO purposes could have side effects.

What I can recommend is the implementation of cookies to store and capture the locale according to what the user wants. It would be faster, you can manipulate by javascript too, for example, and would make your code more readable.

  • Thank you for your reply. But I’m with this implementation in many projects and has given results and as for SEO I don’t see where it can give problems. As for the implementation of cookies, that is what I have just said because my question is a while old and I have had to find a solution. I think I got a good compromise that so far has worked. I ran away from javascript. my point is to see another type of implementations to see if I’m on the right track or not.

-3

Hello, my dear I see that it is very old the post, but I will leave another way to implement i18n in a project using the MVC standard, I am not using framework, pure structure, divided into folders, controllers,models,core,views, so I believe you will understand well. in the core folder, responsible for rotating all gear, I did the following:

inserir a descrição da imagem aqui

I created a folder of Noma Langs at the root of the project and put the files with the languages, they are: en.ini,en.ini,es.ini

in the controllers directory I created a file like this:

inserir a descrição da imagem aqui

Even in the core folder there is a controller.php file and in the constructor I added the Language class instance, it was like this:

inserir a descrição da imagem aqui

Right? I hope you’re getting clear, anyway, in your views just do so:

where will the translation take place: lang->get('CONTANT'); ? >//constant created in the file . ini, I will show a short snippet:

//en inserir a descrição da imagem aqui

//en inserir a descrição da imagem aqui

//es inserir a descrição da imagem aqui

Understood, so you can translate the content of the page with a click on some link or menu ai, hope to have helped, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.