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

RESX file reload at startup #34

Closed edgarssults closed 2 years ago

edgarssults commented 2 years ago

Does the library support reloading RESX file contents at runtime? As I understand ASP.NET Core does not support this since the resource files are compiled. I can have the resource files copied to the output folder, but changing them does not have any effect.

I know it's not the best use case, but this is what an ASP.NET MVC 5 app that I'm migrating to ASP.NET 6 does - customers have the ability to change the texts by changing the RESX files in App_GlobalResources.

Found the library here: https://stackoverflow.com/questions/66336205/update-resx-files-at-runtime-in-net-core

LazZiya commented 2 years ago

Reloading RESX files at runtime is not supported, as you already mentioned they are compliled files, and so the app must be restarted in order to re-generate the compiled RESX files.

But at least you don't need to deploy the app again, it will generate compiled resx resources once the app is restarted.

edgarssults commented 2 years ago

Ah, so an app restart should load the RESX changes? This would still be good, but it's not working for me.

I have the files set as Content and CopyToOutput, they end up in the Resources folder on publish. But if I make any changes to them they are ignored. Otherwise the translation works fine. So it feels like the resources are not recompiled when the app starts? Or the files themselves are not used during startup?

image

image

image

image

From project file:

<PublishSingleFile>false</PublishSingleFile>
<SelfContained>false</SelfContained>
<PublishReadyToRun>false</PublishReadyToRun>

Am I missing some kind of configuration or misunderstood how it works? I am hosting in IIS, but the behavior is the same when starting the published app executable.

LazZiya commented 2 years ago

so an app restart should load the RESX changes?

Yes, it will generate the compiled files with changes applied.

See Best practices and recommendations page, especially step no.9 to export resources to resx.

edgarssults commented 2 years ago

I now understand it's supposed to load the changes to the RESX files, bit it's just not happening for me. The results are not affected, I can even delete the published RESX files and translation still works.

Your ResxLocalizationSample doesn't reload them either, the resources are embedded into the binaries during publish. If I change the resources to be Content and to be copied, making changes to them doesn't affect anything.

Maybe there's a misunderstanding about what I'm trying to achieve?

LazZiya commented 2 years ago

The translation service do not update resx files, if you need to update the resources choose XML or DB approach, then export to resx.

The translation service works with all resources but it only updates XML and DB. If the translation service is used with resx it will show the translation but will not edit the resx, so the translation will be lost after restarting the app.

I can help more if you share your startup file.

edgarssults commented 2 years ago

No, I don't expect the RESX file to be updated by the app. I expected changes in the RESX file to be reflected in the app when it is restarted without rebuilding, which is not happening.

To be clear, these are the steps:

  1. Open the ResxLocalizationSample solution
  2. Change LocSource.tr.resx and LocSource.ar.resx properties to "Build Action: Content" and "Copy to Output Directory: Copy if newer"
  3. Publish the app to a folder
  4. Edit a string in either of the resx files in the published LocalizationResources folder, f.ex. change "Welcome" to "TEST"
  5. Start the app by launching ResxLocalizationSample.exe in the published folder
  6. Open the app URL
  7. Look at the text that was changed, it is still the same as during compilation instead of what it was changed to
edgarssults commented 2 years ago

I decided to write my own implementation of IStringLocalizer which reads the RESX files as XML on startup and caches the strings in a dictionary.