KGy SOFT

LanguageSettings Class

KGy SOFT Core Libraries Help
Represents the language settings of the current thread. Use this class also when you want to be notified on language changes and to control the behavior of those DynamicResourceManager instances, which are configured to use centralized settings.
See the Remarks section for details and an example.
Inheritance Hierarchy

SystemObject
  KGySoftLanguageSettings

Namespace:  KGySoft
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0
Syntax

public static class LanguageSettings

The LanguageSettings type exposes the following members.

Properties

  NameDescription
Public propertyStatic memberDisplayLanguage
Gets or sets the display language of the current Thread, which is used for looking up localizable resources. When set, DisplayLanguageChanged and DisplayLanguageChangedGlobal events are triggered.
Default value: The display culture of the operating system.
Public propertyStatic memberDynamicResourceManagersAutoAppend
Gets or sets the auto append options for the DynamicResourceManager instances of the current application domain when their UseLanguageSettings is .
Default value: AppendFirstNeutralCulture, AppendOnLoad
Public propertyStatic memberDynamicResourceManagersAutoSave
Gets or sets the auto saving options for the DynamicResourceManager instances of the current application domain when their UseLanguageSettings is .
Default value: LanguageChange, DomainUnload, SourceChange
Public propertyStatic memberDynamicResourceManagersSource
Gets or sets the source, from which the DynamicResourceManager instances of the current application domain should take the resources when their UseLanguageSettings is .
Default value: CompiledOnly
Public propertyStatic memberFormattingLanguage
Gets or sets the formatting language of the current Thread, which is used for formatting and parsing numbers, date and time values, currency, etc. When set, FormattingLanguageChanged and FormattingLanguageChangedGlobal events are triggered.
Public propertyStatic memberUnknownResourcePrefix
Gets or sets the prefix of an unknown (non-existing) String resource. Used by the DynamicResourceManager instances if DynamicResourceManagersAutoAppend or AutoAppend property is configured to add non existing resources to the invariant resource set.
Default value: [U]
Public propertyStatic memberUntranslatedResourcePrefix
Gets or sets the prefix of an untranslated String resource. Used by the DynamicResourceManager instances if DynamicResourceManagersAutoAppend or AutoAppend property is configured to use auto appending.
Default value: [T]
Top
Events

  NameDescription
Public eventStatic memberDisplayLanguageChanged
Occurs when the display language (Thread.CurrentThread.CurrentUICulture) has been changed by setting the DisplayLanguage property. Only the subscriptions from the same thread are invoked.
Public eventStatic memberDisplayLanguageChangedGlobal
Occurs when the display language (Thread.CurrentThread.CurrentUICulture) has been changed in any Thread by setting DisplayLanguage property. The subscribers are invoked from all threads.
Public eventStatic memberFormattingLanguageChanged
Occurs when the formatting language (Thread.CurrentThread.CurrentCulture) has been changed by setting the FormattingLanguage property. Only the subscriptions from the same thread are invoked.
Public eventStatic memberFormattingLanguageChangedGlobal
Occurs when the formatting language (Thread.CurrentThread.CurrentCulture) has been changed in any Thread by setting FormattingLanguage property. The subscribers are invoked from all threads.
Top
Remarks

If you use KGySoft.CoreLibraries in a class library, then you do not really need to use this class. Just use the publicly available resources via the PublicResources class and let the consumers of your library adjusting the language settings by this class in their applications.

If you use KGySoft.CoreLibraries or other class libraries dependent on KGySoft.CoreLibraries in an application, then use this class to set the language of your application and the behavior of centralized resource managers. The KGySoft.CoreLibraries contains one centralized resource manager for the string resources used in the library. Some of these strings can be publicly accessed via the PublicResources members. See also the example below.

Examples

The following example demonstrates how to generate resource files in your application for any language.
C#
using System;
using System.Globalization;
using KGySoft;
using KGySoft.Resources;

class Example
{
    static void Main(string[] args)
    {
        LanguageSettings.DisplayLanguage = CultureInfo.GetCultureInfo("de-DE");

        // Even though we set the language above centralized resources still return the built-in compiled resources.
        // Of course, if you use compiled resources in your application with the selected language, then they will be considered.
        Console.WriteLine("Non-localized resource:");
        Console.WriteLine(PublicResources.ArgumentNull);
        Console.WriteLine();

        // In an application that uses KGySoft.CoreLibraries you can opt-in to use dynamic resources from .resx files.
        LanguageSettings.DynamicResourceManagersSource = ResourceManagerSources.CompiledAndResX;

        // Next line tells that whenever a resource is requested, which does not exist in the resource file of the display
        // language, then its resource file will be dynamically created and/or expanded by the requested resource.
        LanguageSettings.DynamicResourceManagersAutoAppend = AutoAppendOptions.AppendFirstNeutralCulture;

        // Actually the default value is AutoAppendOptions.AppendFirstNeutralCulture | AutoAppendOptions.AppendOnLoad,
        // which causes to add not just the requested resource but all of the missing ones to the target resource file.
        // Delete the line above to see the difference.

        Console.WriteLine("Localized resource:");
        Console.WriteLine(PublicResources.ArgumentNull);
    }
}

// When this example is executed for the first time it produces the following output:

// Non-localized resource:
// Value cannot be null.
// 
// Localized resource:
// [T]Value cannot be null.

The [T] before a resource indicates that a new resource has been generated, which is not translated yet. In the Resources subfolder of your compiled project now there must be a new file named KGySoft.CoreLibraries.Messages.de.resx (the exact name depends on the display language and the appending strategy). Its content now must be the following:

XML
<?xml version="1.0"?>
<root>
  <data name = "General_ArgumentNull">
    <value>[T]Value cannot be null.</value>
  </data>
</root>

Search for the [T] values in the generated file to find the untranslated resources and feel free to change them. If you change the resource and execute the example again it will now show the translation you provided.

Tip Tip
To see how to add a dynamic resource manager to your own class library see the Recommended usage for string resources in a class library section in the description of the DynamicResourceManager class.
See Also

Reference