Skip to content

Commit

Permalink
Merge pull request #1094 from hakanoo/LocalizeTupleMethod
Browse files Browse the repository at this point in the history
Localisation of Tupleize method & Add Turkish support
  • Loading branch information
clairernovotny authored Jul 3, 2021
2 parents 7f80e03 + 629a7dc commit c17c86b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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";
}
}
}
}
Original file line number Diff line number Diff line change
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);
}
}
}
Original file line number Diff line number Diff line change
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);
}
}
}
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}
}

0 comments on commit c17c86b

Please sign in to comment.