EFTranslatable 1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EFTranslatable --version 1.0.2
NuGet\Install-Package EFTranslatable -Version 1.0.2
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.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFTranslatable --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EFTranslatable, 1.0.2"
#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.2 // Install EFTranslatable as a Cake Tool #tool nuget:?package=EFTranslatable&version=1.0.2
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
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);
}
- 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; }
public Translatable Content { get; set; }
}
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.Drugs.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 | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. 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.
-
net5.0
- Microsoft.EntityFrameworkCore (>= 5.0.9)
- Microsoft.EntityFrameworkCore.Relational (>= 5.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.