On Nov 9, 7:32 pm, Miles J <[email protected]> wrote:
> No there isn't really. This is also a problem for any language, and
> unless you have back-end parsed JS files it's not possible. The only
> solution is so place a JS literal object in your page source
> containing the message strings:
>
> <script>
> var Msg = {
>         username: '<?php echo __('username'); ?>',
>         someKey: '<?php echo __('moreKeys'); ?>'};
>
> </script>
>
> And then reference it in your JS:
>
> alert(Msg.username);

OR, the other only solution is, to only don't embed things in your
page source. And instead:

1) use __() in your js files
2) load a js file with that function defined, like this:
https://github.com/AD7six/mi_js/blob/master/i18n.js
3) Define a js object (i18n in the above js file) with your message
definitions in it any way you can/like.

That's basically it, and you'll see the same not-only technique used
with, for example, the jquery date picker - see how it works if you
don't understand the above explanation.

The simplest cakephp way to generate those i18n js objects is similar
to what Miles suggested: create a controller and route /js/i18n.xx.js
to it. and define a view which returns valid js like this:

i18n = {
  'foo' => '<?php __d('javascript', 'foo'); ?>',
 ..
};

Then you don't need to do anything special to be able to generate your
po files and use translations in your js files. You only need to
request /js/i18n.xx.js to change the language of things that come out
of your js files. If you want to parse out the __() calls from your js
files - that's not hard either, some reference (but if you don't
understand - just do the above):

https://github.com/AD7six/mi_development/blob/master/vendors/shells/tasks/mi_extract.php#L175
https://github.com/AD7six/mi_js/blob/master/i18n.lang.js

AD

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to