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                
#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                

A simple way to make Entity Framework models translatable!

Nuget Nuget

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 about EFTranslatable, in your DataContext file, in OnModelCreating 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> where T is the current Model.
  • 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 supports json-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 :

  1. By passing a Dictionary where is the Key is the locale and the Value is the transaltion.
  2. 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 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 510 1/29/2022
1.0.3 324 8/19/2021
1.0.2 314 8/19/2021
1.0.1 335 8/18/2021
1.0.0 342 8/18/2021

upgrade dependencies to 6.0