unused_l10n
Reports a localization entry that nothing in the project uses. Delete it from the files of every language.
- Kind
- cross-file check
- Tier
- advisory
- Host
- command-line program only (
assay cross-file) - Fix
- none
What it reports
Section titled “What it reports”A localization entry that nothing in the project uses, once, not once per language. Two ways of localizing a Flutter app are judged, each where the project uses it:
- Flutter’s localization generator (
flutter gen-l10n, ARB files): a getter or method of the generated localization class, one for each key of the template ARB file. - easy_localization (JSON translation files): a key of the translation files, such as
home.title.
A project that uses neither gets no result and no error. No other localization package is judged.
| Localization | Where the result stands | Message | How to fix |
|---|---|---|---|
flutter gen-l10n |
at the entry’s name in the generated base class (String get title; in lib/l10n/app_localizations.dart) |
“Nothing in the project uses unusedEntry of the generated localization class AppLocalizations, the key unusedEntry of lib/l10n/app_en.arb.” |
“Delete unusedEntry from lib/l10n/app_en.arb and the other ARB files if nothing needs it, and generate the localizations again.” |
| easy_localization | at the key’s name in a translation file (assets/translations/en.json, line and column of the key) |
“Nothing in the project uses the translation key home.title.” |
“Delete home.title from every translation file in assets/translations if nothing needs it.” |
A use anywhere in the project counts, a test included. For easy_localization, any string literal equal to the key counts as a use, wherever it is. An entry of a public class in a publishable package is never reported: other packages can use it.
An entry that nothing shows is still translated, reviewed and shipped in every language, and whoever finds it assumes a screen still shows it. Agents leave such entries behind: the label of a rewritten screen, a message replaced by a better one under a new key. The check is advisory: an entry can be kept on purpose, for a screen that is coming, and each project decides.
Configuration
Section titled “Configuration”Under the assay: key of the project’s root analysis_options.yaml. The check is on under every preset unless switched off.
| Key | Default | Effect |
|---|---|---|
assay.cross_file.unused_l10n.enabled |
true |
false switches the check off. The short form unused_l10n: false means the same. |
assay.cross_file.unused_l10n.exclude |
[] |
Path globs, read from the project root, where the check places no result: the generated Dart files, or the translation files. |
assay: cross_file: unused_l10n: exclude: - lib/l10n/** # silence the generated classes - assets/translations/** # silence the translation filesRunning it
Section titled “Running it”assay cross-file runs every cross-file check on the whole project, and assay cross-file unused_l10n runs this one alone (see assay cross-file). It does not run in the editor or in assay check. In the gate its results never fail the gate (see In the gate).
Suppressing it
Section titled “Suppressing it”A suppression comment does not silence a cross-file check, and a JSON file has no comments. Use the check’s exclude.
Details
Section titled “Details”All cases
The result
Section titled “The result”The result is a violation with the source cross_file, the rule unused_l10n and the advisory tier, with a fingerprint made of the line it stands on.
The ARB file in the message is the template that the package’s l10n.yaml names (arb-dir and template-arb-file), or the generator’s default lib/l10n/app_en.arb. Where that file does not exist, the message leaves it out and the how-to-fix says “the template ARB file”.
A result of easy_localization stands in a JSON file, not a Dart file. Every reporter carries it like any other result: the console lists it under the file, and SARIF, GitLab and GitHub name the file and the line.
A project that uses both ways of localizing is judged for both. An unused entry costs more than other dead code: one dead entry is a dead line in every language, and no single file shows it; only the whole project does.
Flutter’s localization generator
Section titled “Flutter’s localization generator”How the class is recognised
Section titled “How the class is recognised”By what the generator writes, not by its name (output-class chooses the name, output-localization-file the file). A generated localization class is an abstract class that declares:
- an instance field
localeNameof typeString; - a static field
delegatewhose type isLocalizationsDelegateof the class itself; - a static field
supportedLocales; - a static method
of.
Its entries are the abstract instance getters and methods it declares that return String: one per key of the template ARB file, a method for an entry with placeholders. localeName, of, delegate, localizationsDelegates and supportedLocales are not entries. A hand-written abstract class of messages is not judged. The ARB files are read only to find the template, so a key added to them is judged once the class is generated again.
The output of the generator’s older synthetic package (package:flutter_gen/gen_l10n/…, under .dart_tool/) is outside the project and is not judged.
What counts as a use
Section titled “What counts as a use”- A reference to the entry through the class:
AppLocalizations.of(context)!.title,AppLocalizations.of(context)?.title, through your own extension (context.l10n.title, wherel10nreturns the class), or through a variable or parameter of the class’s type. - A reference through any subtype of the class: the per-language classes the generator writes (
AppLocalizationsDe().title,AppLocalizationsEnUs), and your own class that extends one, such as a fake in a test. - A use from a test.
- A name used without an element anywhere in the project, such as
strings.titleon adynamicvalue.
What the generated files themselves refer to does not count: the base class’s file, and each file it imports that declares a per-language class. The per-language classes override every entry, and an override is no use.
Never reported
Section titled “Never reported”- An entry of a class in a file that is not judged (it does not compile), that
unused_filesreports, or that only a condition of an import names. While the generated class does not compile, also whereassay.excludeoranalyzer.excludecovers it, the gate andassay baseline updateremove no baseline entry in it. - An entry of a public class in a publishable package (one without
publish_to: none): other packages can use it. See The two package types.
easy_localization
Section titled “easy_localization”Where the translation files are
Section titled “Where the translation files are”Where the project creates the EasyLocalization widget of the package easy_localization, its path argument names the directory of the translation files. The path is relative to the root of the package that creates the widget (the directory with its pubspec.yaml).
The path reaches the widget’s constructor in any of these ways, each read where the path is written:
- an instance creation:
EasyLocalization(path: …); - a subclass that hands it to the super constructor:
class AppLocalization extends EasyLocalization { AppLocalization({required super.child}) : super(path: _path, …); static const _path = 'assets/translations'; }; - a subclass that forwards a
super.pathparameter, read at its own instance creation:AppLocalization(path: 'assets/translations', …); - a redirecting constructor,
: this(path: …), or a redirecting factory.
The widget is recognised by the library that declares it (package:easy_localization/), not by its name alone. A subclass that passes a path from a parameter of its own that is not super.path (: super(path: folder)) gives a path that is not a constant.
The translation files are the files directly in that directory whose names end with .json and that hold a JSON object: what the package’s default loader reads (assets/translations/en.json, en-US.json).
The path is read where it is a constant string: a string literal, or a constant variable whose value is one.
- Where the
pathof anyEasyLocalizationof the project is not a constant, nothing is reported for easy_localization at all, and the check says nothing about it. - Where the directory does not exist or holds no JSON translation file (a loader of YAML, CSV or the network), nothing is reported for that setup.
- Whether the pubspec declares the directory under
flutter: assets:does not matter. - Where a
.jsonfile of the directory cannot be read, nothing is reported for that directory. The file holds no valid JSON, as a file being edited may for a moment, or its objects and lists are nested more than 100 deep. Which file holds a key decides where the key is reported, so with one file unread no key can be placed. The gate andassay baseline updatethen remove no baseline entry of any translation file of that directory. Once the file can be read again, the results are as before. - A JSON file that holds something other than an object, such as a list, is not a translation file and is left out.
What an entry is
Section titled “What an entry is”A key of a translation file, with the keys of the objects it is nested in joined by dots in front, as easy_localization looks it up: home.title for {"home": {"title": "…"}}. An object whose keys hold one of the forms zero, one, two, few, many, other, male or female is one entry, the key of the object: easy_localization looks it up with plural() or a gender, not by its forms.
A key exists when one of the translation files holds it. It is reported once:
- in the file of the
fallbackLocale, where that file holds it; - else in the file of the
startLocale; - else in the first file, by name order, that holds it.
A locale is read where it is written Locale('en') or Locale('en', 'US'): the file en.json or en-US.json, and en.json where there is no en-US.json. A key that only one language has is reported in that language’s file.
What counts as a use
Section titled “What counts as a use”-
Any string literal of the project equal to the key, wherever it is and whatever it is handed to:
'home.title'.tr(),tr('home.title'),context.tr('home.title'),'items'.plural(3),Text('home.title').tr(), a constant, a map, a test. Strings in tests count, and so do strings in generated code, except in what easy_localization’s generator wrote.This is the quietest reading, the one the asset checks use for paths. The check does not follow a key through variables, parameters and widgets, so it does not try to tell which strings reach
tr. The cost: a key equal to an unrelated string of the project ('title'as a JSON field of a model) is never reported. -
A string equal to a key that the entry is nested in keeps every entry below it:
'errors'keepserrors.networkanderrors.timeout. A string that names a form of an entry, or a key below it, uses the entry:'items.one'usesitems. -
A constant of the generated keys class (
LocaleKeys.home_title, fromdart run easy_localization:generate -f keys) used anywhere else counts as its value,'home.title'. The generated files themselves (the keys class, and the loader-f jsonwrites) are recognised by their first comment, “This is code generated via package:easy_localization/generate.dart”. Their own strings do not count, so a key that only the keys class names is reported. -
A key built of parts, such as
'errors.$code'.tr(), keeps alive every key that matches its constant parts: here every key that starts witherrors.. Such a string counts when either:- its constant text holds a letter or a digit, and a dot or an underscore stands next to one of its interpolations (
'errors.$code','${screen}_title'); or - it is handed to easy_localization itself: the receiver of
tr()orplural(), or the key oftr(),plural()orcontext.tr().
Other interpolated strings, such as
'$count items'or'${name}s', name no key. A sum of strings with+is read the same way, each operand that is not a constant string standing for an interpolation:('errors.' + code).tr()andtr('errors.' + code)keep the keys undererrorsalive, while'unused' + codenames no key. A string in parentheses handed to easy_localization counts as handed to it. - its constant text holds a letter or a digit, and a dot or an underscore stands next to one of its interpolations (
-
A link in a text of a translation file, such as
"About @:brand"or"@.upper:brand", usesbrand.
A key the code builds entirely at run time, such as tr(item.labelKey), status.name.tr() or a key read from a server, is used only where a string of the project names it or a key built of parts matches it. Name such keys in a constant list, or use the check’s exclude.
The excluded generated files
Section titled “The excluded generated files”The default assay.exclude names the files Flutter’s localization generator writes by default (**/app_localizations.dart, **/app_localizations_*.dart), so that no rule or metric judges generated code. That does not empty this check. It reads those files and places its results in the generated base class whatever assay.exclude and analyzer.exclude say: many projects exclude the generated localization files from analysis, and the results stand nowhere else. Use the check’s own exclude to silence them. The result’s list of files does not list the generated file as judged.
A result in a translation file follows the ordinary rule: none is placed in a path that assay.exclude or analyzer.exclude covers, nor in a directory that holds no source of the project.
The short form switches the check off:
assay: cross_file: unused_l10n: falseIn the gate
Section titled “In the gate”The gate runs the check with the other cross-file checks: its results never fail the gate, and they pass through the baseline (see In the gate).
No fix
Section titled “No fix”No fix exists, and none is planned: delete an entry from the ARB or translation files of every language by hand, and generate the localizations again.