Example #1
0
        public async Task <IActionResult> PostSportLanguage(int id, [FromBody] SportLanguage sportLanguage)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var sport = await context.Sport.Include(s => s.Languages).SingleOrDefaultAsync(s => s.Id == id);

            if (sport == null)
            {
                return(NotFound());
            }

            sport.AddLanguage(sportLanguage);
            context.SportLanguage.Add(sportLanguage);
            await context.SaveChangesAsync();

            return(CreatedAtAction("PostSportLanguage", new { id = sportLanguage.Id }, sportLanguage));
        }
Example #2
0
        private void ProcessSport(Sport sport)
        {
            // If the language does not exist and machine translator exit, use it to translate the sport to that language.
            var targetSportLanguage = sport.Languages.Find(m => m.Language == Language);

            if ((targetSportLanguage == null) && (machineTranslator != null) && (sport.Languages.Count > 0))
            {
                // Find the English sport to be used as the original language.
                var originalSportLanguage = sport.Languages.Find(m => m.Language == "en");

                // If not found take the first sport.
                if (originalSportLanguage == null)
                {
                    originalSportLanguage = sport.Languages[0];
                }

                // Create a new language specific sport using the machine translated properties
                targetSportLanguage = new SportLanguage
                {
                    Language          = Language,
                    MachineTranslated = true,
                    Name        = machineTranslator.Translate(originalSportLanguage.Name, originalSportLanguage.Language, Language),
                    Origin      = machineTranslator.Translate(originalSportLanguage.Origin, originalSportLanguage.Language, Language),
                    Description = machineTranslator.Translate(originalSportLanguage.Description, originalSportLanguage.Language, Language)
                };

                // If the machine translation succeeded add the sport language to the list and sav it to the DB
                if (!string.IsNullOrEmpty(targetSportLanguage.Name) && !string.IsNullOrEmpty(targetSportLanguage.Origin) && !string.IsNullOrEmpty(targetSportLanguage.Description))
                {
                    sport.AddLanguage(targetSportLanguage);
                    context.SaveChanges();
                }
            }

            // Move the active language to the first in the list
            sport.MoveDefaultLanguateToTop(Language);
        }