OrchardCore icon indicating copy to clipboard operation
OrchardCore copied to clipboard

Localization reload translations if file changes

Open Franklin89 opened this issue 8 years ago • 22 comments

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);

Franklin89 avatar Oct 12 '17 15:10 Franklin89

That would be a nice improvement.

sebastienros avatar Oct 12 '17 18:10 sebastienros

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

Franklin89 avatar Oct 12 '17 19:10 Franklin89

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

sebastienros avatar Oct 12 '17 19:10 sebastienros

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

hishamco avatar Oct 26 '17 01:10 hishamco

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

nicojmb avatar Jan 31 '19 23:01 nicojmb

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

hishamco avatar Feb 01 '19 11:02 hishamco

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.

sebastienros avatar Feb 07 '19 21:02 sebastienros

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

hishamco avatar Feb 08 '19 14:02 hishamco

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

dev-masih avatar Mar 27 '19 20:03 dev-masih

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

dev-masih avatar Mar 28 '19 06:03 dev-masih

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.

Skrypt avatar Mar 28 '19 08:03 Skrypt

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

hishamco avatar Mar 28 '19 14:03 hishamco

@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

  • Add a method in the po file localization manager service to flush the cached dictionaries
  • Add an option to watch the files that it loaded
  • Add a feature that will configure this option

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.

sebastienros avatar Mar 28 '19 19:03 sebastienros

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?

dev-masih avatar Mar 29 '19 13:03 dev-masih

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

Skrypt avatar Mar 29 '19 18:03 Skrypt

@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.

Skrypt avatar Mar 30 '19 07:03 Skrypt

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

dev-masih avatar Mar 30 '19 09:03 dev-masih

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

hishamco avatar Mar 30 '19 09:03 hishamco

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.

jtkech avatar Mar 30 '19 13:03 jtkech

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.

MoazAlkharfan avatar May 26 '21 09:05 MoazAlkharfan

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

sebastienros avatar May 27 '21 17:05 sebastienros

IMHO, this is not possible nowadays because once you change any PO file, once you compile the project, it generates the default PO files

We might need to disable this before we dig into this feature

hishamco avatar Jul 01 '25 00:07 hishamco