Knowit.CodeFirstTranslations.Optimizely
1.0.2
See the version list below for details.
dotnet add package Knowit.CodeFirstTranslations.Optimizely --version 1.0.2
NuGet\Install-Package Knowit.CodeFirstTranslations.Optimizely -Version 1.0.2
<PackageReference Include="Knowit.CodeFirstTranslations.Optimizely" Version="1.0.2" />
<PackageVersion Include="Knowit.CodeFirstTranslations.Optimizely" Version="1.0.2" />
<PackageReference Include="Knowit.CodeFirstTranslations.Optimizely" />
paket add Knowit.CodeFirstTranslations.Optimizely --version 1.0.2
#r "nuget: Knowit.CodeFirstTranslations.Optimizely, 1.0.2"
#:package Knowit.CodeFirstTranslations.Optimizely@1.0.2
#addin nuget:?package=Knowit.CodeFirstTranslations.Optimizely&version=1.0.2
#tool nuget:?package=Knowit.CodeFirstTranslations.Optimizely&version=1.0.2
Knowit.CodeFirstTranslations.Optmizely
Helps to define code-first translations in Optimizely CMS based projects
Installation:
dotnet add package Knowit.CodeFirstTranslations.Optimizely
How to define translations:
Step 1:
Define static translation classes:
public static class LangLabels
{
public static ITranslator Translator { get; set; } = null!;
public static CultureInfo Culture => CultureInfo.GetCultureInfo("en"); // Not required property, default culture is "en"
private static string T(string key, string text) => Translator.Translate(key, text);
public static string Open => T("open", "Open");
public static string Close => T(nameof(Close), "Close");
public static string Hello => T(nameof(Hello), "Hello, World.");
}
public static class LangTexts // Multi-language example
{
public static IMultiLangTranslator Translator { get; set; } = null!;
public static List<CultureInfo> Cultures => new()
{
CultureInfo.GetCultureInfo("en"),
CultureInfo.GetCultureInfo("no")
};
private static string T(string key, string en, string no) => Translator.Translate(key, en, no);
public static string Open => T(nameof(Open),
"Open",
"Åpen");
public static string Close => T(nameof(Close),
"Close",
"Lukk");
public static string Hello => T(nameof(Hello),
"Hello, it is test page of translation classes for EPiServer.",
"Hei, det er testsiden med oversettelsesklasser for EPiServer.");
}
public static class LangEnums
{
public static ITranslator Translator { get; set; }
private static string T(string key, string text)
=> Translator.Translate(key, text);
public static string Layouts_Wide => T(nameof(Layouts_Wide), "Wide");
public static string Layouts_TheeColumns => T(nameof(Layouts_TheeColumns), "Sidebar to the right");
public static string ListItemsLayout_List => T(nameof(ListItemsLayout_List), "List");
public static string ListItemsLayout_Tiles => T(nameof(ListItemsLayout_Tiles), "Tiles");
public static string Gender_Male => T(nameof(Gender_Male), "Male");
public static string Gender_Female => T(nameof(Gender_Female), "Female");
}
Step 2:
Register localization provider and CodeFirstTranslations services:
public void ConfigureServices(IServiceCollection services)
{
if (_webHostingEnvironment.IsDevelopment())
{
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(_webHostingEnvironment.ContentRootPath, "App_Data"));
services.Configure<SchedulerOptions>(options => options.Enabled = false);
}
services
.AddCmsAspNetIdentity<ApplicationUser>()
.AddCms()
.AddAdminUserRegistration()
.AddEmbeddedLocalization<Startup>()
// CodeFirstTranslations
.AddLocalizationProvider<CodeFirstLocalizationProvider>()
.AddCodeFirstTranslations<OptimizelyTranslator, MultiLangOptimizelyTranslator>();
}
Then confiure CodeFirst translations and register translation classes defined in step 1:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
// CodeFirstTranslations
app.UseCodeFirstTranslations(translationsBuilder =>
{
translationsBuilder.RegisterTranslations(new[]
{
new TranslationsClassRegistration(typeof(LangLabels), "Labels"),
new TranslationsClassRegistration(typeof(LangTexts), "Texts"),
new TranslationsClassRegistration(typeof(LangEnums), "Special/Enums"),
});
});
app.UseEndpoints(endpoints =>
{
endpoints.MapContent();
});
}
How to use:
Basic usage:
Just call aprropriate property of tralations classes, since classes are static you don't need to inject them and call properties within classes or Razor views:
new Breadcrumbs
{
Links = breadcrumbsLinks,
Lang = new Breadcrumbs_Lang
{
AriaLabel = LangLabels.BreadcrumbsAreaLabel
}
};
...
var title = string.Format(LangFormats.SitePageTitle, pageTitle);
<span class="toggle__text">@LangLabels.Search</span>
Advanced usage:
It is posible to translate enum values using Display attribute
public enum ListItemsLayout
{
[Display(ResourceType = typeof(LangEnums), Name = "ListItemsLayout_List")]
List = 1,
[Display(ResourceType = typeof(LangEnums), Name = "ListItemsLayout_Tiles")]
Tiles
}
In order to get translated name of enum value please use GetDisplayName() extension method.
ListItemsLayout.List.GetDisplayName()
It is possible to use translations with data annotation attributes
View model:
public class ProfileViewModel
{
[Display(ResourceType = typeof(ProfileTranslations), Name = "FirstName")]
public string FirstName { get; set; } = null!;
[Display(ResourceType = typeof(ProfileTranslations), Name = "LastName")]
[Required(ErrorMessageResourceType = typeof(ProfileTranslations), ErrorMessageResourceName = "ErrorLastNameIsRequired")]
public string LastName { get; set; } = null!;
[Display(ResourceType = typeof(ProfileTranslations), Name = "Email")]
[Required(ErrorMessageResourceType = typeof(ProfileTranslations), ErrorMessageResourceName = "ErrorEmailIsRequired")]
public string Email { get; set; } = null!;
[Display(ResourceType = typeof(ProfileTranslations), Name = "Gender")]
[Required]
public Gender Gender { get; set; }
}
View:
@using (Html.BeginForm("SaveProfile", "HomePage"))
{
<div>
@Html.LabelFor(x => profile.FirstName)
@Html.EditorFor(x => profile.FirstName)
@Html.ValidationMessageFor(x => profile.FirstName)
</div>
<div>
@Html.LabelFor(x => profile.LastName)
@Html.EditorFor(x => profile.LastName)
@Html.ValidationMessageFor(x => profile.LastName)
</div>
<div>
@Html.LabelFor(x => profile.Email)
@Html.EditorFor(x => profile.Email)
@Html.ValidationMessageFor(x => profile.Email)
</div>
<div>
@Html.LabelFor(x => profile.Gender)
@Html.DropDownListFor(x => profile.Gender, Html.GetEnumSelectList<Gender>())
@Html.ValidationMessageFor(x => profile.Gender)
</div>
<button type="submit">@ProfileTranslations.SaveProfile</button>
}
Content property validation:
[ContentType(
GUID = "58fa84b7-73cd-4e4a-a3e3-07676d150f7c",
DisplayName = "Footnote Block",
GroupName = ContentGroupNames.Content)]
[ContentIconImageUrl("block-footnote.png")]
public class FootnoteBlock : BlockBase
{
[Display(Order = 10, Name = "Key")]
[Required]
[RegularExpression(
@"^[A-Za-z]+[\w\-\:\.]*$",
ErrorMessageResourceType = typeof(LangFormats),
ErrorMessageResourceName = "FootnoteKeyValidationMessage")]
public virtual string? Key { get; set; }
[CultureSpecific]
[Display(Order = 20, Name = "Footnotes")]
public virtual XhtmlString? Text { get; set; }
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- EPiServer.Framework (>= 12.0.3)
- Knowit.CodeFirstTranslations.Core (>= 1.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
First release