About
The Wisp.Extensions.I18n extension adds localization functionality to Wisp.
Warning
This extension is in early development stages. Some of the functionality described here may not be implemented yet. Expect many bugs.
Installation
- Install the
Wisp.Extensions.I18nNuGet package - Enable the I18n package in your
HostBuilder - Create a
I18n/directory in the root of your project - Generate POT Files
- Put your translated
.pofiles inI18n/ - You are ready to start translating your application!
Generating Translation Templates (POT)
To write translations, you will need a translation template (.pot) file. You can generate a POT
file by calling PotGenerator.Generate(string locale). The generator will parse your template files,
look for all usages of gettext and generate a template based on that.
To include the generation functionality in your app as a command, you can use the following snippet.
if (args is ["gen-pot", _, ..])
{
var locale = args[1];
if (string.IsNullOrWhiteSpace(locale))
{
Console.WriteLine("Usage: ./tracker gen-pot <locale>");
return;
}
var potBuilder = new PotGenerator();
Console.WriteLine("Generating...");
potBuilder.Generate(locale);
Console.WriteLine("Done");
return;
}
You can then generate template files by running dotnet run -- gen-pot <locale>.
Editing .po Files
There are many .po file editors available. We recommend Poedit.
Switching Languages
The extension includes middleware that detects the desired locale from a query parameter or headers.
By default, wisp will look for the desired locale in the ?lang query parameter and the
X-Wisp-Locale header, in that order.
For information about implementing custom locale detection, see Injecting Locale.