0

i must add a language support to an existing Zend 1.12 project. Is it possible to use a html select box to change the language? The only way that i know is to use the "onChange" attribute of the select box to insert a specific js function which can analyze the selected option.

Is it possible to initiate a zend_translate language switch in javascript?

Tomben
  • 73
  • 7

2 Answers2

1

You need to either reload the website on select change or perhaps make an ajax call that will load new html.

Something like this should work for you (and I assume you have language code in your routing):

HTML code:

<select id="languageSwitch">
    <option value="en">English</option>
    <option value="de">Deutsch</option>
</select>

JS (with jQuery):

$('#languageSwitch').change(function() {
    window.location = '/' + $(this).val() + '/';
});

This will reload your page with /en/ or /de/ (based on the choice) appended to the domain name, eg.

http://www.yourwebsite.com/en/

http://www.yourwebsite.com/de/

If you need help setting up your routing, this video should help you.

Wojciech Zylinski
  • 1,995
  • 13
  • 19
0

This sounds like you need locale switching functionality within your application, maybe check out the manual definitions of Zend locales, and store the active locale in the user's session: http://framework.zend.com/manual/1.12/en/zend.locale.functions.html

I would then build a "Pre-Dispatch Plugin" to recognise and modify the active session locale to reflect a GET parameter defined in a drop-down menu selection. There are numerous ways of passing the locale modify parameter, wether you use the "route" approach in the above answer, a simple GET parameter with links, or if you are set on the drop-down locale approach - you could try JQuery, an example: http://jetlogs.org/2007/09/03/jquery-select-boxes-and-change-events/

The easiest is as you said, is an on-change snippet - but that is ugly in the perspective of standards as there is an expectation ont he browser to recognise onchange attributes.

The choie is yours.

Dan Belden
  • 1,199
  • 1
  • 9
  • 20