Beispiel #1
0
        public static TextEntry CreateSettingsEntry <T>(Panel parent, string title, T defaultValue, string description, Action <T> OnSubmit = null, Action <T> OnChange = null)
        {
            TranslationLabel textLabel = parent.Add.TranslationLabel(new TranslationData(title));

            textLabel.AddTooltip(new TranslationData(description));

            TranslationTextEntry textEntry = parent.Add.TranslationTextEntry();

            textEntry.Text = defaultValue.ToString();

            textEntry.AddEventListener("onsubmit", (panelEvent) =>
            {
                try
                {
                    textEntry.Text.TryToType(typeof(T), out object value);

                    if (value.ToString().Equals(textEntry.Text))
                    {
                        T newValue = (T)value;

                        OnSubmit?.Invoke(newValue);

                        defaultValue = newValue;
                    }
                }
                catch (Exception) { }

                textEntry.Text = defaultValue.ToString();
            });

            textEntry.AddEventListener("onchange", (panelEvent) =>
            {
                try
                {
                    if (string.IsNullOrEmpty(textEntry.Text))
                    {
                        return;
                    }

                    textEntry.Text.TryToType(typeof(T), out object value);

                    if (value.ToString().Equals(textEntry.Text))
                    {
                        T newValue = (T)value;

                        OnChange?.Invoke(newValue);

                        defaultValue = newValue;
                    }
                }
                catch (Exception) { }

                textEntry.Text = defaultValue.ToString();
            });

            return(textEntry);
        }
Beispiel #2
0
        public static void SeedTranslations(
            DatabaseContext context,
            List <BaseTranslationInitializer> listTranslationInitializers)
        {
            var listTranslationLabelDefinitions = context
                                                  .TranslationLabelDefinition
                                                  .ToList();

            var listLanguages = context.Language.ToList();

            var seedList = new List <TranslationLabel>();

            foreach (var initializer in listTranslationInitializers)
            {
                var translations = initializer.Translations();

                foreach (var translation in translations)
                {
                    var definition = listTranslationLabelDefinitions.FirstOrDefault(
                        x => x.Module.Equals(initializer.Module, StringComparison.OrdinalIgnoreCase) &&
                        x.Type.Equals(initializer.Type, StringComparison.OrdinalIgnoreCase) &&
                        x.Key.Equals(translation.Key, StringComparison.OrdinalIgnoreCase));

                    var translationLabels = new List <TranslationLabel>();

                    if (definition.IsNullOrDefault())
                    {
                        definition = new TranslationLabelDefinition
                        {
                            Module = initializer.Module,
                            Type   = initializer.Type,
                            Key    = translation.Key
                        };

                        context.TranslationLabelDefinition.Add(definition);
                        context.SaveChanges();
                    }
                    else
                    {
                        var databaseTranslationLabels = context
                                                        .TranslationLabel
                                                        .Where(x => x.TranslationLabelDefinition_Id == definition.Id)
                                                        .ToList();

                        translationLabels.AddRange(databaseTranslationLabels);
                    }

                    if (!string.IsNullOrWhiteSpace(translation.NL))
                    {
                        var language          = listLanguages.First(x => x.Id == 1);
                        var translationExists = translationLabels.Any(
                            x => x.Language_Id == language.Id &&
                            x.TranslationLabelDefinition_Id == definition.Id);

                        var translationLabel = new TranslationLabel
                        {
                            TranslationLabelDefinition_Id = definition.Id,
                            TranslationLabelDefinition    = definition,
                            Language_Id   = language.Id,
                            Language      = language,
                            Label         = translation.NL,
                            TrackingState = translationExists ? TrackingState.Unchanged : TrackingState.Added
                        };

                        seedList.Add(translationLabel);
                    }

                    if (!string.IsNullOrWhiteSpace(translation.EN))
                    {
                        var language          = listLanguages.First(x => x.Id == 2);
                        var translationExists = translationLabels.Any(
                            x => x.Language_Id == language.Id &&
                            x.TranslationLabelDefinition_Id == definition.Id);

                        var translationLabel = new TranslationLabel
                        {
                            TranslationLabelDefinition_Id = definition.Id,
                            TranslationLabelDefinition    = definition,
                            Language_Id   = language.Id,
                            Language      = language,
                            Label         = translation.EN,
                            TrackingState = translationExists ? TrackingState.Unchanged : TrackingState.Added
                        };

                        seedList.Add(translationLabel);
                    }

                    if (!string.IsNullOrWhiteSpace(translation.DE))
                    {
                        var language          = listLanguages.First(x => x.Id == 3);
                        var translationExists = translationLabels.Any(
                            x => x.Language_Id == language.Id &&
                            x.TranslationLabelDefinition_Id == definition.Id);

                        var translationLabel = new TranslationLabel
                        {
                            TranslationLabelDefinition_Id = definition.Id,
                            TranslationLabelDefinition    = definition,
                            Language_Id   = language.Id,
                            Language      = language,
                            Label         = translation.DE,
                            TrackingState = translationExists ? TrackingState.Unchanged : TrackingState.Added
                        };

                        seedList.Add(translationLabel);
                    }

                    if (!string.IsNullOrWhiteSpace(translation.FR))
                    {
                        var language          = listLanguages.First(x => x.Id == 4);
                        var translationExists = translationLabels.Any(
                            x => x.Language_Id == language.Id &&
                            x.TranslationLabelDefinition_Id == definition.Id);

                        var translationLabel = new TranslationLabel
                        {
                            TranslationLabelDefinition_Id = definition.Id,
                            TranslationLabelDefinition    = definition,
                            Language_Id   = language.Id,
                            Language      = language,
                            Label         = translation.FR,
                            TrackingState = translationExists ? TrackingState.Unchanged : TrackingState.Added
                        };

                        seedList.Add(translationLabel);
                    }
                }
            }

            context.AddOrUpdateRange(seedList);
        }