Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to roll your own with jquery &amp; jquery browserLanguage, this might get you going.</p> <p>Tag all translatable text's contain elements with <code>class="i18n"</code>, and include jquery, jquery browserLanguage, and your i18n script.</p> <p><em>1</em>. <strong>the internationalization javascript</strong> </p> <p><em>— this needs to accept translations via ajax from your server, like:</em></p> <pre><code>var i18n = {}; i18n.bank = new Array(); i18n.t = function ( text, tl=$.browserLanguage ) { var r = false; $.ajax({ url: "/i18n_t.php?type=request&amp;from="+ escape(text) +"&amp;tl="+ tl, success: function(){ i18n.bank[text] = this; r = true; } }); return r; }; </code></pre> <p><em>2</em>. <strong>php i18n translation service</strong></p> <p><em>— now we need to serve up translations, and accept them</em></p> <p>the database will look like a bunch of tables, one for each language.</p> <pre><code>// SCHEMA for each language: CREATE TABLE `en` ( `id` INT PRIMARY KEY AUTO INCREMENT NOT NULL, `from` VARCHAR(500) NOT NULL, `to` VARCHAR(500) NOT NULL ) </code></pre> <p>the php will need some connection and db manipulation.. for now this may do:</p> <pre><code>//Connect to the database $connection = mysql_connect('host (usually localhost)', 'mysql_username' , 'mysql_password'); $selection = mysql_select_db('mysql_database', $connection); function table_exists($tablename, $database = false) { if(!$database) { $res = mysql_query("SELECT DATABASE()"); $database = mysql_result($res, 0); } $res = mysql_query("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$database' AND table_name = '$tablename' "); return mysql_result($res, 0) == 1; } </code></pre> <p>the code is simply:</p> <pre><code>&lt;?php // .. database stuff from above goes here .. $type=$_GET["type"]; $from=$_GET["from"]; $to=$_GET["to"]; $tl=$_GET["tl"]; if (! table_exists($tl)) { ... } if ($type == "request") { // might want to set $tl="en" when ! table_exists($tl) $find = mysql_query("SELECT to FROM `'$tl'` WHERE from='$from'"); $row = mysql_fetch_array($find); echo $row['to']; } elsif ($type == "suggest") { $find = mysql_query("SELECT COUNT(*) AS count FROM `'$tl'` WHERE from='$from'"); if ( !(mysql_result($res, 0)) == 0 ) { $ins = mysql_query("INSERT INTO `'$tl'` (from, to) VALUES ('$from','$to')"); } } ?&gt; </code></pre> <p><em>3</em>. <strong>page translation mechanics</strong></p> <p><em>— finally we can tie them together in your webpages with some further jquery:</em></p> <pre><code>i18n.suggest = function (from) { // post user translation to our php $.ajax({ url: "/i18n_t.php?type=suggest&amp;from='+from+'&amp;to="+ escape( $('#i18n_s').contents() ) +"&amp;tl="+ $.browserLanguage, success: function(){ $('#i18n_t_div').html('&lt;em&gt;Thanks!&lt;/em&gt;').delay(334).fadeOut().remove(); } }); }; $(document).ready(function() { i18n.t("submit"); i18n.t("Thanks!"); $('.i18n').click( function(event) { //add an onClick event for all i18n spans $('#i18n_t_div').remove; $(this).parent().append( '&lt;div id="i18n_t_div"&gt;&lt;form class="i18n_t_form"&gt; &lt;input type="text" id="i18n_s" name="suggestion" value="+$(this).contents()+" /&gt; &lt;input type="button" value="'+ i18n.bank[ "submit" ] +'" onclick="i18n.suggest( '+$(this).contents()+' )" /&gt; &lt;/form&gt;&lt;/div&gt;' ); }).each(function(){ var c = $(this).contents(); //now load initial translations for browser language for all the internationalized content on the page if ( i18n.t(c) ){ $(this).html(i18n.bank[c]); } }); }); </code></pre> <p>Mind you I don't have a server to test this on... and I don't actually code php. :D It will take some debugging but the scaffolding should be correct. </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