Note that there are some explanatory texts on larger screens.

plurals
  1. POLanguage translation using PHP
    text
    copied!<p>Hi i am devloping sample site in php i need to translate whole website in to persian. how can it possible in php?? I have tried using the following code.. This code will working fine for deutsch conversion.</p> <pre><code>1. class.translation.php &lt;?php class Translator { private $language = 'en'; private $lang = array(); public function __construct($language){ $this-&gt;language = $language; } private function findString($str) { if (array_key_exists($str, $this-&gt;lang[$this-&gt;language])) { echo $this-&gt;lang[$this-&gt;language][$str]; return; } echo $str; } private function splitStrings($str) { return explode('=',trim($str)); } public function __($str) { if (!array_key_exists($this-&gt;language, $this-&gt;lang)) { if (file_exists($this-&gt;language.'.txt')) { $strings = array_map(array($this,'splitStrings'),file($this-&gt;language.'.txt')); foreach ($strings as $k =&gt; $v) { $this-&gt;lang[$this-&gt;language][$v[0]] = $v[1]; } return $this-&gt;findString($str); } else { echo $str; } } else { return $this-&gt;findString($str); } } } ?&gt; </code></pre> <p>2.Register.php</p> <pre><code>&lt;?php require_once('class.translation.php'); if(isset($_GET['lang'])) $translate = new Translator($_GET['lang']); else $translate = new Translator('en'); ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;&lt;?php $translate-&gt;__('CSS Registration Form'); ?&gt;&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15"/&gt; &lt;link rel="stylesheet" type="text/css" href="css/default.css"/&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="" class="register"&gt; &lt;h1&gt;&lt;?php $translate-&gt;__('Registration'); ?&gt;&lt;a class="flag_deutsch" title="deutsch" href="register1.php?lang=de"&gt;&lt;/a&gt;&lt;a class="flag_english" title="english" href="register1.php"&gt;&lt;/a&gt;&lt;/h1&gt; &lt;fieldset class="row1"&gt; &lt;legend&gt;&lt;?php $translate-&gt;__('Account Details'); ?&gt;&lt;/legend&gt; &lt;p&gt; &lt;label&gt;&lt;?php $translate-&gt;__('Email'); ?&gt; *&lt;/label&gt; &lt;input type="text"/&gt; &lt;label&gt;&lt;?php $translate-&gt;__('Repeat email'); ?&gt; *&lt;/label&gt; &lt;input type="text"/&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;div&gt;&lt;button class="button"&gt;&lt;?php $translate-&gt;__('Register'); ?&gt; &amp;raquo;&lt;/button&gt;&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Is it possible to transilate to other laguages using this code?? I changed register1.php?lang=de to register1.php?lang=fa(persian).. But nothing hapens..anybody plese help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload