ResourceFallbackManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. /*============================================================
  5. **
  6. **
  7. **
  8. **
  9. **
  10. ** Purpose: Encapsulates CultureInfo fallback for resource
  11. ** lookup
  12. **
  13. **
  14. ===========================================================*/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Globalization;
  19. using System.Runtime.CompilerServices;
  20. using System.Runtime.InteropServices;
  21. using System.Runtime.Versioning;
  22. namespace System.Resources
  23. {
  24. internal class ResourceFallbackManager : IEnumerable<CultureInfo>
  25. {
  26. private CultureInfo m_startingCulture;
  27. private CultureInfo m_neutralResourcesCulture;
  28. private bool m_useParents;
  29. internal ResourceFallbackManager(CultureInfo startingCulture, CultureInfo neutralResourcesCulture, bool useParents)
  30. {
  31. if (startingCulture != null)
  32. {
  33. m_startingCulture = startingCulture;
  34. }
  35. else
  36. {
  37. m_startingCulture = CultureInfo.CurrentUICulture;
  38. }
  39. m_neutralResourcesCulture = neutralResourcesCulture;
  40. m_useParents = useParents;
  41. }
  42. IEnumerator IEnumerable.GetEnumerator()
  43. {
  44. return GetEnumerator();
  45. }
  46. // WARING: This function must be kept in sync with ResourceManager.GetFirstResourceSet()
  47. public IEnumerator<CultureInfo> GetEnumerator()
  48. {
  49. bool reachedNeutralResourcesCulture = false;
  50. // 1. starting culture chain, up to neutral
  51. CultureInfo currentCulture = m_startingCulture;
  52. do
  53. {
  54. if (m_neutralResourcesCulture != null && currentCulture.Name == m_neutralResourcesCulture.Name)
  55. {
  56. // Return the invariant culture all the time, even if the UltimateResourceFallbackLocation
  57. // is a satellite assembly. This is fixed up later in ManifestBasedResourceGroveler::UltimateFallbackFixup.
  58. yield return CultureInfo.InvariantCulture;
  59. reachedNeutralResourcesCulture = true;
  60. break;
  61. }
  62. yield return currentCulture;
  63. currentCulture = currentCulture.Parent;
  64. } while (m_useParents && !currentCulture.HasInvariantCultureName);
  65. if (!m_useParents || m_startingCulture.HasInvariantCultureName)
  66. {
  67. yield break;
  68. }
  69. // 2. invariant
  70. // Don't return invariant twice though.
  71. if (reachedNeutralResourcesCulture)
  72. yield break;
  73. yield return CultureInfo.InvariantCulture;
  74. }
  75. }
  76. }