Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Extract all the localizable strings from an AspNetCore project.

License

Notifications You must be signed in to change notification settings

wis3guy/StringLocalizerExtractor

Repository files navigation

LocalizerStringExtractor

Extract all the localizable strings from an AspNetCore project.

This tool uses the Roslyn compiler to retrieve possible localizable strings that are using the IStringLocalizer (from the AspNetCode Localization project) as well as the Display and DisplayName attributes (as long as the class that defined the properties ends with the suffix "Model" or "ViewModel"). To run the tool from command line, you can use StringLocalizerExtractor.exe [source (defaults to ./)] --output [the output path with filename].

The tool navigates the source directory recursively. Only the files that end with .cs or .cshtml (case insensitive) will be analyzed by the tool. At the same time, the tool omits the following directories:

  • bin
  • obj
  • .git
  • .vs
  • node_modules

Example

IStringLocalizer localizer = ...
var helloWorld = localizer["Hello world"];
var helloUser = localizer.GetString("Hello {0}!", "user");

In the above example, the tool will extract both keys (Hello world and Hello {0}!)

Using attributes

[Display(Name = "User name")]
public string UserName{get;set;}

[DisplayName("Electronic mail")]
public string Email{get;set;}

Output

Right now, the tool only generates POT files, but I'm going to provide more formats.

POT

The POT file this tool generates adds the reference file where every key is defined.

A limitation occurs with the Razor files, because the files code is analyzed, instead of the razor syntax, the reference added to the POT file is not the reference on the razor file.

Limitations

Because the project does not compiles the source, it is not possible (as long as I'm aware) to determine the type of a property or variable using Roslyn or even to ensure it is the expected type, any property or variable which name ends with Localizer (for example myLocalizer) will be considered to be a IStringLocalizer and any "GetString" or "[]" call made to that object will be considered a valid localizable string, unless the key is another variable or numeric.

Because some variables may not be initialized or the value may be dynamic myString[myVar] is currently ignored.

About

Extract all the localizable strings from an AspNetCore project.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages