EFTranslatable 1.0.4
dotnet add package EFTranslatable --version 1.0.4
NuGet\Install-Package EFTranslatable -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFTranslatable" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFTranslatable --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EFTranslatable, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EFTranslatable as a Cake Addin #addin nuget:?package=EFTranslatable&version=1.0.4 // Install EFTranslatable as a Cake Tool #tool nuget:?package=EFTranslatable&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
A simple way to make Entity Framework models translatable!
EFTranslatable is a light weight simple struct/type to allow your Entity/Model properties to be easily translated.
Installation
EFTranslatable is available on NuGet , GitHub
dotnet add package EFTranslatable
Basic usage
The following code demonstrates basic usage of EFTranslatable:-
Making a model translatable
The required steps to make a model translatable are:
- First, you need to allow
DbContext
to know aboutEFTranslatable
, in yourDataContext
file, inOnModelCreating
method add the following:-
using EFTranslatable;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.WithTranslatable(this);
}
public DbSet<Post> Posts { get; set; }
- Next, you should Drive from
HasTranslations<T>
whereT
is the currentModel
. - Next, Add
Translatable
to the property you want to be translatable. - Finally, you should make sure that all translatable properties are set to the
text
-datatype in your database. If your database supportsjson
-columns, use that.
using EFTranslatable;
public class Post : HasTranslations<Post>
{
public uint Id { get; set; }
public Translatable Title { get; set; } = new();
public Translatable Content { get; set; } = new();
}
Creating a model with Translatable
properties
There is two ways of making a Translatable
:
- By passing a
Dictionary
where is theKey
is the locale and theValue
is the transaltion. - Or by passing
Json
string directly .
public void Create()
{
_context.Posts.Add(new Post()
{
Title = new Translatable(new Dictionary<string, string>()
{
{"en","Good Title"},
{"ar", "عنون جيد"}
}),
Content = new Translatable("{\"en\": \"Good Content!\",\"ar\": \"شغال\"}"),
});
_context.SaveChanges();
}
Updating a model with Translatable
properties
public void Update()
{
var post = _context.Posts.First();
post.Title.Set("Different Title", "en").Set("New Locale", "fr");
// If locale is null , the Current `Thread` locale will be used
post.Content.Set("This is the current locale content");
_context.SaveChanges();
}
Quering/Filtering a model with Translatable
properties
using EFTranslatable.Extensions;
public void Equality()
{
// Will use the Current `Thread` locale
var post = _context.Posts.WhereLocalizedEquals(x => x.Title, "Good Title").SingleOrDefault();
//Will use the giving Locale
var post2 = _context.Posts.WhereLocalizedEquals(x => x.Title, "عنون جيد", "ar").SingleOrDefault();
}
public void Contains()
{
// Will use the whole `Json` string for checking
var post = _context.Posts.WhereLocalizedContains(x => x.Title, "Good Title").SingleOrDefault();
//Will use the giving Locale string value for checking
var post2 = _context.Posts.WhereLocalizedContains(x => x.Title, "عنون جيد", "ar").SingleOrDefault();
}
Retrieving/Returning a model with a specific translation
using EFTranslatable.Extensions;
[HttpGet]
public IActionResult Single()
{
var post = _context.Posts.WhereLocalizedEquals(x => x.Title, "Good Title").SingleOrDefault();
return Ok(post?.Translate("ar"));
}
[HttpGet]
public IActionResult Multiple()
{
var posts = _context.Posts.Select(x => x.Translate("ar")).ToList();
return Ok(posts);
}
License
The MIT License (MIT). Please see License File for more information.
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.EntityFrameworkCore (>= 6.0.1)
- Microsoft.EntityFrameworkCore.Relational (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
upgrade dependencies to 6.0