1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Text . Json ;
4
+ using System . Threading . Tasks ;
5
+ using Microsoft . Extensions . Caching . Memory ;
3
6
using Microsoft . Extensions . Hosting ;
4
7
5
8
namespace API . Services ;
@@ -8,19 +11,21 @@ namespace API.Services;
8
11
9
12
public interface ILocalizationService
10
13
{
11
- void LoadLanguage ( string languageCode ) ;
12
- // string Get(string key);
14
+ Task < Dictionary < string , string > > LoadLanguage ( string languageCode ) ;
15
+ Task < string > Get ( string locale , string key , params object [ ] args ) ;
13
16
}
14
17
15
18
public class LocalizationService : ILocalizationService
16
19
{
17
20
private readonly IDirectoryService _directoryService ;
21
+ private readonly IMemoryCache _cache ;
18
22
private readonly string _localizationDirectory ;
19
- private dynamic ? _languageLocale ;
20
23
21
- public LocalizationService ( IDirectoryService directoryService , IHostEnvironment environment )
24
+
25
+ public LocalizationService ( IDirectoryService directoryService , IHostEnvironment environment , IMemoryCache cache )
22
26
{
23
27
_directoryService = directoryService ;
28
+ _cache = cache ;
24
29
if ( environment . IsDevelopment ( ) )
25
30
{
26
31
_localizationDirectory = directoryService . FileSystem . Path . Join (
@@ -40,20 +45,45 @@ public LocalizationService(IDirectoryService directoryService, IHostEnvironment
40
45
/// </summary>
41
46
/// <param name="languageCode"></param>
42
47
/// <returns></returns>
43
- public void LoadLanguage ( string languageCode )
48
+ public async Task < Dictionary < string , string > > LoadLanguage ( string languageCode )
44
49
{
45
50
var languageFile = _directoryService . FileSystem . Path . Join ( _localizationDirectory , languageCode + ".json" ) ;
46
51
if ( ! _directoryService . FileSystem . FileInfo . New ( languageFile ) . Exists )
47
52
throw new ArgumentException ( $ "Language { languageCode } does not exist") ;
48
53
49
- var json = _directoryService . FileSystem . File . ReadAllText ( languageFile ) ;
50
- _languageLocale = JsonSerializer . Deserialize < dynamic > ( json ) ;
54
+ var json = await _directoryService . FileSystem . File . ReadAllTextAsync ( languageFile ) ;
55
+ return Newtonsoft . Json . JsonConvert . DeserializeObject < Dictionary < string , string > > ( json ) ;
51
56
}
52
57
53
- // public string Get(string key)
54
- // {
55
- // if (_languageLocale == null) return key;
56
- // return _languageLocale.
57
- //
58
- // }
58
+ public async Task < string > Get ( string locale , string key , params object [ ] args )
59
+ {
60
+ // Check if the translation for the given locale is cached
61
+ if ( ! _cache . TryGetValue ( $ "{ locale } _{ key } ", out string translatedString ) )
62
+ {
63
+ // Load the locale JSON file
64
+ var translationData = await LoadLanguage ( locale ) ;
65
+
66
+ // Find the translation for the given key
67
+ if ( translationData . TryGetValue ( key , out string value ) )
68
+ {
69
+ translatedString = value ;
70
+
71
+ // Cache the translation for subsequent requests
72
+ _cache . Set ( $ "{ locale } _{ key } ", translatedString , TimeSpan . FromMinutes ( 15 ) ) ; // Cache for 15 minutes
73
+ }
74
+ else
75
+ {
76
+ // If the key is not found, use the key as the translated string
77
+ translatedString = key ;
78
+ }
79
+ }
80
+
81
+ // Format the translated string with arguments
82
+ if ( args . Length > 0 )
83
+ {
84
+ translatedString = string . Format ( translatedString , args ) ;
85
+ }
86
+
87
+ return translatedString ;
88
+ }
59
89
}
0 commit comments