Allow for configuration of additional languages to install and ensure one is set as default. (#13290)
* Allow for configuration of additional languages to install and ensure one is set as default. * Amended install default language method to handle invalid ISO codes and ensure the first specified language is always made the default. * Removed unnecessary using. * Apply suggestions from code review Co-authored-by: Ronald Barendse <ronald@barend.se> * Clean up. * Update src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCreator.cs Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Co-authored-by: Ronald Barendse <ronald@barend.se> Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NPoco;
|
||||
@@ -1745,13 +1747,62 @@ internal class DatabaseDataCreator
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateLanguageData() =>
|
||||
ConditionalInsert(
|
||||
Constants.Configuration.NamedOptions.InstallDefaultData.Languages,
|
||||
"en-us",
|
||||
new LanguageDto { Id = 1, IsoCode = "en-US", CultureName = "English (United States)", IsDefault = true },
|
||||
Constants.DatabaseSchema.Tables.Language,
|
||||
"id");
|
||||
private void CreateLanguageData()
|
||||
{
|
||||
// For languages we support the installation of records that are additional to the default installed data.
|
||||
// We can do this as they are specified by ISO code, which is enough to fully detail them.
|
||||
// All other customizable install data is specified by GUID, and hence we only know about the set that are installed by default.
|
||||
InstallDefaultDataSettings? languageInstallDefaultDataSettings = _installDefaultDataSettings.Get(Constants.Configuration.NamedOptions.InstallDefaultData.Languages);
|
||||
if (languageInstallDefaultDataSettings?.InstallData == InstallDefaultDataOption.Values)
|
||||
{
|
||||
// Insert the specified languages, ensuring the first is marked as default.
|
||||
bool isDefault = true;
|
||||
foreach (var isoCode in languageInstallDefaultDataSettings.Values)
|
||||
{
|
||||
if (!TryCreateCulture(isoCode, out CultureInfo? culture))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var dto = new LanguageDto
|
||||
{
|
||||
IsoCode = culture.Name,
|
||||
CultureName = culture.EnglishName,
|
||||
IsDefault = isDefault,
|
||||
};
|
||||
_database.Insert(Constants.DatabaseSchema.Tables.Language, "id", true, dto);
|
||||
isDefault = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Conditionally insert the default language.
|
||||
if (TryCreateCulture("en-US", out CultureInfo? culture))
|
||||
{
|
||||
ConditionalInsert(
|
||||
Constants.Configuration.NamedOptions.InstallDefaultData.Languages,
|
||||
culture.Name,
|
||||
new LanguageDto { Id = 1, IsoCode = culture.Name, CultureName = culture.EnglishName, IsDefault = true },
|
||||
Constants.DatabaseSchema.Tables.Language,
|
||||
"id");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryCreateCulture(string isoCode, [NotNullWhen(true)] out CultureInfo? culture)
|
||||
{
|
||||
try
|
||||
{
|
||||
culture = CultureInfo.GetCultureInfo(isoCode);
|
||||
return true;
|
||||
}
|
||||
catch (CultureNotFoundException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "CultureInfo could not be created because culture '{IsoCode}' is not available. The language will not be created.", isoCode);
|
||||
culture = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateContentChildTypeData()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user