forked from markvaaz/ScarletCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizationExtensions.cs
More file actions
62 lines (54 loc) · 2.28 KB
/
Copy pathLocalizationExtensions.cs
File metadata and controls
62 lines (54 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Reflection;
using ScarletCore.Localization;
using ScarletCore.Services;
namespace ScarletCore;
/// <summary>
/// Provides extension methods for localizing strings using the server or player language context.
/// </summary>
public static class LocalizationExtensions {
/// <summary>
/// Localize a key using the server language (fallbacks as implemented in LocalizationService.GetText).
/// Example: "help message".Localize()
/// </summary>
public static string Localize(this string key, params object[] parameters) {
if (string.IsNullOrWhiteSpace(key)) return string.Empty;
// Try to resolve custom keys registered by other assemblies.
var assembly = GetOriginatingAssembly();
if (assembly != null) {
var composite = assembly.GetName().Name + ":" + key.Trim();
return Localizer.GetByCompositeKey(null, composite, parameters);
}
return Localizer.GetText(key, parameters);
}
/// <summary>
/// Localize a key for a specific player (uses player's preferred language, server default, then first available).
/// Example: "help message".Localize(playerData)
/// </summary>
public static string Localize(this string key, PlayerData player, params object[] parameters) {
if (string.IsNullOrWhiteSpace(key)) return string.Empty;
if (player == null) return Localizer.GetText(key, parameters);
// Try to determine the originating assembly (the caller outside of ScarletCore)
var assembly = GetOriginatingAssembly();
return Localizer.Get(player, key, assembly, parameters);
}
// Find the first stack frame assembly that is not ScarletCore
private static Assembly GetOriginatingAssembly() {
var scarletName = typeof(LocalizationExtensions).Assembly.GetName().Name;
var stack = new System.Diagnostics.StackTrace();
var frames = stack.GetFrames();
if (frames == null) return null;
foreach (var frame in frames) {
var method = frame.GetMethod();
if (method == null) continue;
var declaring = method.DeclaringType;
if (declaring == null) continue;
var asm = declaring.Assembly;
if (asm == null) continue;
var name = asm.GetName().Name;
if (string.Equals(name, scarletName, StringComparison.OrdinalIgnoreCase)) continue;
return asm;
}
return null;
}
}