Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customization on localization files findings #25

Open
mahdighorbanpourptw opened this issue Mar 28, 2023 · 1 comment
Open

Allow customization on localization files findings #25

mahdighorbanpourptw opened this issue Mar 28, 2023 · 1 comment

Comments

@mahdighorbanpourptw
Copy link

mahdighorbanpourptw commented Mar 28, 2023

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

@mahdighorbanpourptw
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant