| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A collection of localization related helper utilities. |
| | 14 | | /// </summary> |
| | 15 | | [VisualScriptingCompatible(8)] |
| | 16 | | public static class Localization |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// A list of supported languages. |
| | 20 | | /// </summary> |
| | 21 | | /// <remarks> |
| | 22 | | /// This does not differentiate between things like French Canadian and French. |
| | 23 | | /// </remarks> |
| | 24 | | public enum Language : ushort |
| | 25 | | { |
| | 26 | | Development = 0, |
| | 27 | | Default = 1, |
| | 28 | | English = 5, |
| | 29 | | German = 10, |
| | 30 | | Spanish = 15, |
| | 31 | | French = 20, |
| | 32 | | Polish = 25, |
| | 33 | | Russian = 30 |
| | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// The UTC ISO 8601 compliant <see cref="DateTime.ToString(System.String)" />. |
| | 38 | | /// </summary> |
| | 39 | | public const string UtcTimestampFormat = "yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz"; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// The local ISO 8601 compliant <see cref="DateTime.ToString(System.String)" />. |
| | 43 | | /// </summary> |
| | 44 | | public const string LocalTimestampFormat = "yyyy-MM-dd HH\\:mm\\:ss"; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// A filename safe <see cref="DateTime.ToString(System.String)" />. |
| | 48 | | /// </summary> |
| | 49 | | public const string FilenameTimestampFormat = "yyyyMMdd_HHmmss"; |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// An array of strings representative for file size formats. |
| | 53 | | /// </summary> |
| 2 | 54 | | public static readonly string[] ByteSizes = { "B", "KB", "MB", "GB", "TB" }; |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Creates a more human readable <see cref="string" /> of a byte size. |
| | 58 | | /// </summary> |
| | 59 | | /// <example> |
| | 60 | | /// A byte size of 1024, will return a string of 1 KB. |
| | 61 | | /// </example> |
| | 62 | | /// <param name="base2Size">The number of bytes (binary) to measure.</param> |
| | 63 | | /// <returns>A human readable version of the provided <paramref name="base2Size" />.</returns> |
| | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 65 | | public static string GetHumanReadableFileSize(long base2Size) |
| 18167 | 66 | | { |
| 18167 | 67 | | long length = base2Size; |
| 18167 | 68 | | int order = 0; |
| | 69 | | const int k_IncrementLengthAdjusted = 4; |
| 23189 | 70 | | while (length >= 1024 && order < k_IncrementLengthAdjusted) |
| 5022 | 71 | | { |
| 5022 | 72 | | order++; |
| 5022 | 73 | | length /= 1024; |
| 5022 | 74 | | } |
| | 75 | |
|
| 18167 | 76 | | return $"{length:0.##} {ByteSizes[order]}"; |
| 18167 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// <para>Get the ISO 639-1 language code for <paramref name="targetLanguage" />.</para> |
| | 81 | | /// </summary> |
| | 82 | | /// <remarks>Two additional non-compliant values may be returned DEV or DEFAULT.</remarks> |
| | 83 | | /// <param name="targetLanguage">The target <see cref="Language" />.</param> |
| | 84 | | /// <returns>The language code.</returns> |
| | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 86 | | public static string GetISO639_1(this Language targetLanguage) |
| 0 | 87 | | { |
| 0 | 88 | | switch (targetLanguage) |
| | 89 | | { |
| | 90 | | case Language.English: |
| 0 | 91 | | return "en"; |
| | 92 | | case Language.German: |
| 0 | 93 | | return "de"; |
| | 94 | | case Language.Spanish: |
| 0 | 95 | | return "es"; |
| | 96 | | case Language.French: |
| 0 | 97 | | return "fr"; |
| | 98 | | case Language.Polish: |
| 0 | 99 | | return "pl"; |
| | 100 | | case Language.Russian: |
| 0 | 101 | | return "ru"; |
| | 102 | | case Language.Development: |
| 0 | 103 | | return "DEV"; |
| | 104 | | case Language.Default: |
| 0 | 105 | | return "DEFAULT"; |
| | 106 | | default: |
| 0 | 107 | | return "en"; |
| | 108 | | } |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// <para>Get the IETF BCP 47 language code for <paramref name="targetLanguage" />.</para> |
| | 113 | | /// </summary> |
| | 114 | | /// <remarks>Two additional non-compliant values may be returned DEV or DEFAULT.</remarks> |
| | 115 | | /// <param name="targetLanguage">The target <see cref="Language" />.</param> |
| | 116 | | /// <returns>The language code.</returns> |
| | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 118 | | public static string GetIETF_BCP47(this Language targetLanguage) |
| 1 | 119 | | { |
| 1 | 120 | | switch (targetLanguage) |
| | 121 | | { |
| | 122 | | case Language.English: |
| 1 | 123 | | return "en-US"; |
| | 124 | | case Language.German: |
| 0 | 125 | | return "de-DE"; |
| | 126 | | case Language.Spanish: |
| 0 | 127 | | return "es-ES"; |
| | 128 | | case Language.French: |
| 0 | 129 | | return "fr-FR"; |
| | 130 | | case Language.Polish: |
| 0 | 131 | | return "pl-PL"; |
| | 132 | | case Language.Russian: |
| 0 | 133 | | return "ru-RU"; |
| | 134 | | case Language.Development: |
| 0 | 135 | | return "DEV"; |
| | 136 | | case Language.Default: |
| 0 | 137 | | return "DEFAULT"; |
| | 138 | | default: |
| 0 | 139 | | return "en-US"; |
| | 140 | | } |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// Get the <see cref="GDX.Localization.Language" /> equivalent of the <see cref="SystemLanguage" />. |
| | 145 | | /// </summary> |
| | 146 | | /// <returns>The appropriate <see cref="GDX.Localization.Language" />, or default.</returns> |
| | 147 | | public static Language GetSystemLanguage() |
| 0 | 148 | | { |
| | 149 | | #if UNITY_DOTSRUNTIME |
| | 150 | | string cultureName = System.Threading.Thread.CurrentThread.CurrentCulture.Name; |
| | 151 | | if (cultureName.StartsWith("en")) |
| | 152 | | { |
| | 153 | | return Language.English; |
| | 154 | | } |
| | 155 | | if (cultureName.StartsWith("de")) |
| | 156 | | { |
| | 157 | | return Language.German; |
| | 158 | | } |
| | 159 | | if (cultureName.StartsWith("ru")) |
| | 160 | | { |
| | 161 | | return Language.Russian; |
| | 162 | | } |
| | 163 | | if (cultureName.StartsWith("pl")) |
| | 164 | | { |
| | 165 | | return Language.Polish; |
| | 166 | | } |
| | 167 | | if (cultureName.StartsWith("fr")) |
| | 168 | | { |
| | 169 | | return Language.French; |
| | 170 | | } |
| | 171 | | if (cultureName.StartsWith("es")) |
| | 172 | | { |
| | 173 | | return Language.Spanish; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | return Language.Default; |
| | 177 | | #else |
| 0 | 178 | | SystemLanguage language = Application.systemLanguage; |
| 0 | 179 | | switch (language) |
| | 180 | | { |
| | 181 | | case SystemLanguage.German: |
| 0 | 182 | | return Language.German; |
| | 183 | | case SystemLanguage.Russian: |
| 0 | 184 | | return Language.Russian; |
| | 185 | | case SystemLanguage.Polish: |
| 0 | 186 | | return Language.Polish; |
| | 187 | | case SystemLanguage.French: |
| 0 | 188 | | return Language.French; |
| | 189 | | case SystemLanguage.Spanish: |
| 0 | 190 | | return Language.Spanish; |
| | 191 | | case SystemLanguage.English: |
| 0 | 192 | | return Language.English; |
| | 193 | | default: |
| 0 | 194 | | return Language.Default; |
| | 195 | | } |
| | 196 | | #endif // UNITY_DOTSRUNTIME |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | /// <summary> |
| | 200 | | /// Get the localized <see cref="System.DateTime" />.<see cref="System.DateTime.ToString(string)" /> for |
| | 201 | | /// <paramref name="targetLanguage" />. |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="targetLanguage">The target <see cref="Language" />.</param> |
| | 204 | | /// <returns>The format <see cref="System.String" />.</returns> |
| | 205 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 206 | | public static string GetTimestampFormat(this Language targetLanguage) |
| 0 | 207 | | { |
| 0 | 208 | | switch (targetLanguage) |
| | 209 | | { |
| | 210 | | case Language.English: |
| 0 | 211 | | return "M/d/yyyy h:mm a"; |
| | 212 | | case Language.Russian: |
| | 213 | | case Language.Spanish: |
| | 214 | | case Language.Polish: |
| | 215 | | case Language.German: |
| | 216 | | case Language.French: |
| 0 | 217 | | return "d/M/yyyy h:mm a"; |
| | 218 | | case Language.Development: |
| 0 | 219 | | return UtcTimestampFormat; |
| | 220 | | case Language.Default: |
| 0 | 221 | | return LocalTimestampFormat; |
| | 222 | | default: |
| 0 | 223 | | return LocalTimestampFormat; |
| | 224 | | } |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | /// <summary> |
| | 228 | | /// Sets the current threads culture to a defined setting in <see cref="Config" />. |
| | 229 | | /// </summary> |
| | 230 | | /// <remarks> |
| | 231 | | /// Can be used to avoid issues with culture settings without a Gregorian Calendar. Configurable to automati |
| | 232 | | /// execute after assemblies are loaded. |
| | 233 | | /// </remarks> |
| | 234 | | public static void SetDefaultCulture() |
| 3 | 235 | | { |
| 3 | 236 | | if (Config.LocalizationSetDefaultCulture && GetSystemLanguage() == Language.Default) |
| 0 | 237 | | { |
| 0 | 238 | | CultureInfo.DefaultThreadCurrentCulture = |
| | 239 | | new CultureInfo(Config.LocalizationDefaultCulture.GetIETF_BCP47()); |
| 0 | 240 | | } |
| 3 | 241 | | } |
| | 242 | | } |
| | 243 | | } |