コード例 #1
0
ファイル: Translations.cs プロジェクト: ChlckenWings/NecroBot
        public static Translation Load(ILogicSettings logicSettings)
        {
            var translationsLanguageCode = logicSettings.TranslationLanguageCode;
            var translationPath = Path.Combine(logicSettings.GeneralConfigPath, "translations");
            var fullPath = Path.Combine(translationPath, "translation." + translationsLanguageCode + ".json");

            Translation translations;
            if (File.Exists(fullPath))
            {
                var input = File.ReadAllText(fullPath);

                var jsonSettings = new JsonSerializerSettings();
                jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
                jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;

                try
                {
                    translations = JsonConvert.DeserializeObject<Translation>( input, jsonSettings );
                    //TODO make json to fill default values as it won't do it now
                    new Translation()._translationStrings.Where(
                        item => translations._translationStrings.All( a => a.Key != item.Key ) )
                        .ToList()
                        .ForEach( translations._translationStrings.Add );
                    new Translation()._pokemonTranslationStrings.Where(
                        item => translations._pokemonTranslationStrings.All( a => a.Key != item.Key ) )
                        .ToList()
                        .ForEach( translations._pokemonTranslationStrings.Add );
                }
                catch( JsonException )
                {
                    Logger.Write( "[ERROR] Issue loading translations", LogLevel.Error );

                    switch( translationsLanguageCode )
                    {
                        case "en":
                            Logger.Write( "[Request] Rebuild the translations folder? Y/N" );

                            string strInput = Console.ReadLine().ToLower();

                            if( strInput.Equals( "y" ) )
                            {
                                // Currently this section can only rebuild the EN translations file \\
                                // This is because default values cannot be supplied from other languages \\
                                Logger.Write( "Loading fresh translations and continuing" );
                                translations = new Translation();
                                translations.Save( Path.Combine( translationPath, "translation.en.json" ) );
                            }
                            else
                            {
                                ErrorHandler.ThrowFatalError( "[ERROR] Fatal Error", 3, LogLevel.Error );
                                return null;
                            }

                            break;
                        default:
                            ErrorHandler.ThrowFatalError( "[ERROR] Fatal Error\n"
                                + "[ERROR] No replacement translations: Check appropriate files for typos", 5, LogLevel.Error );

                            return null;
                    }
                }
            }
            else
            {
                translations = new Translation();
                translations.Save(Path.Combine(translationPath, "translation.en.json"));
            }

            return translations;
        }
コード例 #2
0
        public static Translation Load(ILogicSettings logicSettings)
        {
            var translationsLanguageCode = logicSettings.TranslationLanguageCode;
            var translationPath          = Path.Combine(logicSettings.GeneralConfigPath, "translations");
            var fullPath = Path.Combine(translationPath, "translation." + translationsLanguageCode + ".json");

            Translation translations;

            if (File.Exists(fullPath))
            {
                var input = File.ReadAllText(fullPath);

                var jsonSettings = new JsonSerializerSettings();
                jsonSettings.Converters.Add(new StringEnumConverter {
                    CamelCaseText = true
                });
                jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                jsonSettings.DefaultValueHandling   = DefaultValueHandling.Populate;

                try
                {
                    translations = JsonConvert.DeserializeObject <Translation>(input, jsonSettings);
                    //TODO make json to fill default values as it won't do it now
                    new Translation()._translationStrings.Where(
                        item => translations._translationStrings.All(a => a.Key != item.Key))
                    .ToList()
                    .ForEach(translations._translationStrings.Add);
                    new Translation()._pokemonTranslationStrings.Where(
                        item => translations._pokemonTranslationStrings.All(a => a.Key != item.Key))
                    .ToList()
                    .ForEach(translations._pokemonTranslationStrings.Add);
                }
                catch (JsonException)
                {
                    Logger.Write("[ERROR] Issue loading translations", LogLevel.Error);

                    switch (translationsLanguageCode)
                    {
                    case "en":
                        Logger.Write("[Request] Rebuild the translations folder? Y/N");

                        string strInput = Console.ReadLine().ToLower();

                        if (strInput.Equals("y"))
                        {
                            // Currently this section can only rebuild the EN translations file \\
                            // This is because default values cannot be supplied from other languages \\
                            Logger.Write("Loading fresh translations and continuing");
                            translations = new Translation();
                            translations.Save(Path.Combine(translationPath, "translation.en.json"));
                        }
                        else
                        {
                            ErrorHandler.ThrowFatalError("[ERROR] Fatal Error", 3, LogLevel.Error);
                            return(null);
                        }

                        break;

                    default:
                        ErrorHandler.ThrowFatalError("[ERROR] Fatal Error\n"
                                                     + "[ERROR] No replacement translations: Check appropriate files for typos", 5, LogLevel.Error);

                        return(null);
                    }
                }
            }
            else
            {
                translations = new Translation();
                translations.Save(Path.Combine(translationPath, "translation.en.json"));
            }

            return(translations);
        }
コード例 #3
0
ファイル: Translations.cs プロジェクト: Rozzla/NecroBot
        public static Translation Load(ILogicSettings logicSettings)
        {
            string translationsLanguageCode = logicSettings.TranslationLanguageCode;
            var translationPath = Path.Combine(logicSettings.GeneralConfigPath, "translations");
            var fullPath = Path.Combine(translationPath, "translation." + translationsLanguageCode + ".json");

            Translation translations;
            if (File.Exists(fullPath))
            {
                var input = File.ReadAllText(fullPath);

                var jsonSettings = new JsonSerializerSettings();
                jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
                jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;
                translations = JsonConvert.DeserializeObject<Translation>(input, jsonSettings);
                //TODO make json to fill default values as it won't do it now
                new Translation().TranslationStrings.Where(item => !translations.TranslationStrings.Any(a => a.Key == item.Key))
                    .ToList()
                    .ForEach(translations.TranslationStrings.Add);
            }
            else
            {
                translations = new Translation();
                translations.Save(Path.Combine(translationPath, "translation.en.json"));
            }
            return translations;
        }
コード例 #4
0
ファイル: Translations.cs プロジェクト: xyannis/NecroBot
        public static Translation Load(ILogicSettings logicSettings)
        {
            string translationsLanguageCode = logicSettings.TranslationLanguageCode;
            var translationPath = Path.Combine(logicSettings.GeneralConfigPath, "translations");
            var fullPath = Path.Combine(translationPath, "translation." + translationsLanguageCode + ".json");

            Translation translations;
            if (File.Exists(fullPath))
            {
                var input = File.ReadAllText(fullPath);

                var jsonSettings = new JsonSerializerSettings();
                jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
                jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
                jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;

                translations = JsonConvert.DeserializeObject<Translation>(input, jsonSettings);
                translations.Save(fullPath);
            }
            else
            {
                translations = new Translation();
                translations.Save(Path.Combine(translationPath, "translation.en.json"));
            }
            return translations;
        }