{"id":1492,"date":"2012-10-18T06:30:25","date_gmt":"2012-10-18T04:30:25","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1492"},"modified":"2019-01-06T21:01:29","modified_gmt":"2019-01-06T20:01:29","slug":"creare-e-gestire-un-sito-multilingua-testo-statico","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/1492\/creare-e-gestire-un-sito-multilingua-testo-statico\/","title":{"rendered":"Creare e gestire un sito multilingua (testo statico)"},"content":{"rendered":"<div id=\"attachment_1513\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a class=\"lightbox\" title=\"I siti che vogliono aprirsi all'estero devono preventivare una gestione multilingua delle pagine\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/10\/Mappamondo-con-bandiere-Stati.jpg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1513\" class=\"size-medium wp-image-1513 \" style=\"margin-bottom: 5px;\" title=\"Mappamondo con bandiere Stati\" src=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/10\/Mappamondo-con-bandiere-Stati-300x300.jpg\" alt=\"Mappamondo pieno di bandiere degli Stati\" width=\"300\" height=\"300\" srcset=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/10\/Mappamondo-con-bandiere-Stati-300x300.jpg 300w, https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/10\/Mappamondo-con-bandiere-Stati-150x150.jpg 150w, https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/10\/Mappamondo-con-bandiere-Stati.jpg 706w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1513\" class=\"wp-caption-text\">I siti che vogliono aprirsi all&#8217;estero devono preventivare una gestione multilingua delle pagine (1)<\/p><\/div>\n<p>Vi \u00e8 mai capitato di dover gestire un sito in due o pi\u00f9 lingue? No? Allora siete stati fortunati, perch\u00e9 con l&#8217;apertura all&#8217;estero (e in particolare per i siti di e-commerce) sempre pi\u00f9 siti anche di &#8220;piccola taglia&#8221; vi chiedono di <strong>affiancare alla lingua italiana almeno quella inglese<\/strong>.<\/p>\n<p>Quando progettate un sito web, di qualsiasi tipo, dovreste gi\u00e0 pensarlo come se fosse un multilingua: spesso, infatti, il cliente vi chieder\u00e0 soltanto in seguito l&#8217;aggiunta di una seconda o terza lingua. Se non avete previsto in anticipo l&#8217;aggiunta, risolvere il problema potrebbe richiedervi di stravolgere molte delle query che estraggono dati dal database.<\/p>\n<p>In questo articolo vi propongo <strong>un sistema per gestire un sito web con un numero indefinito di lingue<\/strong>. Non \u00e8 l&#8217;unico metodo possibile e pu\u00f2 essere perfezionato, ma dopo vari miglioramenti lo ritengo un approccio affidabile di base e che permette di agire agevolmente sui testi modificando pochi file.<br \/>\nIl linguaggio utilizzato \u00e8 il php con database di tipo mysql, ma \u00e8 possibile seguire lo stesso procedimento per qualsiasi altro linguaggio.<\/p>\n<p>All&#8217;interno dello script, liberamente scaricabile, troverete il codice essenziale che mostra la gestione in multilingua di una pagina: dovrete adattarlo voi al vostro sito.<br \/>\nInoltre troverete i <em>dump<\/em> delle tabelle essenziali del database.<\/p>\n<div style=\"float: left; width: 100%; margin-top: 60px; margin-bottom: 40px;\">\n<div style=\"float: left; width: 100px;\"><img decoding=\"async\" title=\"Creare e gestire un sito multilingua (testo statico)\" src=\"https:\/\/www.manuelmarangoni.it\/images\/zip_logo-80x80.png\" alt=\"Creare e gestire un sito multilingua (testo statico)\" \/><\/div>\n<div style=\"float: left; width: 350px; padding-top: 30px; font-size: 14px;\"><strong><a title=\"Creare e gestire un sito multilingua (testo statico)\" href=\"https:\/\/www.manuelmarangoni.it\/download_script.php?idfile=29&amp;link=https:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1492\" target=\"_blank\" rel=\"noopener\">Scarica gli script di questo articolo in formato zip<\/a><\/strong><\/div>\n<\/div>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">La tabella della lingua<\/h3>\n<p>Prima di tutto creiamo nel database la tabella &#8220;lingue&#8221; (trovate il <em>dump<\/em> da importare chiamato &#8220;lingue.sql&#8221;, dove sono gi\u00e0 predisposte l&#8217;italiano e l&#8217;inglese).<br \/>\nLa tabella da importare sar\u00e0 questa:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"mysql\" style=\"font-family: monospace;\"><span style=\"color: #990099; font-weight: bold;\">CREATE<\/span> <span style=\"color: #990099; font-weight: bold;\">TABLE<\/span> <span style=\"color: #990099; font-weight: bold;\"><span style=\"color: #009900;\">IF<\/span> <span style=\"color: #cc0099; font-weight: bold;\">NOT<\/span> EXISTS<\/span> <span style=\"color: #008000;\">`lingue`<\/span> <span style=\"color: #ff00ff;\">(<\/span>\n  <span style=\"color: #008000;\">`id`<\/span> <span style=\"color: #999900; font-weight: bold;\">int<\/span><span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">11<\/span><span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #cc0099; font-weight: bold;\">NOT<\/span> <span style=\"color: #9900ff; font-weight: bold;\">NULL<\/span> <span style=\"color: #ff9900; font-weight: bold;\">AUTO_INCREMENT<\/span><span style=\"color: #000033;\">,<\/span>\n  <span style=\"color: #008000;\">`nome`<\/span> <span style=\"color: #999900; font-weight: bold;\">varchar<\/span><span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">255<\/span><span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #cc0099; font-weight: bold;\">NOT<\/span> <span style=\"color: #9900ff; font-weight: bold;\">NULL<\/span><span style=\"color: #000033;\">,<\/span>\n  <span style=\"color: #008000;\">`estensione`<\/span> <span style=\"color: #999900; font-weight: bold;\">varchar<\/span><span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">2<\/span><span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #cc0099; font-weight: bold;\">NOT<\/span> <span style=\"color: #9900ff; font-weight: bold;\">NULL<\/span><span style=\"color: #000033;\">,<\/span>\n  <span style=\"color: #008000;\">`predefinita`<\/span> <span style=\"color: #999900; font-weight: bold;\">tinyint<\/span><span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">1<\/span><span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #cc0099; font-weight: bold;\">NOT<\/span> <span style=\"color: #9900ff; font-weight: bold;\">NULL<\/span> <span style=\"color: #990099; font-weight: bold;\">DEFAULT<\/span> <span style=\"color: #008000;\">'0'<\/span><span style=\"color: #000033;\">,<\/span>\n  <span style=\"color: #008000;\">`nascosto`<\/span> <span style=\"color: #999900; font-weight: bold;\">tinyint<\/span><span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">1<\/span><span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #cc0099; font-weight: bold;\">NOT<\/span> <span style=\"color: #9900ff; font-weight: bold;\">NULL<\/span> <span style=\"color: #990099; font-weight: bold;\">DEFAULT<\/span> <span style=\"color: #008000;\">'0'<\/span><span style=\"color: #000033;\">,<\/span>\n  <span style=\"color: #990099; font-weight: bold;\">PRIMARY KEY<\/span> <span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008000;\">`id`<\/span><span style=\"color: #ff00ff;\">)<\/span>\n<span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #990099; font-weight: bold;\">ENGINE<\/span><span style=\"color: #cc0099;\">=<\/span><span style=\"color: #990099; font-weight: bold;\">InnoDB<\/span>  <span style=\"color: #990099; font-weight: bold;\">DEFAULT<\/span> <span style=\"color: #ff9900; font-weight: bold;\">CHARSET<\/span><span style=\"color: #cc0099;\">=<\/span>latin1 <span style=\"color: #ff9900; font-weight: bold;\">AUTO_INCREMENT<\/span><span style=\"color: #cc0099;\">=<\/span><span style=\"color: #008080;\">3<\/span> <span style=\"color: #000033;\">;<\/span>\n\n<span style=\"color: #990099; font-weight: bold;\">INSERT<\/span> <span style=\"color: #990099; font-weight: bold;\">INTO<\/span> <span style=\"color: #008000;\">`lingue`<\/span> <span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008000;\">`id`<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">`nome`<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">`estensione`<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">`predefinita`<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">`nascosto`<\/span><span style=\"color: #ff00ff;\">)<\/span> <span style=\"color: #990099; font-weight: bold;\">VALUES<\/span>\n<span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">1<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">'Italiano'<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">'it'<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008080;\">1<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008080;\">0<\/span><span style=\"color: #ff00ff;\">)<\/span><span style=\"color: #000033;\">,<\/span>\n<span style=\"color: #ff00ff;\">(<\/span><span style=\"color: #008080;\">2<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">'Inglese'<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008000;\">'en'<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008080;\">0<\/span><span style=\"color: #000033;\">,<\/span> <span style=\"color: #008080;\">0<\/span><span style=\"color: #ff00ff;\">)<\/span><span style=\"color: #000033;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Il campo &#8220;predefinita&#8221; indica quale lingua da settare per default. Presumibilmente \u00e8 l&#8217;italiano, per cui dovrete inserire il valore 1 associato all&#8217;italiano (come da esempio). Il campo &#8220;nascosto&#8221; \u00e8 in pi\u00f9, nel senso che potr\u00e0 tornarvi utile nel caso in cui decidiate per qualche motivo di sopprimere una lingua (basta impostare il relativo record a 1).<\/p>\n<p><em>Consiglio<\/em>: <strong>fate in modo che l&#8217;estensione sia sempre di due caratteri<\/strong>, potrebbe tornarvi utile nel caso in cui dobbiamo effettuare controlli particolari sulla lunghezza del valore get.<\/p>\n<p><!--more--><\/p>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">La scelta della lingua<\/h3>\n<p>Dal lato del sito, <strong>l&#8217;utente avr\u00e0 un semplice link da cliccare (uno per ogni lingua)<\/strong>. Al click, sar\u00e0 passata la variabile $_GET[&#8220;lingua&#8221;] con il valore relativo alla lingua scelta. Questo valore (cio\u00e8 il parametro get) si dovr\u00e0 portare in ogni pagina del sito.<br \/>\nNello script ho messo semplicemente due link senza nessuna grafica:<\/p>\n<p><script type=\"text\/javascript\">\/\/ <![CDATA[\ngoogle_ad_client = \"ca-pub-9984963620559913\"; \/* Sir Bit - post singolo rettangolare *\/ google_ad_slot = \"5334366715\"; google_ad_width = 336; google_ad_height = 280;\n\/\/ ]]><\/script><\/p>\n<p><script src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\" type=\"text\/javascript\">\/\/ <![CDATA[\n\n\/\/ ]]><\/script><\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\n2\n3<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\"><span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">a<\/span> <span style=\"color: #000066;\">href<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"?lingua=it\"<\/span>&gt;<\/span>italiano<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">a<\/span>&gt;<\/span>\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">a<\/span> <span style=\"color: #000066;\">href<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"?lingua=en\"<\/span>&gt;<\/span>inglese<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">a<\/span>&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Come recuperiamo la lingua voluta? Lo vedete nel codice seguente. Come potete notare, nel caso in cui esista il parametro $_GET[&#8220;lingua&#8221;] sar\u00e0 preso quest&#8217;ultimo come simbolo per la lingua (it, en, ecc.).<br \/>\nSe invece il get sar\u00e0 mancante (per esempio, appena si apre l&#8217;home del sito), <strong>sar\u00e0 presa la lingua di default dal database<\/strong>.<br \/>\nIl valore sar\u00e0 immagazzinato nella costante LANG, che per sua natura \u00e8 globale e non modificabile.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\n2\n3\n4\n5\n6\n7\n8\n9<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #666666; font-style: italic;\">\/\/setto la lingua corrente<\/span>\n<span style=\"color: #666666; font-style: italic;\">\/\/se esiste il get, prendo quella, altrimenti la recupero dal database (prendo la lingua predefinita)<\/span>\n<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #990000;\">isset<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$_GET<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">\"lingua\"<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\n\t<span style=\"color: #990000;\">define<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'LANG'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$_GET<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">\"lingua\"<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #b1b100;\">else<\/span><span style=\"color: #009900;\">{<\/span>\n\t<span style=\"color: #000088;\">$query<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #990000;\">mysql_query<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"SELECT * FROM lingue WHERE predefinita=1 AND nascosto=0 LIMIT 0,1\"<\/span><span style=\"color: #339933;\">,<\/span> DB<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\n\t<span style=\"color: #000088;\">$row<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #990000;\">mysql_fetch_array<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$query<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\n\t<span style=\"color: #990000;\">define<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'LANG'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$row<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'estensione'<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">I testi statici<\/h3>\n<p>A questo punto abbiamo recuperato l&#8217;estensione della lingua corrente, racchiusa nella costante LANG.<br \/>\nVediamo innanzitutto <strong>come sistemare un testo statico<\/strong>. Mettiamo, per esempio, che abbiate due testi da stampare a video; normalmente, anche in puro html, il codice sarebbe il testo stesso cio\u00e8:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\n2\n3<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\">Questa \u00e8 la prima prova in italiano\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\nQuesta \u00e8 la seconda prova in italiano<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Nel nostro caso, se l&#8217;utente sceglie la lingua inglese dovremo trasformare all&#8217;occorrenza queste due stringhe in:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\n2\n3<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\">Questa \u00e8 la prima prova in inglese\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\nQuesta \u00e8 la seconda prova in inglese<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Come fare per rendere dinamica la stampa? Il sistema migliore \u00e8 di creare un file per ogni lingua e di importare il file adatto a seconda del valore di LANG.<br \/>\nNello script trovate una cartella chiamata &#8220;languages&#8221;, che conterr\u00e0 all&#8217;interno dei file di nome identico ma con il suffisso finale che cambia in base alla lingua (&#8220;lingua_it.php&#8221; e &#8220;lingua_en.php&#8221;).<br \/>\nPer importare il giusto file, subito dopo aver definito LANG andremo a includere:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #b1b100;\">include<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"languages\/lingua_\"<\/span><span style=\"color: #339933;\">.<\/span>LANG<span style=\"color: #339933;\">.<\/span><span style=\"color: #0000ff;\">\".php\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>All&#8217;interno di ognuno di questi file troveremo <strong>uno switch che, in base al case passato, ritorner\u00e0 la stringa relativa<\/strong>. Il codice dovrebbe bastare a capire; comunque sotto spender\u00f2 due parole per spiegarlo nel dettaglio:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\">\u00a0\n<span style=\"color: #666666; font-style: italic;\">\/\/contenuto nel file \"lingua_it.php\"<\/span>\n<span style=\"color: #000000; font-weight: bold;\">function<\/span> lingua<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$identificativo<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\n\n\t<span style=\"color: #b1b100;\">switch<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$identificativo<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\n\n\t\t<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000ff;\">\"prova1\"<\/span><span style=\"color: #339933;\">:<\/span> <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000ff;\">\"Questa \u00e8 la prima prova in italiano\"<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #b1b100;\">break<\/span><span style=\"color: #339933;\">;<\/span>\n\n\t\t<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000ff;\">\"prova2\"<\/span><span style=\"color: #339933;\">:<\/span> <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000ff;\">\"Questa \u00e8 la seconda prova in italiano\"<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #b1b100;\">break<\/span><span style=\"color: #339933;\">;<\/span>\n\n\t<span style=\"color: #009900;\">}<\/span>\n\n<span style=\"color: #009900;\">}<\/span>\n\n<span style=\"color: #666666; font-style: italic;\">\/\/contenuto nel file \"lingua_en.php\"<\/span>\n<span style=\"color: #000000; font-weight: bold;\">function<\/span> lingua<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$identificativo<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\n\n\t<span style=\"color: #b1b100;\">switch<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$identificativo<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\n\n\t\t<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000ff;\">\"prova1\"<\/span><span style=\"color: #339933;\">:<\/span> <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000ff;\">\"Questa \u00e8 la prima prova in inglese\"<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #b1b100;\">break<\/span><span style=\"color: #339933;\">;<\/span>\n\n\t\t<span style=\"color: #b1b100;\">case<\/span> <span style=\"color: #0000ff;\">\"prova2\"<\/span><span style=\"color: #339933;\">:<\/span> <span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #0000ff;\">\"Questa \u00e8 la seconda prova in inglese\"<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #b1b100;\">break<\/span><span style=\"color: #339933;\">;<\/span>\n\n\t<span style=\"color: #009900;\">}<\/span>\n\n<span style=\"color: #009900;\">}<\/span>\n\n<span style=\"color: #666666; font-style: italic;\">\/\/stampa a video<\/span>\n<span style=\"color: #b1b100;\">echo<\/span> lingua<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"prova1\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Lo stesso switch lo troviamo nei due file (&#8220;lingua_it.php&#8221; e &#8220;lingua_en.php&#8221;), ma naturalmente sar\u00e0 presente solo quello del file incluso a inizio pagina, stabilito dal valore di LANG.<br \/>\nQuesto significa che se siamo in lingua italiana, sar\u00e0 estratto il valore corrispondente al case &#8220;prova1&#8221; del file &#8220;lingua_it.php&#8221;; se siamo in lingua inglese, sar\u00e0 estratto quello di &#8220;lingua_en.php&#8221;.<\/p>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">Conclusioni<\/h3>\n<p>Una volta capito il concetto, il codice risulta molto semplice. Un sito strutturato in questo modo permette <strong>una modifica semplice e veloce, e soprattutto di adattare il portale a qualsiasi lingua<\/strong> (basta creare un&#8217;ulteriore file di lingua, es: &#8220;lingua_fr.php&#8221; per il francese, e di popolarlo con gli stessi case degli altri file di lingua).<\/p>\n<p>Il mio consiglio \u00e8 di mettere sotto questa forma qualsiasi tipo di testo statico nel sito: questo perch\u00e9, in futuro, potreste aver bisogno di tradurlo o di modificarlo e per farlo vi baster\u00e0 accedere ai file delle lingue.<br \/>\nRiconosco che in grandi siti potrebbe richiedere un bel po&#8217; di tempo, per cui star\u00e0 a voi valutare se \u00e8 il caso di farlo a mano. In genere, comunque, \u00e8 nei portali pi\u00f9 grandi che troverete diverse lingue da gestire.<\/p>\n<p>In questo articolo abbiamo visto un sistema per tradurre in multilingua solo il testo statico. Nel prossimo articolo ci occuperemo della parte dinamica, cio\u00e8 di tradurre i testi estratti da database.<\/p>\n<div style=\"margin-top: 100px; margin-bottom: 20px;\"><strong><em>Copyright immagini<\/em><\/strong><\/div>\n<p>(1) http:\/\/amegliagiovani.blogspot.it\/2010\/09\/giornata-europea-delle-lingue.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Uno script da scaricare che mostra come gestire i testi statici di un sito in multilingua tramite php, in modo semplice e in previsione di aggiunte future.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[40,32,4,6],"class_list":["post-1492","post","type-post","status-publish","format-standard","hentry","category-php-mysql","tag-lingue","tag-mysql","tag-php","tag-script","cat-78-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1492"}],"collection":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/comments?post=1492"}],"version-history":[{"count":10,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1492\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1492\/revisions\/2513"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=1492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=1492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=1492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}