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

Localisation of Tupleize method & Add Turkish support #1094

Merged
merged 3 commits into from Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -657,6 +657,7 @@ namespace Humanizer
{
public static string ToOrdinalWords(this int number, System.Globalization.CultureInfo culture = null) { }
public static string ToOrdinalWords(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { }
public static string ToTuple(this int number, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this int number, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this int number, bool addAnd, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { }
Expand Down Expand Up @@ -1921,6 +1922,7 @@ namespace Humanizer.Localisation.NumberToWords
string Convert(long number, Humanizer.GrammaticalGender gender, bool addAnd = True);
string ConvertToOrdinal(int number);
string ConvertToOrdinal(int number, Humanizer.GrammaticalGender gender);
string ConvertToTuple(int number);
}
}
namespace Humanizer.Localisation.Ordinalizers
Expand Down
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

namespace Humanizer.Localisation.NumberToWords
Expand Down Expand Up @@ -166,5 +166,38 @@ private static bool ExceptionNumbersToWords(long number, out string words)
{
return OrdinalExceptions.TryGetValue(number, out words);
}

public override string ConvertToTuple(int number)
{
switch (number)
{
case 1:
return "single";
case 2:
return "double";
case 3:
return "triple";
case 4:
return "quadruple";
case 5:
return "quintuple";
case 6:
return "sextuple";
case 7:
return "septuple";
case 8:
return "octuple";
case 9:
return "nonuple";
case 10:
return "decuple";
case 100:
return "centuple";
case 1000:
return "milluple";
default:
return $"{number}-tuple";
}
}
}
}
Expand Up @@ -57,5 +57,15 @@ public string ConvertToOrdinal(int number)
/// <param name="gender"></param>
/// <returns></returns>
public abstract string ConvertToOrdinal(int number, GrammaticalGender gender);

/// <summary>
/// Converts integer to named tuple (e.g. 'single', 'double' etc.).
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public virtual string ConvertToTuple(int number)
{
return Convert(number);
}
}
}
Expand Up @@ -50,5 +50,15 @@ public string ConvertToOrdinal(int number, GrammaticalGender gender)
{
return ConvertToOrdinal(number);
}

/// <summary>
/// Converts integer to named tuple (e.g. 'single', 'double' etc.).
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public virtual string ConvertToTuple(int number)
{
return Convert(number);
}
}
}
Expand Up @@ -43,5 +43,12 @@ public interface INumberToWordsConverter
/// <param name="gender"></param>
/// <returns></returns>
string ConvertToOrdinal(int number, GrammaticalGender gender);

/// <summary>
/// Converts integer to named tuple (e.g. 'single', 'double' etc.).
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
string ConvertToTuple(int number);
}
}
Expand Up @@ -19,6 +19,19 @@ internal class TurkishNumberToWordConverter : GenderlessNumberToWordsConverter
{'a', "ıncı"},
};

private static readonly Dictionary<char, string> TupleSuffix = new Dictionary<char, string>
{
{'ı', "lı"},
{'i', "li"},
{'u', "lu"},
{'ü', "lü"},
{'o', "lu"},
{'ö', "lü"},
{'e', "li"},
{'a', "lı"},
};


public override string Convert(long input)
{
var number = input;
Expand Down Expand Up @@ -121,5 +134,29 @@ public override string ConvertToOrdinal(int number)

return string.Format("{0}{1}", word, wordSuffix);
}

public override string ConvertToTuple(int number)
{
switch (number)
{
case 1:
return "tek";
case 2:
return "çift";
default:
var word = Convert(number);
var wordSuffix = string.Empty;

for (var i = word.Length - 1; i >= 0; i--)
{
if (TupleSuffix.TryGetValue(word[i], out wordSuffix))
{
break;
}
}

return string.Format("{0}{1}", word, wordSuffix);
}
}
}
}
11 changes: 11 additions & 0 deletions src/Humanizer/NumberToWordsExtension.cs
Expand Up @@ -118,5 +118,16 @@ public static string ToOrdinalWords(this int number, GrammaticalGender gender, C
{
return Configurator.GetNumberToWordsConverter(culture).ConvertToOrdinal(number, gender);
}

/// <summary>
/// 1.ToTuple() -> "single"
/// </summary>
/// <param name="number">Number to be turned to tuple</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns></returns>
public static string ToTuple(this int number, CultureInfo culture = null)
{
return Configurator.GetNumberToWordsConverter(culture).ConvertToTuple(number);
}
}
}