Ejemplo n.º 1
0
        public IActionResult AddTranslation(TranslationAddModel model)
        {
            if (ModelState.IsValid)
            {
                using (var uow = _localizationDataService.StartUnitOfWork())
                {
                    var resource = uow.ResourceRepository.Get(model.ResourceId);
                    if (resource == null)
                    {
                        return(RedirectToAction(nameof(Index)));
                    }

                    var translation = uow.TranslationRepository.ByResource(resource).SingleOrDefault(t => t.Language == model.Language);
                    if (translation != null)
                    {
                        return(RedirectToAction(nameof(EditTranslation), new { id = translation.Id }));
                    }

                    var newTranslation = new TranslationEntity
                    {
                        ResourceId = model.ResourceId,
                        Language   = model.Language,
                        Value      = model.Value
                    };
                    uow.TranslationRepository.Add(newTranslation);
                    uow.Commit();
                }
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public void ShouldReturnTheNumberTranslation(string originalNumber, int translateNumber)
        {
            TranslationEntity translation = new TranslationEntity();

            translation.TranslateKwegonian(originalNumber);

            translation.ArabicNumeral.Should().Be(translateNumber);
        }
Ejemplo n.º 3
0
        public void ShouldReturnNullIfNotTranslates()
        {
            TranslationEntity translation = new TranslationEntity();

            translation.TranslateKwegonian("popo");

            translation.ArabicNumeral.Should().BeNull();
        }
Ejemplo n.º 4
0
        public TranslationEntity CreateTranslation(int language)
        {
            var t = new TranslationEntity {
                Language = language
            };

            t.MakeDirty();

            return(t);
        }
Ejemplo n.º 5
0
        public Response <decimal> KwegonianToDecimal(string kwegonianNumber)
        {
            try
            {
                var listNumbers = SeparateText(kwegonianNumber);
                int result      = 0;

                for (int i = 0; i < listNumbers.Length; i++)
                {
                    var translete = new TranslationEntity();

                    int arabicNumeral1 = SetArabicNumeral(translete, listNumbers[i]);

                    if (arabicNumeral1 == 0)
                    {
                        return(Response <decimal> .Fail("Number not exists"));
                    }

                    if (i + 1 < listNumbers.Length)
                    {
                        int arabicNumeral2 = SetArabicNumeral(translete, listNumbers[i + 1]);

                        if (arabicNumeral2 == 0)
                        {
                            return(Response <decimal> .Fail("Number not exists"));
                        }

                        if (arabicNumeral1 >= arabicNumeral2)
                        {
                            result += arabicNumeral1;
                        }
                        else
                        {
                            result = result + arabicNumeral2 - arabicNumeral1;
                            i++;
                        }
                    }
                    else
                    {
                        result += arabicNumeral1;
                        i++;
                    }
                }

                return(Response <decimal> .Ok(result));
            }
            catch (Exception)
            {
                //TODO: Fazer LOG da Exception
                throw;
            }
        }
Ejemplo n.º 6
0
        public bool IsTranslationUnique(TranslationEntity expressionToCheck)
        {
            bool res;

            Log.StartTiming();
            using (var context = GetContext())
            {
                res = context.IsTranslationUnique(expressionToCheck);
            }
            Log.StopTiming();

            return(res);
        }
            public static TranslationResponseModel SendTranslation(TranslationEntity translation)
            {
                TranslationResponseModel response = new TranslationResponseModel()
                {
                    English    = translation.English,
                    German     = translation.German,
                    GroupId    = translation.GroupId,
                    Messages   = new string[] { "Translation returned" },
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success    = true
                };

                return(response);
            }
Ejemplo n.º 8
0
        private static void ClearStatistics(TranslationEntity e)
        {
            e.BadAnswers        = 0;
            e.GoodAnswers       = 0;
            e.WasLastAnswerGood = true;

            e.SpellingBadAnswers        = 0;
            e.SpellingGoodAnswers       = 0;
            e.SpellingWasLastAnswerGood = true;

            e.ReverseBadAnswers        = 0;
            e.ReverseGoodAnswers       = 0;
            e.ReverseWasLastAnswerGood = true;

            e.ReverseSpellingBadAnswers        = 0;
            e.ReverseSpellingGoodAnswers       = 0;
            e.ReverseSpellingWasLastAnswerGood = true;
        }
Ejemplo n.º 9
0
        private static void CopyStatistics(TranslationEntity source, TranslationEntity dest)
        {
            dest.BadAnswers        = source.BadAnswers;
            dest.GoodAnswers       = source.GoodAnswers;
            dest.WasLastAnswerGood = source.WasLastAnswerGood;

            dest.SpellingBadAnswers        = source.SpellingBadAnswers;
            dest.SpellingGoodAnswers       = source.SpellingGoodAnswers;
            dest.SpellingWasLastAnswerGood = source.SpellingWasLastAnswerGood;

            dest.ReverseBadAnswers        = source.ReverseBadAnswers;
            dest.ReverseGoodAnswers       = source.ReverseGoodAnswers;
            dest.ReverseWasLastAnswerGood = source.ReverseWasLastAnswerGood;

            dest.ReverseSpellingBadAnswers        = source.ReverseSpellingBadAnswers;
            dest.ReverseSpellingGoodAnswers       = source.ReverseSpellingGoodAnswers;
            dest.ReverseSpellingWasLastAnswerGood = source.ReverseSpellingWasLastAnswerGood;
        }
Ejemplo n.º 10
0
        HelperNormaliseTransliteSection(List <IRowItem> listRowItems)
        {
            Dictionary <ITranslationEntity, List <ITranslationEntity> > sections =
                new Dictionary <ITranslationEntity, List <ITranslationEntity> >();

            IDataResult <Dictionary <ITranslationEntity, List <ITranslationEntity> > > dataResult =
                new DataResult <Dictionary <ITranslationEntity, List <ITranslationEntity> > >();

            foreach (var rowItem in listRowItems)
            {
                List <ITranslationEntity> translationEntities = new List <ITranslationEntity>();
                ITranslationEntity        translationKey      = new TranslationEntity();

                foreach (var columnItem in rowItem.ColumnItems)
                {
                    ITranslationEntity entity = columnItem.BaseEntity as ITranslationEntity;

                    switch (columnItem.ColumnType)
                    {
                    case ColumnType.Section:

                        if (entity != null)
                        {
                            translationKey.Value    = entity.Value;
                            translationKey.Language = entity.Language;
                        }
                        break;

                    case ColumnType.SectionTransfer:
                        ITranslationEntity tmpEntity = new TranslationEntity();
                        tmpEntity.Language = entity.Language;
                        tmpEntity.Value    = entity.Value;
                        translationEntities.Add(tmpEntity);
                        break;
                    }
                }

                sections.Add(translationKey, translationEntities);
            }
            dataResult.Success = true;
            dataResult.Data    = sections;

            return(dataResult);
        }
Ejemplo n.º 11
0
        public static string GetTranslation(SECTION sectionID, int translationID)
        {
            string retVal = "";
            List <TranslationEntity> model = new List <TranslationEntity>();

            model = GetTranslationSection(sectionID);
            if (model != null)
            {
                TranslationEntity myQuery = model.Find(x => x.TranslationID.Equals(translationID));
                if (myQuery != null)
                {
                    retVal = myQuery.TranslationText;
                    if (_debug)
                    {
                        retVal = retVal + _method;
                    }
                }
            }
            return(retVal);
        }
Ejemplo n.º 12
0
        private IBaseEntity GetBaseEntity(IExcelWorksheetEntity excelWorksheet,
                                          string data, int column, ExcelConfiguration excelConfiguration)
        {
            IBaseEntity baseEntity = new BaseEntity()
            {
                Value = data
            };

            if (column == excelConfiguration.DataColumn
                .Datas.FirstOrDefault(p => p.ColumnType == (int)ColumnType.Section)?.Nomer ||
                column == excelConfiguration.DataColumn.Datas
                .FirstOrDefault(p => p.ColumnType == (int)ColumnType.Language)?.Nomer)
            {
                string             name   = excelConfiguration.NameColumnSection.MainLanguage;
                ITranslationEntity entity = new TranslationEntity();
                entity.Language = LanguageHolder.GetISOCodes(name, _dataNormalization);
                entity.Value    = data;

                return(entity);
            }

            if (column == excelConfiguration.DataColumn.Datas
                .FirstOrDefault(p => p.ColumnType == (int)ColumnType.Picture)?.Nomer)
            {
                IDataResult <string> colorNameResult =
                    _readExcelData.GetColorValue(excelWorksheet);
                IExcelColor excelColor = new ExcelColor();
                if (colorNameResult.Success)
                {
                    Color color = ColorTranslator.FromHtml("#" + colorNameResult.Data);
                    excelColor.R = color.R;
                    excelColor.G = color.G;
                    excelColor.B = color.B;
                }

                return(excelColor);
            }

            return(baseEntity);
        }
Ejemplo n.º 13
0
        public IDataResult <IColumnItem> ColumnParser
            (IExcelWorksheetEntity excelWorksheet, ExcelConfiguration excelConfiguration)
        {
            IDataResult <IColumnItem> dataResult =
                new DataResult <IColumnItem>()
            {
                Success = false
            };

            IColumnItem          columnItem  = new ColumnItem();
            int                  column      = excelWorksheet.CellNo;
            IDataResult <string> resultValue = _readExcelData.GetValue(excelWorksheet);
            int                  nomertitle  = excelConfiguration.DataRowIndex.Title;

            IExcelWorksheetEntity tmpExcel = new ExcelWorksheetEntity();

            tmpExcel.RowNo          = nomertitle;
            tmpExcel.ExcelWorksheet = excelWorksheet.ExcelWorksheet;
            tmpExcel.CellNo         = column;
            string nameTitle         = _readExcelData.GetValue(tmpExcel).Data;
            string configNameSection = excelConfiguration.DataColumn.Datas
                                       .FirstOrDefault(p => p.ColumnType == (int)ColumnType.Section)?.Name;

            configNameSection     = _dataNormalization.NormalizeString(configNameSection).Data;
            columnItem.BaseEntity = new BaseEntity();
            bool isResult = false;

            foreach (var data in excelConfiguration.DataColumn.Datas)
            {
                if (column == data.Nomer)
                {
                    if (!resultValue.Success)
                    {
                        if (data.MustExist)
                        {
                            dataResult.Message += resultValue.Message;
                        }
                    }

                    columnItem.ColumnType = (ColumnType)data.ColumnType;
                    columnItem.BaseEntity = GetBaseEntity
                                                (excelWorksheet, resultValue.Data, column, excelConfiguration);
                    isResult = true;
                }
            }
            if (!isResult)
            {
                nameTitle = _dataNormalization.NormalizeString(nameTitle).Data;


                if (nameTitle == null)
                {
                    //TODO
                    dataResult.Success = true;

                    return(dataResult);
                }

                if (nameTitle.Contains(configNameSection))
                {
                    columnItem.ColumnType = ColumnType.SectionTransfer;
                    nameTitle             = nameTitle.Replace(configNameSection, string.Empty);
                }
                else
                {
                    columnItem.ColumnType = ColumnType.WorldSection;
                }
                dataResult.Message += resultValue.Message;
                string language = LanguageHolder.GetISOCodes(nameTitle, _dataNormalization);

                ITranslationEntity entity = new TranslationEntity();
                entity.Language       = language;
                entity.Value          = resultValue.Data;
                columnItem.BaseEntity = entity;
            }

            columnItem.ColumNumber = column;
            dataResult.Data        = columnItem;
            dataResult.Success     = true;


            return(dataResult);
        }
Ejemplo n.º 14
0
        public bool IsTranslationUnique(TranslationEntity translationToCheck)
        {
            translationToCheck.NotNull("translationToCheck");

            return(Translations.Any(t => t.Translation == translationToCheck.Translation));
        }
Ejemplo n.º 15
0
        private int SetArabicNumeral(TranslationEntity translete, string number)
        {
            translete.TranslateKwegonian(number);

            return(translete.ArabicNumeral == null ? 0 : Convert.ToInt32(translete.ArabicNumeral));
        }
Ejemplo n.º 16
0
        //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

        private static IEnumerable <IDictionary <string, object> > GetExistingTranslations <TEntity, TTranslatedEntity>(this DbContext context, TEntity entity,
                                                                                                                        TranslationEntity translationEntity, Translation <TEntity>[] translationEntities)
            where TEntity : class
            where TTranslatedEntity : class
        {
            var schema = !string.IsNullOrWhiteSpace(translationEntity.Schema) ? $"[{translationEntity.Schema}]." : string.Empty;

            var query = new StringBuilder();

            query.Append("SELECT ");
            query.Append(string.Join(" ,", context.Model.FindEntityType(translationEntity.Type).GetProperties().Select(property => $"[t].[{property.GetColumnName()}]")));
            query.Append($" FROM {schema}[{translationEntity.TableName}] AS [t]");
            query.Append(" WHERE ");
            query.Append(string.Join(" AND ", translationEntity.KeysFromSourceEntity
                                     .Select(property => $"[t].[{property.Value}] = @{property.Value}")));
            query.Append(" AND (");
            query.Append(string.Join(" OR ", translationEntities
                                     .Select((translation, index) => string.Join(" ,", translationEntity.KeysFromLanguageEntity
                                                                                 .Select(key => $"[t].[{key.Name}] = @{key.Name}{index}")))));
            query.Append(" );");

            using (var command = context.Database.GetDbConnection().CreateCommand())
            {
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
                command.CommandText = query.ToString();
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

                foreach (var parameter in translationEntity.KeysFromSourceEntity
                         .Select(property => (Name: property.Value, Value: entity.GetType().GetProperty(property.Key).GetValue(entity))))
                {
                    command.AddParameterWithValue(parameter.Name, parameter.Value);
                }

                foreach (var parameter in translationEntities.Select((translation, entityIndex) => translationEntity.KeysFromLanguageEntity
                                                                     .Select((key, keyIndex) => (Name: $"{key.Name}{entityIndex}", Value: translation.LanguageKey[keyIndex])))
                         .SelectMany(tuple => tuple))
                {
                    command.AddParameterWithValue(parameter.Name, parameter.Value);
                }

                context.Database.OpenConnection();
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        yield return(SqlDataReaderToExpando(reader));
                    }
                }
            }
        }
