| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GlobalizationServices.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | using MyNet.Globalization.Culture; |
| | | 10 | | using MyNet.Globalization.DateTime; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Globalization.Facade; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides static access to globalization-related services. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class GlobalizationServices |
| | | 18 | | { |
| | 9 | 19 | | private static readonly GlobalizationServiceProxy CurrentProxy = new(CreateDefaultGlobalization()); |
| | | 20 | | |
| | | 21 | | private static int _configured; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Configures the globalization services by providing an <see cref="IServiceProvider"/>. |
| | | 25 | | /// Call this once during application startup after building the DI container. |
| | | 26 | | /// </summary> |
| | | 27 | | public static void Configure(IGlobalizationService globalization) |
| | | 28 | | { |
| | 9 | 29 | | ArgumentNullException.ThrowIfNull(globalization); |
| | | 30 | | |
| | 9 | 31 | | if (Interlocked.Exchange(ref _configured, 1) == 1) |
| | 3 | 32 | | return; |
| | | 33 | | |
| | 6 | 34 | | CurrentProxy.SetTarget(globalization); |
| | 6 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the combined <see cref="IGlobalizationService"/> providing both culture and time zone management. |
| | | 39 | | /// The returned instance is stable for the application lifetime; <see cref="Configure"/> only replaces its target. |
| | | 40 | | /// </summary> |
| | 5928 | 41 | | public static IGlobalizationService Current => CurrentProxy; |
| | | 42 | | |
| | | 43 | | private static GlobalizationService CreateDefaultGlobalization() |
| | | 44 | | { |
| | 9 | 45 | | CultureService defaultCultureService = new(); |
| | 9 | 46 | | TimeZoneService defaultTimeZoneService = new(); |
| | | 47 | | |
| | 9 | 48 | | return new(defaultCultureService, defaultTimeZoneService); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |