File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/App_Code/Pux/Utils/TextTools.cs
using CMS.Ecommerce;
using CMS.Helpers;
using CMS.SiteProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Pux.Utils
{
public class TextTools
{
/// <summary>
/// Returns corresponding resource string for a number and word.
/// Usage e.g. "1 pivo", "2 piva", "5 piv".
/// </summary>
/// <param name="number"></param>
/// <param name="word"></param>
/// <returns></returns>
public static string Numerate(double number, string word)
{
string key = number >= 5 || number <= 0 ? "5" : number > 1 ? "2" : "1";
return string.Format(ResHelper.GetString(string.Format("{0}.{1}", word, key)), number);
}
/// <summary>
/// Formats price as specified in configuration. If price is zero and freeWord is not null,
/// the freeWord is returned instead the price (usage e.g. "500 EUR / for free")
/// </summary>
/// <param name="number"></param>
/// <param name="currencyName"></param>
/// <param name="forFreeWord"></param>
/// <returns></returns>
public static string FormatPrice(double number, string currencyName, string freeWord, bool useRound = true)
{
var currencyInfo = CurrencyInfoProvider.GetCurrencyInfo(currencyName, SiteContext.CurrentSiteName);
if (currencyInfo == null)
return string.Empty;
if (useRound)
{
var roundedValue = Math.Round(number, currencyInfo.CurrencyRoundTo);
if (roundedValue == 0 && freeWord != null)
return freeWord;
}
if (number == 0 && freeWord != null)
return number.ToString();
return currencyInfo.FormatPrice(number);
}
public static string FormatPriceWithFreeWord(double number, string freeWord)
{
return FormatPrice(number, ECommerceContext.CurrentCurrency.CurrencyCode, freeWord);
}
public static string FormatPriceWithFreeWord(double number, string freeWord, bool useRound = true)
{
return FormatPrice(number, ECommerceContext.CurrentCurrency.CurrencyCode, freeWord, useRound);
}
/// <summary>
/// Tries to load first resource string with sitename prefix (constructs "%SiteName%.stringKey" resource string key).
/// If such resource string does not exist, resource string with name %stringKey% is returned
/// </summary>
/// <param name="stringKey"></param>
/// <returns></returns>
public static string GetSiteResourceString(string stringKey)
{
if (!string.IsNullOrEmpty(stringKey))
{
// add prefix with current site code name
var siteResourceStringKey = SiteContext.CurrentSiteName + "." + stringKey;
// try to translate the site resource string
var siteValue = ResHelper.GetString(siteResourceStringKey);
if (!siteValue.Equals(siteResourceStringKey))
{
// site resource string exists, return it
return siteValue;
}
}
// return default resource string
return ResHelper.GetString(stringKey);
}
public static Dictionary<string, string> GetDictionaryFromText(string text, char delimiter = ';')
{
Dictionary<string, string> dict = new Dictionary<string, string>();
if (!String.IsNullOrEmpty(text))
{
foreach (string line in text.Split('\n').Select(a => a.Trim()))
{
List<string> lineArr = line.Split(delimiter).Select(a => a.Trim()).ToList();
if (lineArr.Count() >= 2)
{
string key = lineArr.ElementAt(0).Trim().ToLowerInvariant();
string value = lineArr.ElementAt(1).Trim();
if (!dict.ContainsKey(key))
{
dict.Add(key, value);
}
}
}
}
return dict;
}
}
}