Creating a web page using Multilanguage in PHP

I would like to develop a multilingual page in PHP, for example, in English / German / Japanese. Therefore, when I click on German, the language of the page will change to German, then I will click English so that it changes to English. Please help me do this. Thanks in advance.

+3
source share
10 answers

Strange, so many php developers believe that all natural languages ​​have the same grammar. Creating a translation framework without parameters is simply not possible. Although the string $lang['welcomeTo'] $sitenamecan be correctly translated for most languages ​​(Welcome to stackoverflow), it fails for the Turkish language, for example, where it should be $sitename $lang['welcomeTo'](stackoverflow'a hos geldiniz) or for the German language, where any way would be possible (Willkommen auf stackoverflow / stackoverflow heißt Sie willkommen). And it gets worse when you try to translate sentences with multiple variables.

The only option is to use parameterized translations (say: functions)

Language::getCurrentLanguage()->translate('welcomeTo', array('sitename' => $sitename));

# Translation file for english (configfile-style):
welcomeTo=Welcome to $sitename

# Translation file for turkish:
welcomeTo=$sitename'a hos geldiniz

# Translation file for german:
welcomeTo=Willkommen auf $sitename
# Or
welcomeTo=$sitename heißt sie willkommen

: i18n, , . , intl php.

+8

, .

$_SESSION["lang"] = "en";
$_SESSION["lang"] = $_GET["lang"]; 
// or this way, then you can do example.com/?lang=en

.

$lang["en"]["welcome"] = "Welcome to the site";

$lang["de"]["welcome"] = "Willkommen auf der Webseite";

$lang

echo $lang[$_SESSION["lang"]]["welcome"];

, . , SELECT .

+2

- UTF-8. . ( header()).

, . . , , , . .

, , , $_SESSION .

:

$lang["en"]["Welcome"] = "Welcome to the site";

-

$lang["ch"]["Welcome"] = "歡迎";

, .

echo $lang[$_SESSION["lang"]]["Welcome"];
+1

- , , . , , , , . #, , , .

+1
0

HTML , . , . , UTF-8 .

0

Rails well. , .

0

; gettext - ...

I- u .po .mo, po , Linux. .po, Linux, .

II-linux .po, Windows ​​, poedit ( : http://www.poedit.net/)

III- . , ?: (

0

. , , :

application/
 someScript.php
views/
 de/
  someView.php
 en/
  someView.php
models/
 someModel.php

, , - .

 session_start(); //dont forget to call this somewhere
 $_SESSION['lang'] = 'en';
 //and then in somewehere else:
 include 'views/'.$_SESSION['lang'].'/someView.php'; //or whatever method is used to include views.


script. , script, , , UNIX, someScript.php :

  //someScript.php
  $timestamp = time();

, , , someView.php. , someView.php "en" :

<html>
<head>
<title>Currect UNIX timestamp!</title>
</head>
<body>
<p>The current UNIX timestamp is: <?php echo $timestamp; ?> seconds</p>
</body></html>

'de' , .

, , someScript.php, script:

 include '../views/'.$_SESSION['lang'].'/someView.php';

: http://www.onlamp.com/pub/a/php/2005/09/15/mvc_intro.html

: MVC, .

-2

Source: https://habr.com/ru/post/1706888/


All Articles