Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add liquid filters for transliteration in ContentLocalization module. #136

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Liquid;
using OrchardCoreContrib.ContentLocalization.Liquid;

namespace OrchardCoreContrib.ContentLocalization.Extensions;

public static class LiquidFilterServicesExtensions
{
public static void AddLiquidFilters(this IServiceCollection services)
{
services.AddLiquidFilter<CyrToLatFilter>("cyr_to_lat");
hishamco marked this conversation as resolved.
Show resolved Hide resolved
services.AddLiquidFilter<ArabToLatFilter>("arab_to_lat");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using Fluid;
using Fluid.Values;
using OrchardCore.Liquid;
using OrchardCoreContrib.ContentLocalization.Services;
using OrchardCoreContrib.ContentLocalization.Transliteration;

namespace OrchardCoreContrib.ContentLocalization.Liquid;

public class ArabToLatFilter(ITransliterationService transliterationService) : ILiquidFilter
{
public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context)
{
var text = input.ToStringValue();
return new StringValue(transliterationService.Transliterate(TransliterateScript.Arabic, text));
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using Fluid;
using Fluid.Values;
using OrchardCore.Liquid;
using OrchardCoreContrib.ContentLocalization.Services;
using OrchardCoreContrib.ContentLocalization.Transliteration;

namespace OrchardCoreContrib.ContentLocalization.Liquid;

public class CyrToLatFilter(ITransliterationService transliterationService) : ILiquidFilter
{
public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context)
{
var text = input.ToStringValue();
return new StringValue(transliterationService.Transliterate(TransliterateScript.Cyrillic, text));
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}
}
8 changes: 8 additions & 0 deletions src/OrchardCoreContrib.ContentLocalization/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@
Dependencies = new[] { "OrchardCoreContrib.ContentLocalization" },
Category = "Internationalization"
)]

[assembly: Feature(
Id = "OrchardCoreContrib.ContentLocalization.TransliterationLiquid",
Name = "TransliterationLiquid",
Description = "Allows the use of existing transliteration services in liquid filters.",
Dependencies = new[] { "OrchardCoreContrib.ContentLocalization" },
Category = "Internationalization"
)]
hishamco marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 14 additions & 3 deletions src/OrchardCoreContrib.ContentLocalization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ This module has no dependencies.
| **Description** | Shows a matix for localized content per culture. |
| **Dependencies** | `OrchardCoreContrib.ContentLocalization`<br/> `OrchardCore.ContentLocalization` |

| | |
|------------------|-------------------------------------------------------------------------------------------------|
| **Name** | Transliteration (`OrchardCoreContrib.ContentLocalization.Transliteration`) |
| **Description** | Provides a type of conversion of a text from one script to another that involves swapping letters. |
| **Dependencies** | `OrchardCore.ContentLocalization` |

| | |
|------------------|----------------------------------------------------------------------------------------------------|
| **Name** | Transliteration (`OrchardCoreContrib.ContentLocalization.Transliteration`) |
| **Description** | Provides a type of conversion of a text from one script to another that involves swapping letters. |
| **Dependencies** | `OrchardCore.ContentLocalization` |
| **Name** | Transliteration Liquid (`OrchardCoreContrib.ContentLocalization.TransliterationLiquid`) |
| **Description** | Allows the use of existing transliteration services in liquid filters. |
| **Dependencies** | `OrchardCore.ContentLocalization`

## NuGet Packages

Expand Down Expand Up @@ -55,6 +61,11 @@ This module has no dependencies.
4. Enable the `Transliteration` feature.
5. Now you can inject the `ITransliterationService`, then use `Transliterate()` method to transliterate from one script to another.

### Transliteration Liquid
4. Enable the `TransliterationLiquid` feature.
5. Now you can use transliteration feature in liquid filters: `cyr_to_lat` and `arab_to_lat`.
6. This is useful for transliterating routes, example: `{{ ContentItem.DisplayText | cyr_to_lat | slugify }}`

## Video

[![Watch the video](https://img.youtube.com/vi/14X8fmmnOL8/maxresdefault.jpg)](https://youtu.be/14X8fmmnOL8)
Expand Down
12 changes: 12 additions & 0 deletions src/OrchardCoreContrib.ContentLocalization/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OrchardCoreContrib.ContentLocalization.Controllers;
using OrchardCoreContrib.ContentLocalization.Services;
using System;
using OrchardCoreContrib.ContentLocalization.Extensions;
hishamco marked this conversation as resolved.
Show resolved Hide resolved

namespace OrchardCoreContrib.ContentLocalization
{
Expand Down Expand Up @@ -52,4 +53,15 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<ITransliterationService, TransliterationService>();
}
}

[Feature("OrchardCoreContrib.ContentLocalization.TransliterationLiquid")]
public class TransliterationLiquidStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddTransliteration();
services.AddScoped<ITransliterationService, TransliterationService>();
services.AddLiquidFilters();
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading