Extract javascript gettext in Poedit?

2019-06-14 09:31发布

问题:

I have used "GNU gettext" with PHP and it did a greate job by using poedit I was able to extract the PO file format

but when I tried the same thing for my javascript inside my HTML "script" tag I didn't work even more I can't see any javascript support.

poedit support almost all languages except javascript or I am doing it In a wrong way can anyone provide working example for javascript?! i followed all example in almost 20 languages using poedit and all worked except javascript

all working examples from GNU gettext found in

gettext-0.19.7.tar.gz

http://ftp.gnu.org/gnu/gettext/

gettext-0.19.7\gettext-tools\examples

this is php examlpe which works i need to do the same for javascript

    #!@PHP@ -q
    <?
      // Example for use of GNU gettext.
      // This file is in the public domain.
      //
      // Source code of the PHP program.

      setlocale (LC_ALL, "");
      textdomain ("hello-php");
      bindtextdomain ("hello-php", "@localedir@");

      echo _("Hello, world!");
      echo "\n";
      echo printf (_("This program is running as process number %d."),posix_getpid());
      echo "\n";
    ?>

回答1:

Both Poedit and xgettext have support for JavaScript for some time now. Your problem is that you're embedding the code in HTML file, so it's not recognized as a JavaScript file (and xgettext, which Poedit uses, doesn't support embedded content in other languages).

Just put your JavaScript code in a separate .js file.

Notice that JavaScript code runs on the client side, so you need to load translations in JavaScript as well. A better solution for you, considering that you have just some (presumably small-ish) snippets of JS inside a PHP code, may be to generate that JS code with PHP — i.e. do the translation on PHP/server side.



回答2:

You can use this npm package to work with those files, assuming you are working from a node environment. I do not think it is possible from the browser.