4

I develop a website (in wordpress) that it is intended for users who speak different languages. The main content of the site is the same (not divisible by languages). But the interface of the site (such as menus, and a few other small things) should be in two different languages​​.
I do not want to use gettext in PHP level. Instead, I want to translate certain things at the level of JavaScript.

Of course I can do an array of words in both languages​​, mark the html elements that require translation (like: <span translate='true'>Hello world!</span>) , and when the user presses a button of his language, the script will look for a match of words, and replace the contents of the elements.

But I want to ask, is there a better way to achieve this?

Shimon S
  • 4,048
  • 2
  • 29
  • 34

3 Answers3

2

If you only plan on having translations of menus, sidebars and other "hard coded" things in your user interface, I would simply serve a different (partial) HTML template that is already translated into that specific language. You could have for example a header_en.html, a header_fr.html and so on.

Translating with JavaScript after the page has been served is too complex for what you are trying to achieve in my opinion.

HttpNinja
  • 394
  • 1
  • 16
  • 2
    If you really want to use JavaScript, have a look at this Stackoverflow question: http://stackoverflow.com/questions/2400106/translation-in-javascript-like-gettext-in-php – HttpNinja Jul 05 '13 at 15:00
  • Thank you for your comment. Really there is another question there, but certainly one of the answers (http://stackoverflow.com/questions/2400106/translation-in-javascript-like-gettext-in-php#answer-2400129) there might help in my case. – Shimon S Jul 06 '13 at 18:29
0

I'd recommend doing things the Wordpress way, even if you don't like the Wordpress way.

See wp_localize_script for the Wordpress way to do this.

This means using Gettext, but in JavaScript. I think that should tick your box unless you've got a real problem with Gettext for some reason.

Tim
  • 8,036
  • 2
  • 36
  • 52
0

You can use http://jsgettext.berlios.de/ to do the translation in JavaScript.

liftarn
  • 429
  • 3
  • 21