Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8105261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:06:56+00:00 2026-06-06T00:06:56+00:00

In order to localize strings used within my javascript, I want scan all my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:06:58+00:00Added an answer on June 6, 2026 at 12:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Order deny,allow deny from all allow from xx.xx.x.xx RewriteRule . - [F] I have
The order of execution of html/javascript , as far as I know , is
<% @recently_active_objects = Activity.find(:all, :limit => 10, :order => id DESC) %> <% @recently_active_objects.group_by{
I have a function that parses a string containing a date(and/or time) e.g. 2009-12-10
What is the best approach in order to localize images (buttons and other content)
Looking for an implementation for C++ of a function like .NET's String.Format. Obviously there
In my WPF 4.0 desktop-based application in order to localize my application (e.g. English
In order to fix a problem with an ajax request not working in IE,
order by in query is taking to much time in MySQL. SHOW PROFILES shows
Order by descending is not working on LINQ to Entity In the following Query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.