# Localize
Simple package to localize strings from json files via static source code generation.
Implemented via C# source generators
Also see example project
Add a Nuget package reference to the project file in the project you want to localize:
<PackageReference Include="kli.Localize" Version="<version>" />
Example:
{
"SampleText": "FooBar",
"Other": "Text42"
}
Give your default localization a name without specifying the culture (e.g. Locale.json
). All other localizations follow the pattern <Filename>_<CultureInfo.Name>.json
(e.g. Locale_en-US.json
for American English or Locale_en.json
for English)
In an ItemGroup
in your csproj file add an AdditionFiles
element for each default localization json file. Set the Include
attribute to the path of the file.
Example:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="kli.Localize" Version="1.0.*" />
<AdditionalFiles Include="TestLocalizations\Locale.json" />
</ItemGroup>
</Project>
This means: if you have a Locale.json
and a Locale_en-US.json
you only have to add the Locale.json
as <AdditionalFiles>
. You can add as many files as you want.
Now you should be able to locate the generated source code in your project under Dependencies/Analyzers.
Of course you can also view and debug the generated source code.
Import the namespace where you put your *.json files and use the generated code to access your localizations.
Access is based on CultureInfo.CurrentUICulture
The namespace is generated using the following pattern:
rootnamespace + relative directory structure
Since v0.8 this behaviour can be overriden [see 'From version 0.8'](#From version 0.8)
All properties that are not string or object will be ignored.
{
"Number": 4.2,
"Bool": true,
"Null": null,
"Array": [1,2,3]
}
It is now possible to use JSON objects in the localization files. During generation, the structure is mapped as a nested class for access
{
"SomeText": "some text",
"Sub":
{
"FileNotFound": "Not found",
"DivideByZero": "x / zero"
},
"UI":{
"LabelOne": "One",
"LabelTwo": "Two",
"Login": {
"LabelUserName": "User",
"LabelPassword": "Pass"
}
}
}
<JsonReaderException.Message>
Json property key must be a valid C# identifier
Json property value must be an object or a string
All diagnostics came with LinePostion (linenumber & column)
It is now possible to override the namespace and the class/file name that will be generated:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="kli.Localize" Version="0.8.*" />
<AdditionalFiles Include="Localizations\Locale.json"
NamespaceName="Namespace.of.your.choice"
ClassName="MyClassName" />
</ItemGroup>
</Project>
From which the following is generated:
namespace Namespace.of.your.choice
{
...
public sealed class MyClassName {
...
Directly after including the package sometimes the tooling (Visual Studio) gets stuck. If you encounter any problems with source generation try to restart Visual Studio and/or check the build log for warnings/errors.
Feel free to create an Issue