This question has already been answered:

I create a rervis website for repairing houses in two languages ​​(Azerbaijani and Russian). I don't know much about programming. I found a script suiting me and translated the whole site. And everything was fine until I started creating pages and articles, because they also need to be translated, but the translation script works on arrays, I didn’t think for a long time, I realized that you need to create tables in two languages, it’s not hard! The page id = 1 will be with the Russian text, and the page with id = 2 on Azerb. language. BUT! The translation script, when selecting a specific language, does not touch the table. Hmm, too, here already without the help of the pros can not do. Maybe somehow make id = 1_rus or rus = 1 aze = 1. I ask for your help.
I do not use CMS, I write it myself. Here is the translation script:

$LangArray = array("Russkiy", "Azerbaycan"); $DefaultLang = "Russkiy"; if(@$_SESSION['NowLang']) { if(!in_array($_SESSION['NowLang'], $LangArray)) { $_SESSION['NowLang'] = $DefaultLang; } } else {$_SESSION['NowLang'] = $DefaultLang;} $language = addslashes($_GET['language']); if($language) {if(!in_array($language, $LangArray)) { $_SESSION['NowLang'] = $DefaultLang;} else {$_SESSION['NowLang'] = $language;} } $CurentLang = addslashes($_SESSION['NowLang']); include_once (ROOT.'/languages/'.$CurentLang.'.php'); 

Reported as a duplicate by members of BOPOH , Streletz , zRrr , Athari , romeo 17 Nov '15 at 20:49 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Show the code where you get the content from the tables. - Vladimir Gordeev
  • If it's not difficult for you, please show the example request you provided in php. - Shahin

2 answers 2

Many options:

  1. One table for content (pages), a text cell is duplicated for each language (title_rus, title_aze, text_rus, text_aze, etc.). In url'y added GET variable lang, then the address will be approximately of the form http://example.com/index.php?id=1&lang=rus . Before sending a request to the database to retrieve the page data, the availability and value of GET['lang'] checked, and depending on its value, certain cells are selected. Also, to simplify, you can make the default language, that is, in the absence of the GET['lang'] variable, select a specific language.

  2. Two tables. One with id pages, the second with values ​​for languages. The structure of the second table is pageID, lang, title, text, etc. In this case, where pageID = GET[id] and lang = GET[lang] embedded in the query. This option is better for cases where there will be many languages.

Example

Table structure

  • table page:

    • id (auto_increment),
    • date_added (creation date),
    • date_update (last edited),
    • active (availability status, there are cases that the page remains in the database but was not visible from the front)

    A separate table is needed for the convenience of maintaining the id pages so that when adding each new page not to calculate the last id in the page_lang table. When creating a new page, send the request:

     insert into page set active = 1 

    after execution we get the id of our page through mysql_insert_id

    example of filling:

     |id|date_added|last_update|active| |1 |01.12.2013|31.03.2014 | 1 | |2 |05.01.2014|25.04.2014 | 1 | 
  • table lang:

    • id (auto_increment),
    • lang_name (varchar)

    example of filling:

     |id|lang_name| |1 | rus | |2 | aze | 
  • table page_lang:

    • page_id (linked to page.id)
    • lang_id (linked to lang.id),
    • title,
    • meta_d,
    • meta_k,
    • text (depending on lang_id contains text data in the specified language).

    example of filling:

     |page_id|lang_id| title | meta_d | meta_k | text | | 1 | 1 | Главная | Главная | Главная | основной текст | | 1 | 2 |Ana səhifə| Ana səhifə | Ana səhifə | əsas mətn | | 2 | 1 | О нас | О нас | О нас | основной текст | | 2 | 2 |Haqqımızda| Haqqımızda | Haqqımızda |Bizim haqqında mətn| 

Logics

At the initial visit to the site, we set $lang by default. When the user changes the language, we check the availability of $_GET[lang] and its value. In the absence of $_GET[lang] we take the default value. If the value does not match (for example, there is rus, aze, eng and we get &lang=blabla ) send to 404.html . If the value matches and the presence of $_GET[id] make a query to the database:

 select pl.* from page_lang as pl inner join lang on (pl.lang_id = lang.id) where pl.page_id = $id and pl.active = 1 and lang.lang_name = $lang 

I highly recommend checking them before using variables in the query.

  • thank you very much! I will try - Shahin
  • @Shahin, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - terantul
  • You can show the second variant as an example with a code, and if the first is not difficult - Shahin
  • Where to write select pl. * From page_lang as pl inner join lang on (pl.lang_id = lang.id) where pl.id = $ id and lang.lang_name = $ lang And what is it? I haven't seen such requests in php. - Shahin
  • How do you select data from a database? - terantul

How about adding another column to the table for the content? The first column is the content in Russian, the second is the content in Azerbaijani.

Not enough information. Give the names and versions of the used CMS and plug-ins, as well as pieces of code where you use tables, arrays.