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

need help extending default localization resources #6731


User avatar
0
alexanderpilhar created

6.8.0, Angular, .NET Framework

Is it possible to extend default localization resources like described here?: Extending Localization Sources

I'm asking because I get the following error when I try to do so:

FATAL 2019-03-29 13:39:47,595 [1    ] Abp.AbpBootstrapper                      - Abp.AbpInitializationException: ProjectName source contains more than one dictionary for the culture: en
   bei Abp.Localization.Dictionaries.Xml.XmlEmbeddedFileLocalizationDictionaryProvider.Initialize(String sourceName)
   bei Abp.Localization.MultiTenantLocalizationSource.Initialize(ILocalizationConfiguration configuration, IIocResolver iocResolver)
   bei Abp.Localization.LocalizationManager.InitializeSources()
   bei Abp.AbpKernelModule.PostInitialize()
   bei System.Collections.Generic.List`1.ForEach(Action`1 action)
   bei Abp.AbpBootstrapper.Initialize()
Abp.AbpInitializationException: ProjectName source contains more than one dictionary for the culture: en
   bei Abp.Localization.Dictionaries.Xml.XmlEmbeddedFileLocalizationDictionaryProvider.Initialize(String sourceName)
   bei Abp.Localization.MultiTenantLocalizationSource.Initialize(ILocalizationConfiguration configuration, IIocResolver iocResolver)
   bei Abp.Localization.LocalizationManager.InitializeSources()
   bei Abp.AbpKernelModule.PostInitialize()
   bei System.Collections.Generic.List`1.ForEach(Action`1 action)
   bei Abp.AbpBootstrapper.Initialize()
FATAL 2019-03-29 13:39:47,700 [1    ] soft.AspNetCore.Hosting.Internal.WebHost - Application startup exception
Abp.AbpInitializationException: ProjectName source contains more than one dictionary for the culture: en
   bei Abp.Localization.Dictionaries.Xml.XmlEmbeddedFileLocalizationDictionaryProvider.Initialize(String sourceName)
   bei Abp.Localization.MultiTenantLocalizationSource.Initialize(ILocalizationConfiguration configuration, IIocResolver iocResolver)
   bei Abp.Localization.LocalizationManager.InitializeSources()
   bei Abp.AbpKernelModule.PostInitialize()
   bei System.Collections.Generic.List`1.ForEach(Action`1 action)
   bei Abp.AbpBootstrapper.Initialize()
   bei Abp.AspNetCore.AbpApplicationBuilderExtensions.InitializeAbp(IApplicationBuilder app)
   bei Abp.AspNetCore.AbpApplicationBuilderExtensions.UseAbp(IApplicationBuilder app, Action`1 optionsAction)
   bei CompanyName.ProjectName.Web.Startup.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

My folder structure for localization resources looks like this:

  • Localization
    • ProjectName
      • ProjectName.xml
      • ProjectName-xx.xml
      • ...
    • ProjectNameExtended.xml
      • ProjectName.xml
      • ProjectName-xx.xml
      • ...

ProjectNameLocalizationConfigurer.cs looks like this:

public static void Configure(ILocalizationConfiguration localizationConfiguration)
{
    localizationConfiguration.Sources.Add(
        new DictionaryBasedLocalizationSource(
            ProjectNameConsts.LocalizationSourceName,
            new XmlEmbeddedFileLocalizationDictionaryProvider(
                typeof(ProjectNameLocalizationConfigurer).GetAssembly(),
                "CompanyName.ProjectName.Localization.ProjectName"
            )
        )
    );

    localizationConfiguration.Sources.Extensions.Add(
        new LocalizationSourceExtensionInfo(
            "(ProjectName",
            new XmlEmbeddedFileLocalizationDictionaryProvider(
                typeof(ProjectNameLocalizationConfigurer).GetAssembly(),
                "CompanyName.ProjectName.Localization.ProjectNameExtended"
            )
        )
    );
}

Please, advise!

EDIT: Removed project name from code-block


27 Answer(s)
  • User Avatar
    1
    ryancyq created
    Support Team

    yes, localization extension only works for localization files with the same name.

  • User Avatar
    0
    michael.pear created

    This was an extremely confusing post to address the issue of extending a localization source, which allows overriding existing entries and adding new ones. I wanted to organize things so that what I add for my application is separate from the AspNetZero supplied language files. To summarize the solution, you need to do the following assuming a project "YourProject" is the project that is provided from AspNetZero:

    1. In folder YourProject.Core/Localization, you find a YourProject folder with language files prefixed with "YourProject" (e.g. YourProject.xml, YourProject-de.xml, etc.
    2. You need to add a folder under YourProject.Core/Localization with a different name like "ExtendYourProject".
    3. In the new Folder, for any languages you want to extend, add a file with the SAME name as the language file you are extending (e.g., YourProject-de.xml). The file will contain any entries you want to override and any new entries.
    4. In the language configurer in YourProject.Core/Localization file YourProjectLocalizationConfigurer add the code:
                localizationConfiguration.Sources.Extensions.Add(
                    new LocalizationSourceExtensionInfo("YourProject",
                        new XmlEmbeddedFileLocalizationDictionaryProvider(
                            typeof(YourProjectLocalizationConfigurer).GetAssembly(),
                            "YourProject.Localization.ExtendYourProject"
                        )
                     )
                );
               
    

    Rebuild, and you should find that the extensions are now used. You do need to make sure that the "build action" for any files you add are showing "Embedded Resource". Any XML files that I added seem to be given that automatically in visual studio.

    Hope that helps makes sense of a rather long involved post for something relatively simple in the end.