Skip to content

Commit ae0ae32

Browse files
committed
feat(i18n): implement IEnumerable interface for translation enumeration
- Added IEnumerable<KeyValuePair<string, string>> implementation to the I18N class for enumerating merged translations. - Introduced a snapshot copy mechanism to prevent collection-modified exceptions during iteration. - Enhanced the class with a new GetEnumerator method for improved usability in translation management.
1 parent c088558 commit ae0ae32

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

Utils/I18N.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections;
12
using System.Collections.ObjectModel;
23
using System.Reflection;
34
using System.Text.Json;
@@ -10,7 +11,7 @@ namespace STS2RitsuLib.Utils
1011
/// Loads merged JSON translation dictionaries from the file system, embedded resources, and PCK paths,
1112
/// reacting to game locale changes when possible.
1213
/// </summary>
13-
public class I18N : IDisposable
14+
public class I18N : IDisposable, IEnumerable<KeyValuePair<string, string>>
1415
{
1516
private readonly string[] _fsFolders;
1617
private readonly string _instanceName;
@@ -59,6 +60,24 @@ public void Dispose()
5960
GC.SuppressFinalize(this);
6061
}
6162

63+
/// <summary>
64+
/// Enumerates the current merged translations as key-value pairs.
65+
/// </summary>
66+
/// <remarks>
67+
/// Enumeration uses a snapshot copy to avoid collection-modified exceptions if reload happens during iteration.
68+
/// </remarks>
69+
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
70+
{
71+
ObjectDisposedException.ThrowIf(_disposed, this);
72+
EnsureLoaded();
73+
return _translations.ToArray().AsEnumerable().GetEnumerator();
74+
}
75+
76+
IEnumerator IEnumerable.GetEnumerator()
77+
{
78+
return GetEnumerator();
79+
}
80+
6281
/// <summary>
6382
/// Raised after translations are reloaded (locale change or <see cref="ForceReload" />).
6483
/// </summary>

0 commit comments

Comments
 (0)