How can one seed the Language Management ? I would like to include new words that is not included default. This is what is generated:
using Volo.Abp.DependencyInjection;
using Volo.Abp.LanguageManagement.Data;
using Volo.Abp.Uow;
using Volo.Abp.MultiTenancy;
namespace GeoTicket.LanguageService.Data;
public class LanguageServiceDataSeeder : ITransientDependency
{
private readonly ILogger<LanguageServiceDataSeeder> _logger;
private readonly ICurrentTenant _currentTenant;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly LanguageManagementDataSeeder _languageManagementDataSeeder;
public LanguageServiceDataSeeder(
ILogger<LanguageServiceDataSeeder> logger,
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
LanguageManagementDataSeeder languageManagementDataSeeder)
{
_logger = logger;
_unitOfWorkManager = unitOfWorkManager;
_currentTenant = currentTenant;
_languageManagementDataSeeder = languageManagementDataSeeder;
}
public async Task SeedAsync(Guid? tenantId = null)
{
using (_currentTenant.Change(tenantId))
{
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
{
await SeedLanguagesAsync(tenantId);
await uow.CompleteAsync();
}
}
}
private async Task SeedLanguagesAsync(Guid? tenantId)
{
if (tenantId != null)
{
/* Language list is not multi-tenant */
return;
}
await _languageManagementDataSeeder.SeedAsync();
}
}
5 Answer(s)
-
0
Hi, you can inject the
ILanguageTextRepository
and use itsInsertAsync
method to insert new language texts:await LanguageTextRepository.InsertAsync( new LanguageText( GuidGenerator.Create(), resourceName, cultureName, name, value, CurrentTenant?.Id ) );
However, we don't suggest that, because the Language texts are designed to get the already defined localizations from the module localization resources and allow you to change the localization value dynamically, not creating additional values. (https://abp.io/docs/latest/modules/language-management)
Regards.
-
0
Thanks,
I can understand why you don't recommend to use Language Module for static content. However, we was first trying to follow the localization guide at https://abp.io/docs/latest/framework/fundamentals/localization but failed.
So perhaps you can guide us to set the localization instead of using the language module?
This is what we have done:
ProjectBlazorClientModule.cs: [DependsOn( typeof(AbpLocalizationModule), ... typeof(AbpVirtualFileSystemModule) //virtual file system )] public class ProjectBlazorClientModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>(); var builder = context.Services.GetSingletonInstance<WebAssemblyHostBuilder>() // Include the generated app-generate-proxy.json in the virtual file system Configure<AbpVirtualFileSystemOptions>(options => { options.FileSets.AddEmbedded<ProjectBlazorClientModule>(); }); ConfigureLocalization(); ... } private void ConfigureLocalization() { Configure<AbpVirtualFileSystemOptions>(options => { options.FileSets.AddEmbedded<ProjectBlazorClientModule>(); }); Configure<AbpLocalizationOptions>(options => { //Define a new localization resource (TestResource) options.Resources .Add<TestResource>("en") .AddVirtualJson("/Localization/Resources/Test"); options.DefaultResourceType = typeof(TestResource); }); } }
TestResource.cs using Volo.Abp.Localization; namespace GeoTicket.Blazor.Client.Localization.Resources; [LocalizationResourceName("Test")] public class TestResource;
File structure: Project.Blazor.Client/ ├── Project.Blazor.Client.csproj ├── ProjectBlazorClientModule.cs ├── ..... ├── ProjectComponentBase.cs ├── Localization │ ├── Resources │ │ └── Test │ │ ├── en.json │ │ └── sv.json │ └── TestResource.cs .....
en.json content (similar for sv.json): { "culture": "en", "texts": { "HelloWorld": "Hello World!" } }
@page "/" @using Project.Blazor.Client.Localization.Resources @using Microsoft.Extensions.Localization @inherits ProjectComponentBase @inject IStringLocalizer<TestResource> W @W["HelloWorld"]
The @W["HelloWorld"] is not working, just HelloWorld in the page above.
I think that we have followed the guide, but still doesn't get it. Any ideas?
-
0
Hi, it seems your configuration is correct. But there are some things that we should double-check:
1-) In your
*BlazorClient.csproj
file there should be a section as below:<ItemGroup> <EmbeddedResource Include="Localization\Resources\Test\*.json" /> <Content Remove="Localization\Resources\Test\*.json" /> </ItemGroup>
This will make all of your .json files under the localization directory as embedded-resources.
2-) Also, check that
Microsoft.Extensions.FileProviders.Embedded
is added in csproj file and there is<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
line in a property group:<ItemGroup> <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="9.0.0" /> </ItemGroup> <PropertyGroup> <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest> </PropertyGroup>
After these configurations, it should work as expected. Please let me know about the result. Regards.
Ref: https://abp.io/docs/latest/framework/infrastructure/virtual-file-system#embedding-the-files
-
0
Hi, I added your suggestions in the project file. However, the generate embedded files manifest setting
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
result with this in the Developer console in web browser:
[Error] Error: One or more errors occurred. (An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module Volo.Abp.AspNetCore.Components.WebAssembly.AbpAspNetCoreComponentsWebAssemblyModule, Volo.Abp.AspNetCore.Components.WebAssembly, Version=9.0.4.0, Culture=neutral, PublicKeyToken=null: The API description of the Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.IAbpApplicationConfigurationAppService.GetAsync method was not found!. See the inner exception for details.) (anonym funktion) (blazor.web.js:1:157835)
When setting to false / remove it, then it is the same result as before. Any other ideas?
Thanks
-
0
Hi, actually, you should have a shared library for your localization files and the related configuration. So, for example, if you move your localization configurations and the related
Localization/Resources/Test/*.json
to*.Application.Contracts
it should work.In summary, you should have a shared library to make these configurations (because normally, Blazor.Client is a Blazor WASM application at its core), and then you can inject the
IStringLocalizer<TestResource>
and use it in your blazor application:1-)
2-) Update the related
*.csproj
:<ItemGroup> <EmbeddedResource Include="Localization\Resources\Test\*.json" /> <Content Remove="Localization\Resources\Test\*.json" /> </ItemGroup>
<ItemGroup> <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="9.0.0" /> </ItemGroup> <PropertyGroup> <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest> </PropertyGroup>
3-) Then, use it in your page: