Skip to content

Commit

Permalink
Fix locale replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
fantakim committed Sep 11, 2024
1 parent b0f8dfc commit 7221d13
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 12 additions & 0 deletions SlugifyNet.Tests/SlugifyNetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,17 @@ public void slugify_multilingual_examples(string input, string expected)

Assert.Equal(expected, actual);
}

[Theory]
[InlineData("de", "Größenmaßstäbe", "groessenmassstaebe")]
[InlineData("es", "¿Cómo estás?", "como-estas")]
[InlineData("fr", "L'été de l'amour", "l'ete-de-l'amour")]
[InlineData("vi", "Xin chào thế giới", "xin-chao-the-gioi")]
public void slugify_locale_examples(string locale, string input, string expected)
{
var actual = SlugifyNet.GenerateSlug(input, locale: locale);

Assert.Equal(expected, actual);
}
}
}
23 changes: 14 additions & 9 deletions SlugifyNet/SlugifyNet.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SlugifyNet
{
public static class SlugifyNet
{
private static readonly Dictionary<string, string> characters;
private static readonly Dictionary<string, string> defaultCharacters;
private static readonly Dictionary<string, Dictionary<string, string>> locales;

public static string GenerateSlug(this string input, string replacement = "-", string locale = "", bool lower = true, bool trim = true)
{
if (string.IsNullOrEmpty(input))
return string.Empty;

// Create a new dictionary for this method call
var characters = new Dictionary<string, string>(defaultCharacters);

if (!string.IsNullOrEmpty(locale))
{
// Replace characters based on locales.
if (locales.TryGetValue(locale, out Dictionary<string, string> o))
// Apply locale-specific replacements
if (locales.TryGetValue(locale, out Dictionary<string, string> localeReplacements))
{
o.ToList().ForEach(x => characters[x.Key] = x.Value);
foreach (var kvp in localeReplacements)
{
characters[kvp.Key] = kvp.Value;
}
}
}

Expand All @@ -30,10 +35,10 @@ public static string GenerateSlug(this string input, string replacement = "-", s
var s = c.ToString();
s = (s == replacement) ? " " : s;

// Replace string based on characters.
// Replace string based on characters
s = characters.TryGetValue(s, out string o) ? o : s;

// Remove not allowed characters.
// Remove not allowed characters
s = Regex.Replace(s, @"[^\w\s$*_+~.()'""!\-:@]+", string.Empty);

sb.Append(s);
Expand All @@ -47,15 +52,15 @@ public static string GenerateSlug(this string input, string replacement = "-", s
if (lower)
slug = slug.ToString().ToLower();

// Replace spaces with replacement character, treating multiple consecutive spaces as a single space.
// Replace spaces with replacement character, treating multiple consecutive spaces as a single space
slug = Regex.Replace(slug, @"\s+", replacement);

return slug;
}

static SlugifyNet()
{
characters = new Dictionary<string, string>
defaultCharacters = new Dictionary<string, string>
{
{ "$", "dollar" },
{ "%", "percent" },
Expand Down

0 comments on commit 7221d13

Please sign in to comment.