H
H
hdtor2016-03-14 20:26:08
PHP
hdtor, 2016-03-14 20:26:08

What's the best way to do multiple languages?

I am writing a self-written engine of one site.
There is a task to make several languages ​​​​on the site, but I don’t want to produce folders for us:
/ ru
/ en
/ pl
and put all the files into each folder again.
All data is taken from the database. Of course already created an array with phrases for each language. But the problem is that you need to make links according to the type as above, but not to produce heaps of folders.
How can this be done? Can you do something in htaccess by type, if there is a get parameter ?lang=ru then redirect to the same link just substitute the ru folder or if ?lang=en then redirect to the en folder?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lumore, 2016-03-14
@Lumore

Set the default language to Russian, create 'lang.php', where you take get lang. And depending on the language, output the word from the array.
Haven't posted in a while, but still:

if(isset($_SESSION['lang'])) { include('/langs/'.$_SESSION['lang'].'.php'); }
<a href="/lang.php?lang=en">En</a>
<a href="/lang.php?lang=pl">Pl</a>

<p><?= $lang['hello'] ?>, <?= $user->username ?></p>

lang.php
if(isset($_GET['lang'])) {
$_SESSION['lang'] = $_GET['lang'];
}

PS code may not be accurate

I
Ivan, 2016-03-14
@IvanCher

I am writing a self-written engine of one site.

I'm not even going to explain how ridiculous the idea is.
Just a few questions for you to ask yourself and answer:
I think, of course, that you have hardly worked with anything yet and don’t even really know how to design a more or less flexible and reliable system, but oh well, that’s up to you.
What htaccess? And if the "site takes off" and you have to install nginx? :) Okay, just kidding, neither the site nor the cms will take off, so don't worry about it :)
You should have a layer that is responsible for mapping the request to some controller/action/method/whatever. Let's call this layer Router.
And Router'a can have the following logic: if /(ru|en|pl) comes after the domain, then set the corresponding value of the current language to the session or cookie.
In the code where you display some text, you wrap this text in a translation method. For example, let it be the static method Translator::trans($message). Inside the static method, take the current language from the session/cookie and see if there is a given message for that language. Well, then develop the logic as you want.

E
Eugene 222, 2016-03-14
@mik222

If you write the engine yourself. Why are you doing this in PHP?
There are many nice and convenient tools in the world.
Why choose something inferior.
--------
How would I do the localization.
I would define a global hashmap in a separate file. And a function that maps your standard localization to hashmap localization.
Fast, no parsing, no databases and other files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question