Ejemplo n.º 17
0
        public IDataResult <IColumnItem> ColumnParser
            (IExcelWorksheetEntity worksheetEntity, ExcelConfiguration excelConfiguration)
        {
            IDataResult <IColumnItem> dataResult =
                new DataResult <IColumnItem>()
            {
                Success = false
            };

            IColumnItem          columnItem  = new ColumnItem();
            int                  column      = worksheetEntity.CellNo;
            IDataResult <string> resultValue = _readExcelData.GetValue(worksheetEntity);
            int                  nomertitle  = excelConfiguration.DataRowIndex.Title;

            if (!resultValue.Success)
            {
                dataResult.Message = resultValue.Message;
            }

            IExcelWorksheetEntity tmpExcel = new ExcelWorksheetEntity();

            tmpExcel.RowNo          = nomertitle;
            tmpExcel.ExcelWorksheet = worksheetEntity.ExcelWorksheet;
            tmpExcel.CellNo         = column;

            IDataResult <string> resultNameTitle = _readExcelData.GetValue(tmpExcel);

            if (!resultNameTitle.Success)
            {
                dataResult.Message = MessageHolder.GetErrorMessage(MessageType.NotNameTitle);
            }

            string nameTitle = resultNameTitle.Data;

            nameTitle = _dataNormalization.NormalizeString(nameTitle).Data;

            string titleConfig = excelConfiguration.DataColumn.Datas?
                                 .FirstOrDefault(p => p.ColumnType == (int)ColumnType.Section)?.Name;

            titleConfig = _dataNormalization.NormalizeString(titleConfig).Data;
            ITranslationEntity translationEntity = new TranslationEntity();

            translationEntity.Value = resultValue.Data;
            string mainLanguage = excelConfiguration.NameColumnSection.MainLanguage;

            mainLanguage = _dataNormalization.NormalizeString(mainLanguage).Data;
            string excelLanguage = null;

            if (titleConfig != null)
            {
                excelLanguage = nameTitle.Replace(titleConfig, string.Empty);
            }


            if (nameTitle.Equals(titleConfig) || (excelLanguage != null && excelLanguage.Equals(mainLanguage)))
            {
                columnItem.ColumnType      = ColumnType.Section;
                translationEntity.Language =
                    LanguageHolder.GetISOCodes(mainLanguage, _dataNormalization);

                if (!resultValue.Success)
                {
                    dataResult.Message = resultValue.Message;

                    return(dataResult);
                }

                columnItem.BaseEntity = translationEntity;
            }
            else
            {
                columnItem.ColumnType = ColumnType.SectionTransfer;

                translationEntity.Language =
                    LanguageHolder.GetISOCodes(excelLanguage, _dataNormalization);

                if (!resultValue.Success)
                {
                    dataResult.Message = resultValue.Message;

                    return(dataResult);
                }

                columnItem.BaseEntity = translationEntity;
            }

            dataResult.Data    = columnItem;
            dataResult.Success = true;

            return(dataResult);
        }
