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

Multi-tenancy provider #20

Closed mkgn closed 3 years ago

mkgn commented 3 years ago

Hello,

My question is about some guidance to see whether I can change your code so I can try to modify your code to support multi-tenancy. Let me brief my setup. I am trying to keep it simple and only considering storing strings (no localized icons, images etc.)

Each tenant has it's own database and each tenant can customize their resource strings. So your table structure can be reused

Tenant identification is happening via the url pattern. example :- tenant1.domain.com , tenant2.domain.com. So the app will identitify tenant1/tenant2 and then load the correct connection string to work with the correct database for that tenant. For multitenancy i am using https://github.com/Finbuckle/Finbuckle.MultiTenant. 

So our localization setup should be able to load resources from the connection string based on the tenant that is logged in (identified via subdomain).

What I don't get is which parts in your code needs customizations to use the connection string dynamically. Basically resource should be loaded at the first request which is the only way to identify the tenant. So itll be the point whre it loads all resources fro that tenant and cache it for subsequent calls for that tenant. Ex:- a dictionary object with <key,resource collection> like <tenant1,tenant1resourcecollection>

If you can give me some pointers, I will try to manage it myself.

LazZiya commented 3 years ago

Hello @mkgn ,

Well, I will try to assist you to better understand the code of XLocalizer.

Below is a brief of the contents of each package.

XLocalizer

XLocalizer.DB

So, I think you may want to modify the caching system to have a different cache page for each tenant. Another part to customize could be in the DependencyInjection class of XLocalizer.DB, so instead of passing a DbContext you may want to pass some tenant object I'm not sure, just brainstorming :)

I recommend that instead of modifying the original code, you can extend it by implementing its interfaces (IDbResourceManager, IDbCultureManager, etc.) to provide multi-tenant support. See the docs for some more details.

Last but not least, XLocalizerOptions is another place to add extra options if necessary.

Let me know if you need any additional help,

Best, Ziya

LazZiya commented 3 years ago

Btw, all localization services IStringLocalizer, IHtmlLocalizer, RequestLocalizationOptions, ...etc. are registered as singleton services. So it will not work as you expect! may be you should modify the code to register all related singleton services as TenantSingleton. Below articles may give an idea:

LazZiya commented 3 years ago

Issue is closed due to long time no activity.

Feel free to reopen if you need any further help.

BR, Ziya