Welcome Guest, Not a member yet? Register   Sign In
localisationin .js files?
#1

[eluser]Bramme[/eluser]
Hey everyone

I'm working on a little something something that uses different bits of javascript. Now I wanted to start with localisation right from the start (so I don't end up having to go through my code again :p ) but hit a bit of a snag:

I have some language strings in a .js file. What's the best way to handle these?

I know I can create a seperate file for the javascript, with a simple object in it, but ideally, I'd like to use the same language file. Or is this unrealistic?
#2

[eluser]Narkboy[/eluser]
Can't think off-hand of a solid way to do this but did have an idea.

If you have the js in a php file rather than a .js, you can get PHP to insert the correct language strings directly into the js code. Means putting js into script tages rather than linking it, which I don't really like.

Second thought, use AJAX to request JSON based on the back-end language file, then use that to display text as required. Increases you're server calls, and probably puts a small delay on the availability of the language when the page loads, but I don't know if that would be an issue or not.

/B

Edit - filter dosen't like angualr brackets..
#3

[eluser]Bramme[/eluser]
Thanks for the suggestion. I posted this question on Forrst too, somebody there gave me a good idea:"Put the strings in a JSON file? Readable by both JS and PHP." Which would actually work nice.

I use another way at the moment, that works pretty well for me too: I made a js controller that outputs a javascript/application header, in the controller methods I simply echo the javascript I need in json objects. This way, I created two objects: lang and config, which lets me use things like base_url() etc too.

If anyone's interested, I can post some more code.




Theme © iAndrew 2016 - Forum software by © MyBB