Ejemplo n.º 18
0
        private Result Validate(TranslationEntity translation)
        {
            var placeholder = translation?.Placeholder?.Name;

            return(IsPlaceholderValid(placeholder) ? Result.Ok() : Result.Fail($"Placeholder '{placeholder}' has invalid format or contains forbidden characters"));
        }
Ejemplo n.º 19
0
        //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

        private static void ConfigureKeys(this EntityTypeBuilder builder, IMutableEntityType entity, IMutableEntityType languageBuilder, TranslationEntity configuration)
        {
            var primaryKeys = new List <string>();

            //Source Table
            foreach (var key in entity.GetProperties().Where(p => p.IsPrimaryKey()))
            {
                string name = $"{entity.ClrType.Name}{key.GetColumnName()}";

                builder.Property(key.ClrType, name)
                .HasColumnType(key.GetColumnType());

                primaryKeys.Add(name);
                configuration.KeysFromSourceEntity.Add(key.GetColumnName(), name);
            }

            builder.HasOne(entity.ClrType)
            .WithMany()
            .HasForeignKey(primaryKeys.ToArray())
            .OnDelete(configuration.DeleteBehavior);

            //Language Table
            if (languageBuilder == null)
            {
                foreach (var key in TranslationConfiguration.LanguageTableConfiguration.PrimaryKey)
                {
                    builder.Property(key.Type, key.Name);

                    primaryKeys.Add(key.Name);
                    configuration.KeysFromLanguageEntity.Add(new KeyConfiguration(key.Type, key.Name));
                }
            }
            else
            {
                var foreignKeys = new List <KeyConfiguration>();

                foreach (var property in languageBuilder.GetProperties().Where(p => p.IsPrimaryKey()))
                {
                    string name = $"{languageBuilder.ClrType.Name}{property.GetColumnName()}";

                    builder.Property(property.ClrType, name)
                    .HasColumnType(property.GetColumnType());

                    primaryKeys.Add(name);
                    foreignKeys.Add(new KeyConfiguration(property.ClrType, name));
                    configuration.KeysFromLanguageEntity.Add(new KeyConfiguration(property.ClrType, name));
                }

                builder.HasOne(languageBuilder.ClrType)
                .WithMany()
                .HasForeignKey(foreignKeys.Select(fk => fk.Name).ToArray())
                .OnDelete(configuration.DeleteBehavior);

                TranslationConfiguration.LanguageTableConfiguration = new LanguageTableConfiguration(foreignKeys);
            }

            builder.HasKey(primaryKeys.ToArray());
        }