< Summary

Class:GDX.Localization
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Localization.cs
Covered lines:17
Uncovered lines:43
Coverable lines:60
Total lines:237
Line coverage:28.3% (17 of 60)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:7
Method coverage:42.8% (3 of 7)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Localization()0%2100%
GetHumanReadableFileSize(...)0%330100%
GetISO639_1(...)0%1561200%
GetIETF_BCP47(...)0%54.6712033.33%
GetSystemLanguage()0%72800%
GetTimestampFormat(...)0%1561200%
SetDefaultCulture()0%4.123050%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Localization.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 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
 5using System;
 6using System.Globalization;
 7using System.Runtime.CompilerServices;
 8using UnityEngine;
 9
 10namespace 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        ///     An array of strings representative for file size formats.
 20        /// </summary>
 021        public static readonly string[] ByteSizes = { "B", "KB", "MB", "GB", "TB" };
 22
 23        /// <summary>
 24        ///     A list of supported languages.
 25        /// </summary>
 26        /// <remarks>
 27        ///     This does not differentiate between things like French Canadian and French.
 28        /// </remarks>
 29        public enum Language : ushort
 30        {
 31            Development = 0,
 32            Default = 1,
 33            English = 5,
 34            German = 10,
 35            Spanish = 15,
 36            French = 20,
 37            Polish = 25,
 38            Russian = 30
 39        }
 40
 41        /// <summary>
 42        ///     The UTC ISO 8601 compliant <see cref="DateTime.ToString(System.String)" />.
 43        /// </summary>
 44        public const string UtcTimestampFormat = "yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz";
 45
 46        /// <summary>
 47        ///     The local ISO 8601 compliant <see cref="DateTime.ToString(System.String)" />.
 48        /// </summary>
 49        public const string LocalTimestampFormat = "yyyy-MM-dd HH\\:mm\\:ss";
 50
 51        /// <summary>
 52        ///     Creates a more human readable <see cref="string" /> of a byte size.
 53        /// </summary>
 54        /// <example>
 55        ///     A byte size of 1024, will return a string of 1 KB.
 56        /// </example>
 57        /// <param name="base2Size">The number of bytes (binary) to measure.</param>
 58        /// <returns>A human readable version of the provided <paramref name="base2Size" />.</returns>
 59        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60        public static string GetHumanReadableFileSize(long base2Size)
 1837161        {
 1837162            long length = base2Size;
 1837163            int order = 0;
 64            const int k_IncrementLengthAdjusted = 4;
 3253565            while (length >= 1024 && order < k_IncrementLengthAdjusted)
 1416466            {
 1416467                order++;
 1416468                length /= 1024;
 1416469            }
 70
 1837171            return $"{length:0.##} {ByteSizes[order]}";
 1837172        }
 73
 74        /// <summary>
 75        ///     <para>Get the ISO 639-1 language code for <paramref name="targetLanguage" />.</para>
 76        /// </summary>
 77        /// <remarks>Two additional non-compliant values may be returned DEV or DEFAULT.</remarks>
 78        /// <param name="targetLanguage">The target <see cref="Language" />.</param>
 79        /// <returns>The language code.</returns>
 80        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 81        public static string GetISO639_1(this Language targetLanguage)
 082        {
 083            switch (targetLanguage)
 84            {
 85                case Language.English:
 086                    return "en";
 87                case Language.German:
 088                    return "de";
 89                case Language.Spanish:
 090                    return "es";
 91                case Language.French:
 092                    return "fr";
 93                case Language.Polish:
 094                    return "pl";
 95                case Language.Russian:
 096                    return "ru";
 97                case Language.Development:
 098                    return "DEV";
 99                case Language.Default:
 0100                    return "DEFAULT";
 101                default:
 0102                    return "en";
 103            }
 0104        }
 105
 106        /// <summary>
 107        ///     <para>Get the IETF BCP 47 language code for <paramref name="targetLanguage" />.</para>
 108        /// </summary>
 109        /// <remarks>Two additional non-compliant values may be returned DEV or DEFAULT.</remarks>
 110        /// <param name="targetLanguage">The target <see cref="Language" />.</param>
 111        /// <returns>The language code.</returns>
 112        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 113        public static string GetIETF_BCP47(this Language targetLanguage)
 1114        {
 1115            switch (targetLanguage)
 116            {
 117                case Language.English:
 1118                    return "en-US";
 119                case Language.German:
 0120                    return "de-DE";
 121                case Language.Spanish:
 0122                    return "es-ES";
 123                case Language.French:
 0124                    return "fr-FR";
 125                case Language.Polish:
 0126                    return "pl-PL";
 127                case Language.Russian:
 0128                    return "ru-RU";
 129                case Language.Development:
 0130                    return "DEV";
 131                case Language.Default:
 0132                    return "DEFAULT";
 133                default:
 0134                    return "en-US";
 135            }
 1136        }
 137
 138        /// <summary>
 139        ///     Get the <see cref="GDX.Localization.Language" /> equivalent of the <see cref="SystemLanguage" />.
 140        /// </summary>
 141        /// <returns>The appropriate <see cref="GDX.Localization.Language" />, or default.</returns>
 142        public static Language GetSystemLanguage()
 0143        {
 144#if UNITY_DOTSRUNTIME
 145            string cultureName = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
 146            if (cultureName.StartsWith("en"))
 147            {
 148                return Language.English;
 149            }
 150            if (cultureName.StartsWith("de"))
 151            {
 152                return Language.German;
 153            }
 154            if (cultureName.StartsWith("ru"))
 155            {
 156                return Language.Russian;
 157            }
 158            if (cultureName.StartsWith("pl"))
 159            {
 160                return Language.Polish;
 161            }
 162            if (cultureName.StartsWith("fr"))
 163            {
 164                return Language.French;
 165            }
 166            if (cultureName.StartsWith("es"))
 167            {
 168                return Language.Spanish;
 169            }
 170
 171            return Language.Default;
 172#else
 0173            SystemLanguage language = Application.systemLanguage;
 0174            switch (language)
 175            {
 176                case SystemLanguage.German:
 0177                    return Language.German;
 178                case SystemLanguage.Russian:
 0179                    return Language.Russian;
 180                case SystemLanguage.Polish:
 0181                    return Language.Polish;
 182                case SystemLanguage.French:
 0183                    return Language.French;
 184                case SystemLanguage.Spanish:
 0185                    return Language.Spanish;
 186                case SystemLanguage.English:
 0187                    return Language.English;
 188                default:
 0189                    return Language.Default;
 190            }
 191#endif
 0192        }
 193
 194        /// <summary>
 195        ///     Get the localized <see cref="System.DateTime" />.<see cref="System.DateTime.ToString(string)" /> for
 196        ///     <paramref name="targetLanguage" />.
 197        /// </summary>
 198        /// <param name="targetLanguage">The target <see cref="Language" />.</param>
 199        /// <returns>The format <see cref="System.String" />.</returns>
 200        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 201        public static string GetTimestampFormat(this Language targetLanguage)
 0202        {
 0203            switch (targetLanguage)
 204            {
 205                case Language.English:
 0206                    return "M/d/yyyy h:mm a";
 207                case Language.Russian:
 208                case Language.Spanish:
 209                case Language.Polish:
 210                case Language.German:
 211                case Language.French:
 0212                    return "d/M/yyyy h:mm a";
 213                case Language.Development:
 0214                    return UtcTimestampFormat;
 215                case Language.Default:
 0216                    return LocalTimestampFormat;
 217                default:
 0218                    return LocalTimestampFormat;
 219            }
 0220        }
 221
 222        /// <summary>
 223        ///     Sets the current threads culture to a defined setting in <see cref="Config" />.
 224        /// </summary>
 225        /// <remarks>
 226        ///     Can be used to avoid issues with culture settings without a Gregorian Calendar. Configurable to automati
 227        ///     execute after assemblies are loaded.
 228        /// </remarks>
 229        public static void SetDefaultCulture()
 1230        {
 1231            if (Config.LocalizationSetDefaultCulture && GetSystemLanguage() == Language.Default)
 0232            {
 0233                CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(Config.LocalizationDefaultCulture.GetIETF_BCP4
 0234            }
 1235        }
 236    }
 237}