Example #1
0
        private static async Task InitGlobalConfigAndLanguage()
        {
            string globalConfigFile = ASF.GetFilePath(ASF.EFileType.Config);

            if (string.IsNullOrEmpty(globalConfigFile))
            {
                ASF.ArchiLogger.LogNullError(nameof(globalConfigFile));

                return;
            }

            GlobalConfig globalConfig;

            if (File.Exists(globalConfigFile))
            {
                globalConfig = await GlobalConfig.Load(globalConfigFile).ConfigureAwait(false);

                if (globalConfig == null)
                {
                    ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorGlobalConfigNotLoaded, globalConfigFile));
                    await Task.Delay(5 * 1000).ConfigureAwait(false);
                    await Exit(1).ConfigureAwait(false);

                    return;
                }
            }
            else
            {
                globalConfig = GlobalConfig.Create();
            }

            ASF.InitGlobalConfig(globalConfig);

            if (Debugging.IsUserDebugging)
            {
                ASF.ArchiLogger.LogGenericDebug(globalConfigFile + ": " + JsonConvert.SerializeObject(ASF.GlobalConfig, Formatting.Indented));
            }

            if (!string.IsNullOrEmpty(ASF.GlobalConfig.CurrentCulture))
            {
                try {
                    // GetCultureInfo() would be better but we can't use it for specifying neutral cultures such as "en"
                    CultureInfo culture = CultureInfo.CreateSpecificCulture(ASF.GlobalConfig.CurrentCulture);
                    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture;
                } catch (Exception) {
                    ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture);
                }
            }

            if (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Equals("en"))
            {
                return;
            }

            ResourceSet defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);

            if (defaultResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));

                return;
            }

            HashSet <DictionaryEntry> defaultStringObjects = defaultResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (defaultStringObjects.Count == 0)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));

                return;
            }

            ResourceSet currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

            if (currentResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));

                return;
            }

            HashSet <DictionaryEntry> currentStringObjects = currentResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (currentStringObjects.Count >= defaultStringObjects.Count)
            {
                // Either we have 100% finished translation, or we're missing it entirely and using en-US
                HashSet <DictionaryEntry> testStringObjects = currentStringObjects.ToHashSet();
                testStringObjects.ExceptWith(defaultStringObjects);

                // If we got 0 as final result, this is the missing language
                // Otherwise it's just a small amount of strings that happen to be the same
                if (testStringObjects.Count == 0)
                {
                    currentStringObjects = testStringObjects;
                }
            }

            if (currentStringObjects.Count < defaultStringObjects.Count)
            {
                float translationCompleteness = currentStringObjects.Count / (float)defaultStringObjects.Count;
                ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.TranslationIncomplete, CultureInfo.CurrentUICulture.Name, translationCompleteness.ToString("P1")));
            }
        }
Example #2
0
        private static async Task <bool> InitGlobalConfigAndLanguage()
        {
            string globalConfigFile = ASF.GetFilePath(ASF.EFileType.Config);

            if (string.IsNullOrEmpty(globalConfigFile))
            {
                throw new ArgumentNullException(nameof(globalConfigFile));
            }

            GlobalConfig?globalConfig;

            if (File.Exists(globalConfigFile))
            {
                globalConfig = await GlobalConfig.Load(globalConfigFile).ConfigureAwait(false);

                if (globalConfig == null)
                {
                    ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorGlobalConfigNotLoaded, globalConfigFile));
                    await Task.Delay(5 * 1000).ConfigureAwait(false);
                    await Exit(1).ConfigureAwait(false);

                    return(false);
                }
            }
            else
            {
                globalConfig = new GlobalConfig();
            }

            ASF.InitGlobalConfig(globalConfig);

            if (Debugging.IsDebugConfigured)
            {
                ASF.ArchiLogger.LogGenericDebug(globalConfigFile + ": " + JsonConvert.SerializeObject(ASF.GlobalConfig, Formatting.Indented));
            }

            if (!string.IsNullOrEmpty(ASF.GlobalConfig?.CurrentCulture))
            {
                try {
                    // GetCultureInfo() would be better but we can't use it for specifying neutral cultures such as "en"
                    CultureInfo culture = CultureInfo.CreateSpecificCulture(ASF.GlobalConfig !.CurrentCulture !);
                    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture;
                } catch (Exception e) {
                    ASF.ArchiLogger.LogGenericWarningException(e);

                    ASF.ArchiLogger.LogGenericError(Strings.ErrorInvalidCurrentCulture);
                }
            }

            // Skip translation progress for English and invariant (such as "C") cultures
            switch (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
            {
            case "en":
            case "iv":
                return(true);
            }

            // We can't dispose this resource set, as we can't be sure if it isn't used somewhere else, rely on GC in this case
            ResourceSet?defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);

            if (defaultResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultResourceSet));

                return(true);
            }

            HashSet <DictionaryEntry> defaultStringObjects = defaultResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (defaultStringObjects.Count == 0)
            {
                ASF.ArchiLogger.LogNullError(nameof(defaultStringObjects));

                return(true);
            }

            // We can't dispose this resource set, as we can't be sure if it isn't used somewhere else, rely on GC in this case
            ResourceSet?currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

            if (currentResourceSet == null)
            {
                ASF.ArchiLogger.LogNullError(nameof(currentResourceSet));

                return(true);
            }

            HashSet <DictionaryEntry> currentStringObjects = currentResourceSet.Cast <DictionaryEntry>().ToHashSet();

            if (currentStringObjects.Count >= defaultStringObjects.Count)
            {
                // Either we have 100% finished translation, or we're missing it entirely and using en-US
                HashSet <DictionaryEntry> testStringObjects = currentStringObjects.ToHashSet();
                testStringObjects.ExceptWith(defaultStringObjects);

                // If we got 0 as final result, this is the missing language
                // Otherwise it's just a small amount of strings that happen to be the same
                if (testStringObjects.Count == 0)
                {
                    currentStringObjects = testStringObjects;
                }
            }

            if (currentStringObjects.Count < defaultStringObjects.Count)
            {
                float translationCompleteness = currentStringObjects.Count / (float)defaultStringObjects.Count;
                ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.TranslationIncomplete, CultureInfo.CurrentUICulture.Name, translationCompleteness.ToString("P1")));
            }

            return(true);
        }