Imports System.Globalization
Imports System.Threading
Imports DigitalData.Modules.Logging

''' <summary>
''' Functions relating to i18n, Cultures, Translations
''' </summary>
Public Class LanguageEx
    ''' <summary>
    ''' Sets the Language of the current thread by setting CurrentCulture and CurrentUICulture
    ''' </summary>
    ''' <param name="pLogger">A Logger instance</param>
    ''' <param name="pUserLanguage">A language code in the form of 'de-DE'</param>
    ''' <param name="pUserDateFormat">A custom date pattern</param>
    Public Shared Sub SetApplicationLanguage(pLogger As Logger, pUserLanguage As String, Optional pUserDateFormat As String = Nothing)
        Try
            pLogger.Debug("Setting application language..")

            Dim Culture As New CultureInfo(pUserLanguage)
            If String.IsNullOrEmpty(pUserDateFormat) = False Then
                Culture.DateTimeFormat.ShortDatePattern = pUserDateFormat
            End If

            pLogger.Debug("Culture object for language [{0}] created", pUserLanguage)

            ' The following line provides localization for data formats.
            Thread.CurrentThread.CurrentCulture = Culture
            ' The following line provides localization for the application's user interface.
            Thread.CurrentThread.CurrentUICulture = Culture

            ' Set this culture as the default culture for all threads in this application. 
            ' Note: The following properties are supported in the .NET Framework 4.5+
            CultureInfo.DefaultThreadCurrentCulture = Culture
            CultureInfo.DefaultThreadCurrentUICulture = Culture

            pLogger.Debug("Application language set to [{0}]", Culture.Name)
        Catch ex As Exception
            pLogger.Warn("Could not set application language!")
            pLogger.Error(ex)
        End Try
    End Sub

    ''' <summary>
    ''' Logs the culture settings of the current thread
    ''' </summary>
    ''' <param name="pLogger">A Logger instance</param>
    Public Shared Sub LogApplicationLanguage(pLogger As Logger)
        pLogger.Debug("=== Application Language ===")
        pLogger.Debug("Thread.CurrentThread.CurrentCulture: [{0}]", Thread.CurrentThread.CurrentCulture)
        pLogger.Debug("Thread.CurrentThread.CurrentUICulture: [{0}]", Thread.CurrentThread.CurrentUICulture)
        pLogger.Debug("CultureInfo.DefaultThreadCurrentCulture: [{0}]", CultureInfo.DefaultThreadCurrentCulture)
        pLogger.Debug("CultureInfo.DefaultThreadCurrentUICulture: [{0}]", CultureInfo.DefaultThreadCurrentUICulture)

    End Sub
End Class
