Skip to content

Commit 44584c9

Browse files
committed
Enabled database based localization.
1 parent 6c1f57f commit 44584c9

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/AbpCompanyName.AbpProjectName.EntityFramework/AbpCompanyName.AbpProjectName.EntityFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
</Compile>
131131
<Compile Include="Migrations\Configuration.cs" />
132132
<Compile Include="AbpProjectNameDataModule.cs" />
133+
<Compile Include="Migrations\SeedData\DefaultLanguagesBuilder.cs" />
133134
<Compile Include="Migrations\SeedData\DefaultTenantRoleAndUserBuilder.cs" />
134135
<Compile Include="Migrations\SeedData\InitialDataBuilder.cs" />
135136
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Abp.Localization;
4+
using AbpCompanyName.AbpProjectName.EntityFramework;
5+
6+
namespace AbpCompanyName.AbpProjectName.Migrations.SeedData
7+
{
8+
public class DefaultLanguagesBuilder
9+
{
10+
private readonly AbpProjectNameDbContext _context;
11+
public static List<ApplicationLanguage> InitialLanguages { get; private set; }
12+
13+
static DefaultLanguagesBuilder()
14+
{
15+
InitialLanguages = new List<ApplicationLanguage>
16+
{
17+
new ApplicationLanguage(null, "en", "English", "famfamfam-flag-gb"),
18+
new ApplicationLanguage(null, "tr", "Türkçe", "famfamfam-flag-tr"),
19+
new ApplicationLanguage(null, "zh-CN", "简体中文", "famfamfam-flag-cn")
20+
};
21+
}
22+
23+
public DefaultLanguagesBuilder(AbpProjectNameDbContext context)
24+
{
25+
_context = context;
26+
}
27+
28+
public void Build()
29+
{
30+
CreateLanguages();
31+
}
32+
33+
private void CreateLanguages()
34+
{
35+
foreach (var language in InitialLanguages)
36+
{
37+
AddLanguageIfNotExists(language);
38+
}
39+
}
40+
41+
private void AddLanguageIfNotExists(ApplicationLanguage language)
42+
{
43+
if (_context.Languages.Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
44+
{
45+
return;
46+
}
47+
48+
_context.Languages.Add(language);
49+
50+
_context.SaveChanges();
51+
}
52+
}
53+
}

src/AbpCompanyName.AbpProjectName.EntityFramework/Migrations/SeedData/InitialDataBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public InitialDataBuilder(AbpProjectNameDbContext context)
1414
public void Build()
1515
{
1616
new DefaultTenantRoleAndUserBuilder(_context).Build();
17+
new DefaultLanguagesBuilder(_context).Build();
1718
}
1819
}
1920
}

src/AbpCompanyName.AbpProjectName.WebSpaAngular/App_Start/AbpProjectNameWebModule.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using System.Reflection;
2-
using System.Web;
3-
using System.Web.Http;
42
using System.Web.Mvc;
53
using System.Web.Optimization;
64
using System.Web.Routing;
7-
using Abp.Localization;
8-
using Abp.Localization.Sources;
9-
using Abp.Localization.Sources.Xml;
105
using Abp.Modules;
6+
using Abp.Zero.Configuration;
117
using AbpCompanyName.AbpProjectName.Api;
128

139
namespace AbpCompanyName.AbpProjectName.WebSpaAngular
@@ -17,10 +13,8 @@ public class AbpProjectNameWebModule : AbpModule
1713
{
1814
public override void PreInitialize()
1915
{
20-
//Add/remove languages for your application
21-
Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-england", true));
22-
Configuration.Localization.Languages.Add(new LanguageInfo("tr", "Türkçe", "famfamfam-flag-tr"));
23-
Configuration.Localization.Languages.Add(new LanguageInfo("zh-CN", "简体中文", "famfamfam-flag-cn"));
16+
//Enable database based localization
17+
Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
2418

2519
//Configure navigation/menu
2620
Configuration.Navigation.Providers.Add<AbpProjectNameNavigationProvider>();

0 commit comments

Comments
 (0)