Need primer on Plugin fields and Locales

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
So I've built a plugin by copying and modifying another plugin.   Things are going smooth but I'm having an issue and I just don't have a grip on exactly how local strings are mapped to plugin fields and it's making it hard to debug.

First I changed the existing plugin to my field name.  (This worked)

Model.cs
       [NopResourceDisplayName("RG.Plugin.MyPlugin.nopDefaultProductId")]
        public int nopDefaultProductId { get; set; }

Plugin.cs
            _localizationService.AddOrUpdatePluginLocaleResource("RG.Plugin.MyPlugin.nopDefaultProductId", "Default nop Product Id");
            _localizationService.AddOrUpdatePluginLocaleResource("RG.Plugin.MyPlugin.nopDefaultProductId.Hint", "The nopCommerce ProductId to use");

Configure.cshtml
                    <div class="col-md-3">
                        <nop-label asp-for="nopDefaultProductId" />
                    </div>

As I said, this worked fine.  But then I went back to add another field -  Select list based on an enum (using Payments.Square as my guide)

Model.cs
        [NopResourceDisplayName("RG.Plugin.MyPlugin.Country")]
        public int CountryId { get; set; }

Plugin.cs
            _localizationService.AddOrUpdatePluginLocaleResource("RG.Plugin.MyPlugin.Country", "Country");
            _localizationService.AddOrUpdatePluginLocaleResource("RG.Plugin.MyPlugin.Fields.Country.Hint", "Choose your country");

Configure.cshtml
                    <div class="col-md-3">
                        <nop-label asp-for="CountryId" />
                    </div>

This does not work.   The select list works fine - but for the label, the configure page shows "RG.Plugin.MyPlugin.Fields.Country" (instead of the prompt and hint).  

When I look in Languages configuration - I see that the string has been added to the database with the name NOT INCLUDING the word "Fields" (RG.Plugin.MyPlugin.Country).   But clearly the view is looking for the word "Fields" in the name.  

In fact, if I manually change the language string name and include "Fields" - the configure page pulls over the correct prompt and hint text.

So my question is - where is "Fields" coming from?  

Or, more generally, my question is - is there a document somewhere that describes the correlation between Model/View and database with regard to using localized strings on a plugin configure page? (e.g. how to correctly add them, delete them and use them)  

This would help me understand so that whenever this happens I'll know where to start looking to track down the problem.
4 years ago
Hello,

To be honest I haven't experienced this behaviour but I think I know what is happening here.

Looking at your code:
            _localizationService.AddOrUpdatePluginLocaleResource("RG.Plugin.MyPlugin.Country", "Country");
            _localizationService.AddOrUpdatePluginLocaleResource("RG.Plugin.MyPlugin.Fields.Country.Hint", "Choose your country");


Those two strings should be the same up until the ".Hint" part. This is because when nopCommerce tries to display the label and hint it builds the hint resource name from the label resource name (just ads + .hint to the resource name). Without looking at the code I will guess that when those 2 resource keys are not the same up until the .Hint part of the key, will lead to an issue like this.

Just change the both of the resource keys to be:
RG.Plugin.MyPlugin.Country
RG.Plugin.MyPlugin.Country.Hint


and you shouldn't have an issue.

Hope that helps!

Regrads,
Anton
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.