using System;
using System.Globalization;

using CommonHelpers;

namespace BookRegistry.Server {

//**************************************************************************************************
public static class LocaleHlp {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[HThreadStatic]
public static void UpdateCulture()
{
   if ( StartupData.UseInvariantCulture )
      SetCulture( null );
}

[HThreadStatic]
public static void UseLanguage(string? language)
{
   string? cultureId = GetSpecificCultureId( language );
   SetCulture( cultureId!=null ? new CultureInfo( cultureId ) : null );
}

//==================================================================================================

static string? GetSpecificCultureId(string? language)
{
   if ( language == null )
      return null;
   switch ( language?.ToUpper() )
   {
   case "EN": return "en-US";
   case "RU": return "ru-RU";
   default: return null;
   } // switch
}

[HThreadStatic]
static void SetCulture(CultureInfo? culture)
{
   if ( culture == null )
      culture = CultureInfo.InvariantCulture;
   var thread = HThread.CurrentThread;
   if (!( thread.CurrentCulture.Equals( culture ) ))
      thread.CurrentCulture = culture;
   if (!( thread.CurrentUICulture.Equals( culture ) ))
      thread.CurrentUICulture = culture;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // LocaleHlp
//**************************************************************************************************

} // BookRegistry.Server
