1

I have localization written for my wabsite, in PHP. Since I'm also working with Javascript (jQuery), to dynamically add some tags, I have problems with localization.

For example: I add comments dynamically. The user can click "Show more comments", to show the next page of comments. If all comments are shown, the text changes to "All comments displayed". But this way, nothing is localized, because it's only using Javascript.

How could I transfer the localization to javascript? My current idea is to create a PHP script, that would accept a GET variable, for example "all-comments". Then it would search for the localized version of that, and return a JSON string. Is there any better way of doing it?

4
  • How are you currently doing it? Commented Dec 24, 2012 at 23:45
  • Well, currently all the strings in Javascript are outputted in English, there is no JS Localization yet. Commented Dec 25, 2012 at 10:12
  • "I have localization written for my wabsite, in PHP." HOW. Commented Dec 25, 2012 at 10:13
  • Oh, I have a PHP file for every language. In each file I define a variable: $l = new Language('en'); then, I add each string to translate. For example: $l->addWord('welcome_msg', 'Welcome to my website!');. Then, when I have to echo the string, I just call it: echo $l->welcome_msg;. Commented Dec 25, 2012 at 13:00

1 Answer 1

2

There are a number of ways to approach this.

  • If you want to have all localization handled in server-side PHP logic, you could have all calls that change DOM content pull that new localized content from the server (via AJAX).
  • Alternatively, if you don't want server calls but still want all localization generated via PHP, you could pre-populate a javascript object with a set of localizations to use for whatever content might need to be displayed on that page.
  • If you want to keep javascript localizations within javascript, you could have localized js files on the server (or at least have localized javascript language files that for example contain a javascript object with all localizations).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.