< Summary

Class:GDX.Localization
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Localization.cs
Covered lines:18
Uncovered lines:42
Coverable lines:60
Total lines:243
Line coverage:30% (18 of 60)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:7
Method coverage:57.1% (4 of 7)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Localization()0%110100%
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)

./Packages/com.dotbunny.gdx/GDX/Localization.cs

#LineLine coverage
 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
 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        ///     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>
 254        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)
 1816766        {
 1816767            long length = base2Size;
 1816768            int order = 0;
 69            const int k_IncrementLengthAdjusted = 4;
 2318970            while (length >= 1024 && order < k_IncrementLengthAdjusted)
 502271            {
 502272                order++;
 502273                length /= 1024;
 502274            }
 75
 1816776            return $"{length:0.##} {ByteSizes[order]}";
 1816777        }
 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)
 087        {
 088            switch (targetLanguage)
 89            {
 90                case Language.English:
 091                    return "en";
 92                case Language.German:
 093                    return "de";
 94                case Language.Spanish:
 095                    return "es";
 96                case Language.French:
 097                    return "fr";
 98                case Language.Polish:
 099                    return "pl";
 100                case Language.Russian:
 0101                    return "ru";
 102                case Language.Development:
 0103                    return "DEV";
 104                case Language.Default:
 0105                    return "DEFAULT";
 106                default:
 0107                    return "en";
 108            }
 0109        }
 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)
 1119        {
 1120            switch (targetLanguage)
 121            {
 122                case Language.English:
 1123                    return "en-US";
 124                case Language.German:
 0125                    return "de-DE";
 126                case Language.Spanish:
 0127                    return "es-ES";
 128                case Language.French:
 0129                    return "fr-FR";
 130                case Language.Polish:
 0131                    return "pl-PL";
 132                case Language.Russian:
 0133                    return "ru-RU";
 134                case Language.Development:
 0135                    return "DEV";
 136                case Language.Default:
 0137                    return "DEFAULT";
 138                default:
 0139                    return "en-US";
 140            }
 1141        }
 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()
 0148        {
 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
 0178            SystemLanguage language = Application.systemLanguage;
 0179            switch (language)
 180            {
 181                case SystemLanguage.German:
 0182                    return Language.German;
 183                case SystemLanguage.Russian:
 0184                    return Language.Russian;
 185                case SystemLanguage.Polish:
 0186                    return Language.Polish;
 187                case SystemLanguage.French:
 0188                    return Language.French;
 189                case SystemLanguage.Spanish:
 0190                    return Language.Spanish;
 191                case SystemLanguage.English:
 0192                    return Language.English;
 193                default:
 0194                    return Language.Default;
 195            }
 196#endif // UNITY_DOTSRUNTIME
 0197        }
 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)
 0207        {
 0208            switch (targetLanguage)
 209            {
 210                case Language.English:
 0211                    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:
 0217                    return "d/M/yyyy h:mm a";
 218                case Language.Development:
 0219                    return UtcTimestampFormat;
 220                case Language.Default:
 0221                    return LocalTimestampFormat;
 222                default:
 0223                    return LocalTimestampFormat;
 224            }
 0225        }
 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()
 3235        {
 3236            if (Config.LocalizationSetDefaultCulture && GetSystemLanguage() == Language.Default)
 0237            {
 0238                CultureInfo.DefaultThreadCurrentCulture =
 239                    new CultureInfo(Config.LocalizationDefaultCulture.GetIETF_BCP47());
 0240            }
 3241        }
 242    }
 243}

Coverage by test methods











Methods/Properties

Localization()
GetHumanReadableFileSize(System.Int64)
GetISO639_1(GDX.Localization/Language)
GetIETF_BCP47(GDX.Localization/Language)
GetSystemLanguage()
GetTimestampFormat(GDX.Localization/Language)
SetDefaultCulture()