G
G
Gregory2021-11-25 22:11:57
PHP
Gregory, 2021-11-25 22:11:57

Multilingual php twig site - how best to implement?

Good evening!
I am building a multilingual website.
The basis of the backend is implemented in php + template engine twig

How best to implement multilingualism?
Option to use the i18n twig extension - a colleague says that gettext based on which is not a very good option for a number of reasons)
Option to use plug-in dictionaries depending on the language - and convert them to a twig array
Maybe someone else will tell you some other options?)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
ThunderCat, 2021-11-25
@ThunderCat

Localization (good) is always based on custom translation sets, usually it is either a table in the database or a file with an array that is edited as the site is being filled. How exactly to implement it depends on many parameters, including the availability of ready-made solutions for the framework used. Or write something of your own. But the meaning is always about the same - the final array with keys like

$translate = ['welcome'=>['ru'=>"Приветики",'jp'=>"Komichiwa"]...];
How to get the values ​​​​by the key I think it is not necessary to chew? ..

G
grek_cheburek, 2021-11-25
@grek_cheburek

I always keep keys for words in ini files.
I have a lang_pack folder.
It has subdirectories de, ru, en, uk and so on.
In each of these folders there are .ini files that contain keys for the words
title=My Site
link_index=Home Page
And so on.
By the way, I make a separate such file for each module.
And on the website I use something similar.

<?php
echo "<a href='/'>".$main->lang('link_index','head_menu.ini')."</a>";
?>

I
Ipatiev, 2021-11-26
@Fockker

If we talk about Twig , and not just remember the old days, as in the fifth grade I translated my mother’s site for jewelry on my knee, then the filter is used for translations .
That is, a ready-made filter is taken or a custom filter is made. Which is engaged in the search for a match for the translated phrase. Which is displayed as {{ message|trans }}
Here is an example of the implementation of such a filter in Symphony
. If we talk about translations in general, then store it as you like, but when deploying, write to an array in a php file. Which lies to itself quietly in an opkeshe, and is connected by that filter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question