{"id":167,"date":"2013-08-26T22:12:06","date_gmt":"2013-08-26T20:12:06","guid":{"rendered":"http:\/\/www.zellot.at\/blogs\/?p=167"},"modified":"2013-08-26T22:14:26","modified_gmt":"2013-08-26T20:14:26","slug":"language-switch","status":"publish","type":"post","link":"https:\/\/www.zellot.at\/blogs\/2013\/08\/26\/language-switch\/","title":{"rendered":"Language Switch"},"content":{"rendered":"<p>Heute stelle ich eine L\u00f6sung f\u00fcr einen Language switch im Zend2 Framework mit Ajax und Cookies. <\/p>\n<p>Zu allererst muss die module.config.php Datei angepasst werden. In der Sekeletton Application von Zend2 ist der Translator bereits Konfiguriert und im Service Manager eingebunden. Ich habe lediglich die \u00dcbersetzungsdatei auf ein php &#8211; Array umgestellt: <\/p>\n<p>(<i>type => phparray<\/i> und <i>pattern => %s.php<\/i>)<br \/>\n<pre><code class=\"preserve-code-formatting\">\n&nbsp;&nbsp;&nbsp;&nbsp;&#039;service_manager&#039; =&gt; array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;factories&#039; =&gt; array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;translator&#039; =&gt; &#039;Zend\\I18n\\Translator\\TranslatorServiceFactory&#039;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;&nbsp;&#039;translator&#039; =&gt; array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;locale&#039; =&gt; &#039;de_DE&#039;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;translation_file_patterns&#039; =&gt; array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;type&#039;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; &#039;phparray&#039;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;base_dir&#039; =&gt; __DIR__ . &#039;\/..\/language&#039;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;pattern&#039;&nbsp;&nbsp;=&gt; &#039;%s.php&#039;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;&nbsp;),\n<\/code><\/pre><\/p>\n<p>Au\u00dferdem habe ich f\u00fcr die Cookies die Module Klasse modifiziert:<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\nclass Module\n{\n&nbsp;&nbsp;&nbsp;&nbsp;public function onBootstrap(MvcEvent $e)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$e-&gt;getApplication()-&gt;getServiceManager()-&gt;get(&#039;translator&#039;);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$translator = $e-&gt;getApplication()-&gt;getServiceManager()-&gt;get(&#039;translator&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isset($e-&gt;getRequest()-&gt;getCookie()-&gt;language) &amp;&amp; $e-&gt;getRequest()-&gt;getCookie()-&gt;language != &#039;&#039; &amp;&amp; $e-&gt;getRequest()-&gt;getCookie()-&gt;language != null){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$language = $e-&gt;getRequest()-&gt;getCookie()-&gt;language;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$translator-&gt;setLocale($language)-&gt;setFallbackLocale(&#039;en_EN&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp; }\n}\n<\/code><\/pre><\/p>\n<p> Hierbei wird der Translator geladen. Weiters wird in der onBoostrap &#8211; Methode der Request abgefangen und ausgelesen. Ich hole mir mithilfe von getCookie() die Cookies aus den Request. Warum hier konkret language geholt wird werden wir anschlie\u00dfend im Controller sehen. Wenn die Sprache gesetzt worden ist wird sie dem Translator \u00fcbergeben bzw. die Locale gesetzt. Also jene Variable die die Sprache bestimmt. Als Fallback wird hier z.B. Englisch angenommen.\n<\/p>\n<p>Weiters ben\u00f6tigen wir f\u00fcr die L\u00f6sung einen Controller der uns den Cookie setzt. ich habe ihn hier in einen ApiController von mir geladen der Json f\u00fcr den Ajax Call als Antwort zur\u00fcckliefert.\n<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\nclass LanguageApiController extends AbstractActionController\n{\n\n&nbsp;&nbsp; public function indexAction(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$language = htmlspecialchars($this-&gt;getRequest()-&gt;getQuery(&#039;language&#039;, &#039;de_DE&#039;));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$cookie = new SetCookie(&#039;language&#039;, $language);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;getResponse()-&gt;setStatusCode(Response::STATUS_CODE_201);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;getResponse()-&gt;getHeaders()-&gt;addHeader($cookie);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new JsonModel(array(&#039;cookiecreated&#039;=&gt;&#039;ok&#039;, &#039;language&#039;=&gt;$language));\n&nbsp;&nbsp; }\n}\n<\/code><\/pre><\/p>\n<p>\nHier wird, wie wir sp\u00e4ter noch sehen werden, einfach die Sprache mit einen GET Parameter gesetzt. Es wird ein Cookie erstellt der &#8218;language&#8216; auf den ausgelesen Wert speichert. Anschlie\u00dfend wird er der Response \u00fcbergeben und dem Header des Requests hinzugef\u00fcgt. Zum Schluss ben\u00f6tigen wir noch das notwendige JavaScript um die Sprache umzustellen.\n<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n$(document).ready(function(){\n\n&nbsp;&nbsp;&nbsp;&nbsp;$(document).on(&#039;click&#039;,&#039;a[data-action|=languagechanger]&#039;,function(event){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.preventDefault();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$.ajax({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: baseUrl + &#039;\/languagesetter&#039;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data: &#039;language=&#039;+$(this).attr(&#039;data-value&#039;)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).always(function(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; location.reload();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;})\n\n});\n<\/code><\/pre><\/p>\n<p>Mit dieser einfachen Methode wird nun der Controller aufgerufen und der Cookie gesetzt. Anschlie\u00dfend wird die Seite neu geladen und wir haben die Sprache im Cookie gespeichert. Wir k\u00f6nnen nun einfach in der View die translate Methode des Viewhelpers aufrufen.\n<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n## \u00fcbersetzungsfile\nreturn array(\n&nbsp;&nbsp;&nbsp;&nbsp;&#039;Search&#039;=&gt;&#039;Suche&#039;, \n....);\n\n## in der view\necho $this-&gt;translate(&#039;Search&#039;)\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Heute stelle ich eine L\u00f6sung f\u00fcr einen Language switch im Zend2 Framework mit Ajax und Cookies. Zu allererst muss die module.config.php Datei angepasst werden. In der Sekeletton Application von Zend2 ist der Translator bereits Konfiguriert und im Service Manager eingebunden. Ich habe lediglich die \u00dcbersetzungsdatei auf ein php &#8211; Array umgestellt: (type => phparray und [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,11,3,8,4],"tags":[48,49,50,47,19],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-javascript","category-jquery","category-php","category-rest","category-zend-2","tag-cookie","tag-javascript-2","tag-jquery-2","tag-sprachumstellung","tag-zend2"],"_links":{"self":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":4,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts\/167\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}