| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CultureService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Threading; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Globalization.Culture; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides services for managing the current culture of the application. |
| | | 15 | | /// Implements both <see cref="ICultureService"/> (read + write) and <see cref="ICultureContext"/> (read-only). |
| | | 16 | | /// When the culture changes, <see cref="CultureChanged"/> is raised outside the internal lock to prevent |
| | | 17 | | /// deadlocks in subscribers, and <see cref="CultureInfo.CurrentCulture" /> / |
| | | 18 | | /// <see cref="CultureInfo.CurrentUICulture" /> of the calling thread are updated automatically. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class CultureService(CultureInfo initialCulture) : ICultureService |
| | | 21 | | { |
| | 78 | 22 | | private readonly Lock _lockObject = new(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="CultureService"/> class with the current culture of the application |
| | | 26 | | /// </summary> |
| | | 27 | | public CultureService() |
| | 18 | 28 | | : this(CultureInfo.CurrentCulture) { } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="CultureService"/> class with the default culture specified in the p |
| | | 32 | | /// </summary> |
| | | 33 | | public CultureService(GlobalizationOptions options) |
| | 12 | 34 | | : this(options.DefaultCulture) { } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public CultureInfo CurrentCulture { get; private set; } = initialCulture; |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public event EventHandler<CultureChangedEventArgs>? CultureChanged; |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | /// <remarks> |
| | | 44 | | /// This method is thread-safe. The <see cref="CultureChanged"/> event is raised <b>outside</b> the internal lock. |
| | | 45 | | /// <see cref="CultureInfo.CurrentCulture"/> and <see cref="CultureInfo.CurrentUICulture"/> of the calling |
| | | 46 | | /// thread are updated so that standard .NET formatting APIs reflect the new culture immediately. |
| | | 47 | | /// </remarks> |
| | | 48 | | public void SetCulture(CultureInfo culture) |
| | | 49 | | { |
| | 5817 | 50 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 51 | | |
| | | 52 | | CultureChangedEventArgs? args; |
| | | 53 | | lock (_lockObject) |
| | | 54 | | { |
| | 6858 | 55 | | if (Equals(CurrentCulture, culture)) return; |
| | | 56 | | |
| | 4770 | 57 | | var old = CurrentCulture; |
| | 4770 | 58 | | CurrentCulture = culture; |
| | 4770 | 59 | | args = new(old, culture); |
| | 4770 | 60 | | } |
| | | 61 | | |
| | | 62 | | // Update the calling thread's culture so that formatting APIs (e.g. ResourceManager, string.Format) |
| | | 63 | | // reflect the change without requiring callers to pass the culture explicitly. |
| | 4770 | 64 | | CultureInfo.CurrentCulture = culture; |
| | 4770 | 65 | | CultureInfo.CurrentUICulture = culture; |
| | | 66 | | |
| | | 67 | | // Raise event outside the lock to prevent deadlocks when subscribers call back into this service. |
| | 4770 | 68 | | CultureChanged?.Invoke(this, args); |
| | 5802 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | 54 | 72 | | public void SetCulture(string cultureCode) => SetCulture(CultureInfo.GetCultureInfo(cultureCode)); |
| | | 73 | | } |
| | | 74 | | |