Question

In order to localize strings used within my javascript, I want scan all my js files for such strings.

I am using a t() function to request string translations as follows:

t("Hello world");

or with dynamic portions:

t("Hello @user", {"@user": "d_inevitable"});

I want to detect all calls to the t() function and thus gather the strings contained in the first argument in a php "build" script, but skipping the following:

function foo(t) {
   t("This is not the real t, do not localize this!");
}

function bar() {
    var t = function(){}; //not the real t either...
}

function zoo() {
    function t() {
       //This also isn't the real t() function.
    }
}

t("Translate this string, because this is the real t() in its global scope");

So the simple rule here is that the t function being invokes must be in global scope in order for the first argument to qualify as a translation string.

As a rule, dynamic runtime data as first argument is not allowed. The first argument to t() must always be a "constant" literal string.

I think php codesniffer will help me do it, however all the documentation I could find on it is about enforcing code standard (or detecting violations of it). I need lower level access to its js lexer.

My question is:

  1. Would the php codesniffer's js lexer be able to help me solve my problem?
  2. If so how do I access that lexer?
  3. Are there any other php libs that could help me find the calls to t()?

Please do not suggest stand-alone regular expressions as they cannot possibly solve my problem in full.

Thank you in advance.

Était-ce utile?

La solution

What you are describing is basically a coding standard. Certainly, ensuring strings are localised correctly is part of many project standards. So I think PHPCS is the right tool for you, but you will need to write a custom sniff for it because nothing exists to do exactly what you are after.

The best thing to do is probably clone the PHPCS Git repo from Github and then create a new directory under CodeSniffer/Standards to contain your custom sniff. Let's say you call it MyStandard. Make sure you create a Sniffs directory under it and then a subdirectory to house your new sniff. Take a look at the other standards in there to see how they work. You'll also find it easier to copy an existing ruleset.xml file from another standard and just change the cotent to suit you. if you don't want to include any other sniffs from anywhere (you just want to run this one check over your code) then you can just specify a name and description and leave the rest blank.

There is a basic tutorial that covers that.

Inside your sniff, you'll obviously want it to check JS files only, so make sure you specify that in the supportedTokenizers member var (also in the docs). This will ensure PHP and CSS files are always ignored.

When you get down to the actual checking, you'll have full low-level access to the parsed and tokenised content of your file. There are a lot of helper functions to check things like if the code inside other scopes, or to help you move backwards and forwards through the stack looking for bits of code you need.

TIP: run PHPCS using the -v option to see the token output on your file. It should help you see the structure more easily.

If you want to really do things properly, you can even create a nice unit test for your sniff to make sure it keeps running over time.

After all this, you'd check your code like this: phpcs --standard=MyStandard /path/to/code

And you can use a lot of integrations that exist for PHPCS inside code editors.

You might decide to add a new more sniffs to the standard to check other things, which you can then do easily using your ruleset.xml file or by writing more custom sniff classes.

I hope that helps a bit. If you do decide to write your own sniff and need help, just let me know.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top