Open Closed

How to create custom localization provider? #8434


User avatar
0
dshapiro created

I'm reviewing the documentation for Localization and I'm not finding any information on how to create a custom localization provider. I thought I had seen this before, but can't find it now.

We're looking to continue using the existing out of box localization functionality, but we'd like to augment this to load resources from a custom table within our database. The intent is to be able to continue to use IStringLocalizer (L["<KEY>"]) on the server and abpLocalization pipe in angular to reference both out-of-the-box json-based resources and resources stored in our custom database table.

  • ABP Framework version: v8.3.0
  • UI Type: Angular
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Exception message and full stack trace: N/A
  • Steps to reproduce the issue: N/A

9 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can check the source code of the Language Management module.

    The DynamicLocalizationResourceContributor and DynamicResourceLocalizer services

  • User Avatar
    0
    dshapiro created

    Hi maliming,

    The Language Management module is a pro module which we don't have access to the source code for; the best I can do is use a decompiler to view an obfuscated version of the code and try to understand it.

    Reviewing the decompiled code, it looks as if these classes are dependent on a LocalizationResourceBase being passed in, but I'm not sure if this is appropriate to my use case. We have a structure whereas our lookup tables (ex: Company Type, Lead Source, Tax Class, etc) store localization values in the database (see a demo ERD); in our example, each CompanyType record has one corresponding TextResource record with multiple TextResourceTranslations (one per supported culture). This is one example from a large list of lookup tables that we support.

    In our ABP application we have a series of pages for each lookup that supports the creation of the lookup entity, text resource, and text resource translations. It's now our desire to be able to use the built-in localization engine to pull the correct translation from our database table utilizing the group & key values of the Text Resource table. Continuing our example of Company Type, this would be something like {{ 'CompanyTypes:Software' | abpLocalization }}.

    I created a sample implementation of DynamicLocalizationResourceContributor and DynamicResourceLocalizer and walked through how it's called and it seems to only be called for classes in the code that have been marked as a resource file.

    Can you provide any other guidance on implementing this requirement?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please send an email to liming.ma@volosoft.com

    I will share the source code of DynamicLocalizationResourceContributor and DynamicResourceLocalizer : )

  • User Avatar
    0
    dshapiro created

    Hi, I've looked at the code you've sent me, but I'm still not sure how this should be used for our use case. These classes you've shared expect a LocalizationResourceBase to be passed in via constructor/parameters and it's not clear to me what generates the LocalizationResourceBase and how they get passed in. My suspicion is that these are generated based on code (possibly classes decorated with LocalizationResourceNameAttribute?) and it's not clear how to tie this class that's dependent on something baked into the assembly to instead look for a dynamic list of texts from the database. Do we need to create an empty resource class for each of our lookups and have the custom contributor go to the database for these?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The DynamicLocalizationResourceContributor and DynamicResourceLocalizer will first read the value from the database. So you can change the localization texts in the database.

    The DynamicResourceLocalizer will use ILanguageTextRepository inside to query the texts.

    This is how it works.

    https://abp.io/docs/latest/modules/language-management#language-texts

    All your code should be compatible with the current design: https://abp.io/docs/latest/framework/fundamentals/localization

  • User Avatar
    0
    dshapiro created

    Ok, I think I understand. Our custom contributor and localizer will be responsible for caching the texts from the database and then we can do one of a few options: 1. We can create a custom resource class per lookup:

    public class CompanyTypeResources {
    
    }
    
    ...
    
    Configure<AbpLocalizationOptions>(options => options.Resources.Add&lt;CompanyTypeResources>());
    

    2. We can create a single resource class instead (ex: LookupResources) and have our localizer cache all text for this resource 3. Or we can just use the existing MyProjectResources that's created as part of the default template Does this sound right? I'd like to avoid creating a resource class per lookup, so I'm thinking option 2 or 3 would suit best. Any recommendations on best practices?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can consider to create one or some Resource and using Inherit and Extending

    A localization resource(CompanyTypeResources) is used to group related localization strings together and separate them from other localization strings of the application

    Inherit From Other Resources A resource can inherit from other resources which makes possible to re-use existing localization strings without referring the existing resource.

    https://abp.io/docs/latest/framework/fundamentals/localization#inherit-from-other-resources

    Extending Existing Resource Inheriting from a resource creates a new resource without modifying the existing one. In some cases, you may want to not create a new resource but directly extend an existing resource.

    https://abp.io/docs/latest/framework/fundamentals/localization#extending-existing-resource

  • User Avatar
    0
    dshapiro created

    Thanks, we've got this working now!

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Great

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09