From 7221d131489b91bf6d298ed18ff48b118c0cdd69 Mon Sep 17 00:00:00 2001 From: Fanta Kim Date: Thu, 12 Sep 2024 00:45:08 +0900 Subject: [PATCH] Fix locale replacements --- SlugifyNet.Tests/SlugifyNetTests.cs | 12 ++++++++++++ SlugifyNet/SlugifyNet.cs | 23 ++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/SlugifyNet.Tests/SlugifyNetTests.cs b/SlugifyNet.Tests/SlugifyNetTests.cs index 1b9935e..8399b8e 100644 --- a/SlugifyNet.Tests/SlugifyNetTests.cs +++ b/SlugifyNet.Tests/SlugifyNetTests.cs @@ -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); + } } } diff --git a/SlugifyNet/SlugifyNet.cs b/SlugifyNet/SlugifyNet.cs index 710a4a9..f2a1336 100644 --- a/SlugifyNet/SlugifyNet.cs +++ b/SlugifyNet/SlugifyNet.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -7,7 +6,7 @@ namespace SlugifyNet { public static class SlugifyNet { - private static readonly Dictionary characters; + private static readonly Dictionary defaultCharacters; private static readonly Dictionary> locales; public static string GenerateSlug(this string input, string replacement = "-", string locale = "", bool lower = true, bool trim = true) @@ -15,12 +14,18 @@ public static string GenerateSlug(this string input, string replacement = "-", s if (string.IsNullOrEmpty(input)) return string.Empty; + // Create a new dictionary for this method call + var characters = new Dictionary(defaultCharacters); + if (!string.IsNullOrEmpty(locale)) { - // Replace characters based on locales. - if (locales.TryGetValue(locale, out Dictionary o)) + // Apply locale-specific replacements + if (locales.TryGetValue(locale, out Dictionary localeReplacements)) { - o.ToList().ForEach(x => characters[x.Key] = x.Value); + foreach (var kvp in localeReplacements) + { + characters[kvp.Key] = kvp.Value; + } } } @@ -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); @@ -47,7 +52,7 @@ 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; @@ -55,7 +60,7 @@ public static string GenerateSlug(this string input, string replacement = "-", s static SlugifyNet() { - characters = new Dictionary + defaultCharacters = new Dictionary { { "$", "dollar" }, { "%", "percent" },