Update/Reset all Plugin's String Resources Without Forcing Uninstall / Install

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Problem Summary:
Sometimes we need to update or reset string resources for a plugin. This can happen if there is an update to an existing plugin that is already installed, or changes have been made to the string resources, and the store owner needs to reset the original text that came with the plugin.

Current Options:
The current plugin architecture allows 3 ways that a plugin's resource strings can updated:

Option 1: Uninstall and then install the plugin.
This option is not ideal.  We do not want to uninstall an active plugin, there may be loss of data in the database, and many have other unintended consequences. If all we need is to update our string resources this is a major overkill.

Option 2: Store owner manually finds each string resource in admin and makes the update.
This is also not ideal. These changes have to be done one at a time, hopefully correctly. Each store owner needs to be given a list of what the string resources are, instructions on how to make the update, etc.  This is time consuming, introduces risk of human error, and may not be feasible in all cases.

Option 3: Execute an SQL Script directly on the database
This avoids the front end completely and makes the updates directly on the database.  This is more of a workaround to avoid options 1 and 2, and only applies to those that have direct database access and are comfortable making such changes. In most cases, this is not a good approach and typical store owner would not be making this kind of an update.

Suggestions for Improvement:
What I have been doing in our plugins was to move all of the "AddOrUpdatePluginLocaleResource" code and the "DeletePluginLocaleResource" code from the install and uninstall functions of the plugin to static extension functions in a static extension class, as in this example:
public static void AddOrUpdateResources(this BasePlugin plugin)
{
    plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.ResourceName1", "Resource Value 1");
    plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.ResourceName2", "Resource Value 2");
}

public static void DeleteResources(this BasePlugin plugin)
{
    plugin.DeletePluginLocaleResource("Plugin.Misc.ResourceName1");
    plugin.DeletePluginLocaleResource("Plugin.Misc.ResourceName2");
}

This now allows me to call this code anywhere in the project.
In the plugin's install function I now have:
this.AddOrUpdateResources();

In the plugin's uninstall function I now have:
this.DeleteResources();

Both install and uninstall functions are now also smaller, cleaner and more readable.

I can now easily update all the resources from any controller in the plugin, below is an example:
[Area(AreaNames.Admin)]
public IActionResult ResetStringResources(ConfigurationModel model)
{
    try
    {
        var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName("Misc.MyPlugin");
        var plugin = pluginDescriptor.Instance() as MyPlugin;
        plugin.AddOrUpdateResources();
        SuccessNotification("Plugin default resource strings were successfully reset");
    }
    catch (Exception exc)
    {
        _logger.Error(exc.Message, exc);
        ErrorNotification("Failed to reset default strings. Error: " + exc.Message);
    }

    //redisplay the form
    return Configure()
}

While I can add this workaround in every plugin we have, what would be very good is to have NopCommerce solution include some architecture to allow the update/reset of string resources for everyone making plugins easier.  This is especially useful for developers making plugins with many string resources or with frequent updates to their plugins. It just makes the whole architecture more powerful and appealing for everyone.
5 years ago
Thanks for your suggestion. Here is a work item for it.
3 years ago
Starting from nopCommerce version 4.30 we may use migrations mechanism to do this, see the short example below:

[NopMigration("2020/07/06 09:27:23:6455432", "Update/reset string resources without forcing uninstall/install")]
public class UpdateResetStringResources : MigrationBase
{
  private readonly ILocalizationService _localizationService;

  public UpdateResetStringResources(ILocalizationService localizationService)
  {
    _localizationService = localizationService;
  }

  public override void Up()
  {
    _localizationService.AddOrUpdatePluginLocaleResource("resourceName", "resourceValue");
  }

  public override void Down()
  {
    _localizationService.DeletePluginLocaleResource("resourceName");
    //or return back an old value
  }
}


Sizzler wrote:
Problem Summary:
Sometimes we need to update or reset string resources for a plugin. This can happen if there is an update to an existing plugin that is already installed, or changes have been made to the string resources, and the store owner needs to reset the original text that came with the plugin.
3 years ago
It would be helpful if there was more information on how the "migrations mechanism" works in general.
3 years ago
Please read this article in our official documentation

New York wrote:
It would be helpful if there was more information on how the "migrations mechanism" works in general.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.