0

I'm using PHP.

I've a system that I need to translate in french, italian, spanish, ...

So I came with two solutions.

Solution 1: All the language strings are store in a DB to allow easy editing after for the administrator. So on each page I will need to call this table in order to get the correct strings.

Solution 2: Same like the first one except that I will generate after each change a Json file. This Json file will be call on each page to print the right strings.

Witch of my solution is the best pleas ?

Perhaps, another solution exists for that ?

Thanks.

2 Answers2

1

I once did a project with multiple languages. It was a big hassle. I am not recommending my method below, but showing it to you as an example of how it can be achieved.

lang.en.php

$lang = array();

$lang['menu'] = array(
  'home'     => 'Home',
  'projects' => 'Projects',
  'about'    => 'About',
  'contact'  => 'Contact'
);

$lang['some_other_section'] = array(
  'some_value' => 'some value',
  ...
  ...
);

lang.no.php

$lang = array();

$lang['menu'] = array(
  'home'     => 'Hjem',
  'projects' => 'Prosjekter',
  'about'    => 'Om',
  'contact'  => 'Kontakt'
);

$lang['some_other_section'] = array(
  'some_value' => 'En annen verdi',
  ...
  ...
);

Now, the big hassle was to write all the text in the application using variables, then add them and translate in each language file.

index.php

<div class="menu">
  <a href="#"><?php echo $lang['menu']['home']; ?></a>
  <a href="#"><?php echo $lang['menu']['projects']; ?></a>
  <a href="#"><?php echo $lang['menu']['about']; ?></a>
  <a href="#"><?php echo $lang['menu']['contact']; ?></a>
</div>

You would have to set the "active" language some place in the initialization of the page. i.e:

<?php
  session_start();

  if($_SESSION['lang'] == "en") {

    require('lang.en.php');

  } elseif($_SESSION['lang'] == "no") {

    require('lang.no.php');

  }

?>
<html>
<head>
 ...
 ...
hesonline
  • 88
  • 9
0

Use a database: you can hold questions and answers in a table that has each language answer in columns - its simple enough to then set up an RBAC module to allow specific users to add new questions and answers.

questions_table:
    question_id (int) 255 PK AUTO_INCREMENT
    question (varchar) 160

answers_table:
    answer_id (int) 255 PK AUTO_INCREMENT
    question_id (int) 255
    language_id (int) 255
    answer (varchar)

language_table:
    language_id (int) 255 PK AUTO_INCREMENT
    language (varchar) 30

user table and user answer table also

using this, you'll be able to reference between tables using INNER JOIN sql statements, then all you need to do is create a driver object for your connection (preferably pdo) and then objects for answering questions, logging in, registering, admin and a front end display.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86