OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.45k stars 2.41k forks source link

Localization reload translations if file changes #1093

Open Franklin89 opened 7 years ago

Franklin89 commented 7 years ago

I was wondering if this might be something that anyone else is interested in or if it is just me.

I am only using the Localization package at the moment from OrchadCore for my asp.net core mvc project, following the guide lines here on Microsoft Docs

If I change the po file I have to restart the application so that the change gets reflected in the application. Microsoft.Extensions.Configuration package used for loading appsettings.json has the possibility to watch the configuration file and reload if it is changed with a simple flag reloadOnChange

// Set up configuration sources.
var builder = new ConfigurationBuilder()
   .SetBasePath(env.ContentRootPath)
   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

Would this be a feature that could be implemented?

For example like this:

services.AddPortableObjectLocalization(reloadOnChange: true);
sebastienros commented 7 years ago

That would be a nice improvement.

Franklin89 commented 7 years ago

Cool I’ll see what I can come up with and try to work on a PR next week.

sebastienros commented 7 years ago

Feel free to drop on the gitter room if you need to check your design ideas.

hishamco commented 7 years ago

You read my mind @Franklin89 😄, I just need to file an issue suggesting such things, also I wanna add a Localization JSON Request Culture Provider which I already implemented [here](Localization JSON Request Culture Provider) this may a good way to setup the active culture in appsettings.json, @sebastienros if sound looks good, I will make a PR ASAP

nicojmb commented 5 years ago

Hi, any news about this, hat would be a nice improvement :)

hishamco commented 5 years ago

I was waiting for @sebastienros to approve this, so I can make a PR

sebastienros commented 5 years ago

I think it would be better to have the feature that handles custom localization entries from the db and editable in the admin. Files should not be updated while the application is running, that's a more minor issue IMO.

hishamco commented 5 years ago

Is this mean that there's should be a checkbox to enable reload translation , and this should take affect after the application restart?

dev-masih commented 5 years ago

any update regarding reloading translation files without need to restart the whole application?

dev-masih commented 5 years ago

Is there at least a method or a way to force localization to reload programmatically?

Skrypt commented 5 years ago

Normally if you enable/disable the Localization module it should try to fetch and load the .PO file again. But you need to recompile the project in which this .PO file is since we bundle it inside our assemblies. So, right now there is no difference between .po files and .resx files for that matter. Something that could be done is to make use of the main web app App_Data/Localization folder since that the assets in that folder are not packaged inside it's assembly.

hishamco commented 5 years ago

I'm not sure why we should package the po files inside the assembly, because this will make them similar to resx which I hate!! because it needs to restart the app if something change.

We can do sort of loading them directly something similar to the configuration file sources

sebastienros commented 5 years ago

@skrypt I don't think the po files are part of any assemblies, there are just loaded from the file system.

To make this work

It has to be a feature as otherwise we would potentially watch a lot of files and it would be a perf issue. It's only if you are working on these files that you want this feature.

dev-masih commented 5 years ago

Normally OrchardCore library not embed .po files into any assembly, personally i have to move them manually as i publish the website in dot net core. so if there is a mechanism that reload translation without restarting webservice it would be great. like for example we could have implement a special get request for our website to update or reload all translations. i want to know, that is there a public method or a hack like way that we can achieve this?

Skrypt commented 5 years ago

Add a method in the po file localization manager service to flush the cached dictionaries

Skrypt commented 5 years ago

@sebastienros When you say that the .po files are not part of the assemblies I think it's maybe not accurate in all type of compilations. For example, if I'm publishing my project, I think they are bundled inside each module/theme dll since I don't see any Localization files in my Azure website folder except the one inside /App_Data/Localization which doesn't contain every localization strings I created for my entire solution.

So, in that scenario, the only way to refresh localization files on runtime is to recompile each assemblies containing those .po files. The only files that could be edited on runtime in that case would be the one contained in the App_Data folder contained in the published main app folder.

dev-masih commented 5 years ago

i tried publishing my asp.net core project with and without .po files and i can confirm that they are exactly the same so at least for core projects .po files are not shipped in any assemblies. and when i deploy website on server there aren't any localization until i moved .po files near main webservice dll. however i don't know how to embed po files inside dlls

hishamco commented 5 years ago

If the build action is not embedded they shouldn't bundle inside their assemblies

jtkech commented 5 years ago

For infos, mainly in OC.Module.props we can see the EmbeddedResource that are excluded by default when building a module. E.g by default the module /Assets.* files are excluded but not .po files under an eventual module /App_Data folder, unless explicitly overridden in the module project file.

MoazAlkharfan commented 3 years ago

This should allow the LocalizationManager to reload the translations as it clears all the cached ones.

https://gist.github.com/MoazAlkharfan/86ee6563c80727d215e9e64827add688

You can add it to the service collection as a hosted service.

sebastienros commented 3 years ago

Probably can be done with a FileWatcher instead of a bg task, in development mode. Like it's done for views.