Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Custom Module Localization with new Languages Feature (db) #507


User avatar
0
byteplatz created

Hello,

How should I configure a given custom module for localization using the new db-stored language feature from v1.6 ?

Right now we are using this code for XML:

Configuration.Localization.Sources.Add(
                new DictionaryBasedLocalizationSource(
                    ModuleConsts.LocalizationSourceName,
                    new XmlEmbeddedFileLocalizationDictionaryProvider(
                        Assembly.GetExecutingAssembly(),
                        ModuleConsts.LocalizationSourceRootNameSpace
                        )
                    )
                );

8 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I will document this today or tomorrow.

    Enabling Db Localization is simple actually and you can see here: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/App_Start/AbpZeroTemplateWebModule.cs#L35">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L35</a>

    Also,

    We should seed Languages in EF migration: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.EntityFramework/Migrations/Seed/DefaultLanguagesCreator.cs">https://github.com/aspnetzero/aspnet-ze ... Creator.cs</a>

    And delete Configuration.Localization.Languages.Add... lines (<a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/6c58d5b751ea9bbf90258ca18813d55d7a200634/src/MyCompanyName.AbpZeroTemplate.Web/App_Start/AbpZeroTemplateWebModule.cs#L32">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L32</a>) since they are not used anymore.

    Do not delete Xml Localization Source registrations since db-localization is a wrapper and internally uses it.

  • User Avatar
    0
    byteplatz created

    I can see that WebModule has a new config line :

    Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
    

    Bottomline I just need that line at my custom module core Project module and remove this

    //Add/remove localization sources
                Configuration.Localization.Sources.Add(
                    new DictionaryBasedLocalizationSource(
                        CardStockConsts.LocalizationSourceName,
                        new XmlEmbeddedFileLocalizationDictionaryProvider(
                            Assembly.GetExecutingAssembly(),
                            CardStockConsts.LocalizationSourceRootNameSpace
                            )
                        )
                    );
    

    Is that enough or do I need to do something else ?

    Bruno

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Do not remove your "Configuration.Localization.Sources.Add(...)" code in the core module. You should also add database seed code as I did write in the response.

  • User Avatar
    0
    byteplatz created

    Ok thanks.

    Do I need to add this code to PreInitialize for each custom module ? Or only in App (since I am using a separated solution for each module)

    //Use database as language management
                Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
    

    Cheers

    Bruno

  • User Avatar
    0
    hikalkan created
    Support Team

    No, this code should run only once in an application. And it should run at the most top module (it's the web module for a web application).

  • User Avatar
    0
    byteplatz created

    Thank you very much

    I will code accordingly and get back to you

    Bruno

  • User Avatar
    0
    byteplatz created

    Thank you very much. It worked like a charm.

    I did the following:

    • Main App upgraded to 1.6 (clean install - localization db working)
    • Custom Module in separate solution containing default projects (core, app, web, entityframework)
    • Custom Module Web Core Project CoreModule with:
    public override void PreInitialize()
            {
                //Add/remove localization sources
                Configuration.Localization.Sources.Add(
                    new DictionaryBasedLocalizationSource(
                        MyModuleConsts.LocalizationSourceName,
                        new XmlEmbeddedFileLocalizationDictionaryProvider(
                            Assembly.GetExecutingAssembly(),
                            MyModuleConsts.LocalizationSourceRootNameSpace
                            )
                        )
                    );
            }
    

    And the localization is working out of the box...

    For another module (separated solution as well, I didnt have to change anything)

    Well done !!!

    Amazing job on these smooth upgrades!

    Regards

    Bruno Bertechini

  • User Avatar
    0
    hikalkan created
    Support Team

    You're welcome :)