LazZiya / XLocalizer

Localizer package for Asp.Net Core web applications, powered by online translation and auto resource creating.
https://docs.ziyad.info
129 stars 14 forks source link

Configure XLocalizer to use XML files inside an RCL #42

Closed morgrowe closed 1 year ago

morgrowe commented 1 year ago

Hi Ziya

Hope you've been well.

I'm trying to use XLocalizer with the XML provider (XLocalizer.Xml.XmlResourceProvider), but I'm having an issue with getting the resources from the XML files at runtime.

I have two WebUi projects that need to share resources, so I have a CommonUi project (an RCL) to put them in. However, at runtime, these XML files are ignored and new ones are created in the startup project. Note in the first screen shot that there's no 'Localization' folder inside the ManagerUi project:

image

However, after I run it, this folder is created:

image

How do I tell XLocalizer to use the resource keys inside CommonUi/Resources/LocalizationResources/*.x.xml instead of ManagerUi/Resources/LocalizationResources/*.x.xml?

In case it helps. Here's how I've configured XLocalizer:

services.Configure<RequestLocalizationOptions>(opts =>
{
    var cultures = new CultureInfo[]
    {
        new CultureInfo("en")
        , new CultureInfo("cy")
    };

    opts.SupportedCultures = cultures;
    opts.SupportedUICultures = cultures;
    opts.DefaultRequestCulture = new RequestCulture("en");
    opts.RequestCultureProviders.Insert(0, new RouteSegmentRequestCultureProvider(cultures));
});

services.AddSingleton<IXResourceProvider, XmlResourceProvider>();
builder.AddRazorPagesOptions((ops) =>
{
    ops.Conventions.Insert(0, new RouteTemplateModelConventionRazorPages());
})
    .AddXLocalizer<Caforb.CommonUi.Localization.LocalizationResources.LocSource, DummyTranslator>((opts) =>
    {
        opts.ResourcesPath = $"Localization/LocalizationResources";

        opts.AutoTranslate = false;
        opts.AutoAddKeys = false;

        //opts.UseExpressMemoryCache = true;
        opts.UseExpressMemoryCache = false;

        opts.TranslateFromCulture = "en";
        opts.LocalizeDefaultCulture = true;

        ...
    });

Kind regards Morgan

morgrowe commented 1 year ago

Hi Ziya

I implemented my own IXResourceProvider in the end. It's been working very well, so I'm happy to close this issue.

Thanks Morgan

LazZiya commented 1 year ago

Hi @morgrowe

Hope you are doing well .

Sorry for the late reply I am so busy with multiple projects nowadays :( and glad to see you solved your problem with your own implementation :)

I will be glad if you can also share what you did differently to solve the problem, maybe this can help someone else in the feature.

Best regards, Ziya