aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Core/Localization/TextLocalizer.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2016-11-29 14:47:45 -0500
committerGitHub <noreply@github.com>2016-11-29 14:47:45 -0500
commit8c2095b24ee3228c0e112ebc3cfcb73e146d2a78 (patch)
tree6a83c8132f9eac18c0a0bc2461e191c5c725c3c1 /Emby.Server.Core/Localization/TextLocalizer.cs
parent1d09262963fb1d09f3a04843922a6a7a2f4364b3 (diff)
parent456d5e5b760abe46c8dedf08b2465d2bd3ced4af (diff)
Merge pull request #2317 from MediaBrowser/dev
Dev
Diffstat (limited to 'Emby.Server.Core/Localization/TextLocalizer.cs')
-rw-r--r--Emby.Server.Core/Localization/TextLocalizer.cs41
1 files changed, 36 insertions, 5 deletions
diff --git a/Emby.Server.Core/Localization/TextLocalizer.cs b/Emby.Server.Core/Localization/TextLocalizer.cs
index 016d59659..6690c6263 100644
--- a/Emby.Server.Core/Localization/TextLocalizer.cs
+++ b/Emby.Server.Core/Localization/TextLocalizer.cs
@@ -2,6 +2,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
+using System.Text.RegularExpressions;
using Emby.Server.Implementations.Localization;
namespace Emby.Server.Core.Localization
@@ -10,11 +11,41 @@ namespace Emby.Server.Core.Localization
{
public string RemoveDiacritics(string text)
{
- return String.Concat(
- text.Normalize(NormalizationForm.FormD)
- .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
- UnicodeCategory.NonSpacingMark)
- ).Normalize(NormalizationForm.FormC);
+ if (text == null)
+ {
+ throw new ArgumentNullException("text");
+ }
+
+ var chars = Normalize(text, NormalizationForm.FormD)
+ .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark);
+
+ return Normalize(String.Concat(chars), NormalizationForm.FormC);
+ }
+
+ private static string Normalize(string text, NormalizationForm form, bool stripStringOnFailure = true)
+ {
+ if (stripStringOnFailure)
+ {
+ try
+ {
+ return text.Normalize(form);
+ }
+ catch (ArgumentException)
+ {
+ // will throw if input contains invalid unicode chars
+ // https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
+ text = StripInvalidUnicodeCharacters(text);
+ return Normalize(text, form, false);
+ }
+ }
+
+ return text.Normalize(form);
+ }
+
+ private static string StripInvalidUnicodeCharacters(string str)
+ {
+ var invalidCharactersRegex = new Regex("([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])");
+ return invalidCharactersRegex.Replace(str, "");
}
public string NormalizeFormKD(string text)