Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

new rule: no-unlocalized-strings #95

Closed
HamletDRC opened this issue Jan 18, 2016 · 2 comments
Closed

new rule: no-unlocalized-strings #95

HamletDRC opened this issue Jan 18, 2016 · 2 comments
Milestone

Comments

@HamletDRC
Copy link
Member

Create a rule to find instances of unlocalized strings.

An unlocalized string is defined as:

  • A double-quoted string (single-quoted strings are assumed to be technical)
  • and, not passed to a localization function

Options:

  • localization-function - the string name of a localization function. For example, "localize".
  • localization-expression - the string text of a localization expression. For example, "nls.localize"

Examples:

// assume localization-function is "localize"
localize("Title");     // ok (localization function used)
nls.localize("Title"); // ok (localization function used)
var title = 'Title';   // ok (not double quotes)
var title = "Title";   // violation 
l10n("title");         // violation
nls.l10n("Title");     // violation

// assume localization-expression is "nls.localize"
nls.localize("Title");     // ok (localization function used)
xxx.localize('Title');     // ok (not double quotes)    
xxx.localize("Title");     // violation
localize("Title");         // violation
nls.l10n("Title");         // violation
@dbaeumer
Copy link
Member

I implemented the new rule. The rule in named no-unexternalized-strings. I changed the name because check is about externalization. It doesn't check if the strings is actually localized (e.g. translated into a different language). The behavior is as follows:

  • single quoted strings and template strings are ignored.
  • by default all double quoted strings are treated as non externalized.
  • there is no default function setup.
  • messages passed to the localize function must be literals. (e.g. "Hello" + "World" is not allowed since it can't be handled correctly in RTL languages).

The rule can be controlled using the following object literal:

{
    /** list function signatures that localize a string */
    "signatures": string[];
    /** defines which argument denotes the message */
    "messageIndex": number;
    /** list signatures which are ignored when  double quoted strings */
    "ignores": string[]
}

Given the following options:

{
    "signatures": ["localize", "nls.localize"],
    "messageIndex": 1,
    "ignores": ["require"]
}

the rule will validate code like this:

localize("key", "message");         // ok (localization function used)
nls.localize("key", "message");     // ok (localization function used)
var title = 'Title';                // ok (not double quotes)
var title = "Title";                // violation 
l10n("title");                      // violation
nls.l10n("Title");                  // violation
var fs = require("fs");             // ok (require is in ignore list)
localize("key", "mes" + "sage");    // violation (messages should be literals)

dbaeumer added a commit to dbaeumer/tslint-microsoft-contrib that referenced this issue Jan 20, 2016
@HamletDRC HamletDRC modified the milestone: Pre 2.0.9 Aug 15, 2016
@Sayan751
Copy link

Is there a way to ignore the lint error for new Error("My non-localzied error"), without using single-quotes or template string?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants