I18Next.Net
I18Next.Net copied to clipboard
Allow customization on localization files findings
Hi, I would like to have my localization files structured like this:
- locales/localization_en.json
- locales/localization_de.json
The default JsonFileBackend has a method FindFile which is private. If this method (or even other methods) are defined protected virtual then the only effort is to implement the methods that are necessary. And re-use the code from the JsonFileBackend. One more additional benefit is that if you make changes to this class in future release, my custom class will inherit these changes automatically when I upgrade the I18Next NuGet package. Please let me know what you think. Thank you
I just modified the source code and then implemented my own custom version like below:
public class CustomJsonFileBackend : JsonFileBackend
{
public CustomJsonFileBackend()
{
}
public CustomJsonFileBackend(string basePath) : base(basePath)
{
}
public CustomJsonFileBackend(ITranslationTreeBuilderFactory treeBuilderFactory) : base(treeBuilderFactory)
{
}
public CustomJsonFileBackend(string basePath, ITranslationTreeBuilderFactory treeBuilderFactory) : base(basePath, treeBuilderFactory)
{
}
protected override string FindFile(string language, string @namespace)
{
var path = Path.Combine(_basePath, @namespace + $"_{language}.json");
if (File.Exists(path))
return path;
path = Path.Combine(_basePath, @namespace + $"_{BackendUtilities.GetLanguagePart(language)}.json");
return !File.Exists(path) ? null : path;
}
}
And it works like a charm.