public ActionResult TranslatorForm(int id, string containerName = "", string containerType = "", string language = "")
        {
            TranslationRecord messageRecord = _translatorServices.GetTranslations().Where(m => m.Id == id).FirstOrDefault();

            if (messageRecord != null)
            {
                ViewBag.SuggestedTranslations = _translatorServices.GetSuggestedTranslations(messageRecord.Message, messageRecord.Language);
                var viewModel = new TranslationRecordViewModel(messageRecord)
                {
                    CultureList = _translatorServices.GetCultureList()
                };
                return(View(viewModel));
            }
            else
            {
                var model = new TranslationRecord {
                    ContainerName = containerName,
                    ContainerType = containerType,
                    Language      = language
                };

                var viewModel = new TranslationRecordViewModel(model)
                {
                    CultureList = _translatorServices.GetCultureList()
                };

                return(View(viewModel));
            }
        }
示例#2
0
        public void UpdateTranslation(int id, string culture, string value)
        {
            var session     = _sessionLocator.For(typeof(LocalizableStringRecord));
            var localizable = session.Get <LocalizableStringRecord>(id);
            var translation = localizable.Translations.Where(t => t.Culture == culture).FirstOrDefault();

            if (translation == null)
            {
                if (!string.IsNullOrEmpty(value))
                {
                    var newTranslation = new TranslationRecord {
                        Culture = culture,
                        Value   = value,
                        LocalizableStringRecord = localizable
                    };
                    localizable.Translations.Add(newTranslation);
                    session.SaveOrUpdate(newTranslation);
                    session.SaveOrUpdate(localizable);
                    SetCacheInvalid();
                }
            }
            else if (string.IsNullOrEmpty(value))
            {
                session.Delete(translation);
                SetCacheInvalid();
            }
            else if (translation.Value != value)
            {
                translation.Value = value;
                session.SaveOrUpdate(translation);
                SetCacheInvalid();
            }
        }
示例#3
0
        async Task <int> SetTransRecord(string japanese, string fictional)
        {
            if (japanese == fictional)
            {
                return(0);
            }
            if (textDecomposer.IsSpecialChars(japanese))
            {
                return(0);
            }

            var exsistJapaneseQuery  = context.records.Where(record => record.japanese == japanese);
            var exsistFictionalQuery = context.records.Where(record => record.fictional == fictional);

            context.records.RemoveRange(exsistJapaneseQuery);
            context.records.RemoveRange(exsistFictionalQuery);

            var newRecord = new TranslationRecord {
                japanese = japanese, fictional = fictional
            };

            context.records.Add(newRecord);

            return(await context.SaveChangesAsync());
        }
示例#4
0
        public void UpdateTranslation(int id, string culture, string value)
        {
            var localizable = _localizableStringRepository.Get(id);
            var translation = localizable.Translations.Where(t => t.Culture == culture).FirstOrDefault();

            if (translation == null)
            {
                if (!String.IsNullOrEmpty(value))
                {
                    var newTranslation = new TranslationRecord
                    {
                        Culture = culture,
                        Value   = value,
                        LocalizableStringRecord = localizable
                    };
                    localizable.Translations.Add(newTranslation);

                    _translationRepository.Create(newTranslation);
                    SetCacheInvalid();
                }
            }
            else if (String.IsNullOrEmpty(value))
            {
                _translationRepository.Delete(translation);
                SetCacheInvalid();
            }
            else if (String.Compare(translation.Value, value) != 0)
            {
                translation.Value = value;
                _translationRepository.Update(translation);
                SetCacheInvalid();
            }
        }
        public ActionResult SaveTranslation(TranslationRecordViewModel translationVM)
        {
            TranslationRecord translation = translationVM.ToTranslationRecord();
            // I need to check if parent page needs to be refreshed.
            // If I'm creating a new record, I have to refresh parent page.
            // I also need to refresh parent page if I changed the ContainerType of my record.
            // I also need to refresh parent page if I changed the Language of my record.
            bool refreshParent = (translationVM.Id == 0 || !translation.ContainerType.Equals(translationVM.ContainerType, System.StringComparison.InvariantCulture) || !translationVM.OriginalLanguage.Equals(translationVM.Language, System.StringComparison.InvariantCulture));

            // If I'm saving a new translation, I must not overwrite an existing matching translation.
            bool success = _translatorServices.TryAddOrUpdateTranslation(translation, translation.Id == 0 ? false : true);

            ViewBag.SuggestedTranslations = _translatorServices.GetSuggestedTranslations(translation.Message, translation.Language);

            if (!success)
            {
                ModelState.AddModelError("SaveTranslationError", T("An error occurred while saving the translation. Please reload the page and retry.").ToString());
                ViewBag.RefreshParent = false;
                ViewBag.SaveSuccess   = false;
            }
            else if (refreshParent)
            {
                ViewBag.RefreshParent = true;
                ViewBag.SaveSuccess   = false;
            }
            else
            {
                ViewBag.RefreshParent = false;
                ViewBag.SaveSuccess   = true;
            }

            translationVM.CultureList = _translatorServices.GetCultureList();

            return(View(translationVM));
        }
示例#6
0
        /// <summary>
        /// Saves to database. If culture is present, Translation entry is saved
        /// </summary>
        /// <param name="session"></param>
        /// <param name="input"></param>
        private void SaveStringToDatabase(ISession session, StringEntry input, bool overwrite)
        {
            var translatableString =
                (from s in session.Linq <LocalizableStringRecord>()
                 where s.StringKey == input.Key &&
                 s.Context == input.Context
                 select s).FirstOrDefault();

            if (translatableString == null)
            {
                string path = input.Path;
                if (!path.Contains("{0}") && !string.IsNullOrEmpty(input.Culture))
                {
                    path = path.Replace(input.Culture, "{0}");
                }
                translatableString = new LocalizableStringRecord
                {
                    Path      = path,
                    Context   = input.Context,
                    StringKey = input.Key,
                    OriginalLanguageString = input.English
                };
                if (!translatableString.Path.Contains("{0}"))
                {
                    throw new Exception("Path should contain {0}, but doesn't.\n" + translatableString.Path);
                }
                session.SaveOrUpdate(translatableString);
            }
            else if (translatableString.OriginalLanguageString != input.English)
            {
                translatableString.OriginalLanguageString = input.English;
                session.SaveOrUpdate(translatableString);
            }

            if (!string.IsNullOrEmpty(input.Culture) && !string.IsNullOrEmpty(input.Translation))
            {
                var translation =
                    (from t in translatableString.Translations
                     where t.Culture.Equals(input.Culture)
                     select t).FirstOrDefault();

                if (translation == null)
                {
                    translation = new TranslationRecord
                    {
                        Culture = input.Culture,
                        Value   = input.Translation
                    };
                    translatableString.AddTranslation(translation);
                }
                else if (overwrite)
                {
                    translation.Value = input.Translation;
                }
                session.SaveOrUpdate(translatableString);
                session.SaveOrUpdate(translation);
            }

            SetCacheInvalid();
        }
        private void ImportFromPO(List <string> foldersToImport, ElementToTranslate type)
        {
            string parentFolder = "";
            string fileName     = "";

            if (type == ElementToTranslate.Module)
            {
                parentFolder = "Modules";
                fileName     = "orchard.module.po";
            }
            else if (type == ElementToTranslate.Theme)
            {
                parentFolder = "Themes";
                fileName     = "orchard.theme.po";
            }
            else
            {
                return;
            }

            foreach (var folder in foldersToImport)
            {
                var path = Path.Combine(_utilsServices.TenantPath, parentFolder, folder, "App_Data", "Localization");
                if (Directory.Exists(path))
                {
                    var languages = Directory.GetDirectories(path).Select(d => new DirectoryInfo(d).Name);
                    foreach (var language in languages)
                    {
                        var filePath = Path.Combine(path, language, fileName);
                        if (System.IO.File.Exists(filePath))
                        {
                            string fileContent = System.IO.File.ReadAllText(filePath);
                            foreach (Match match in Regex.Matches(fileContent, pattern, RegexOptions.IgnoreCase))
                            {
                                TranslationRecord translation = new TranslationRecord();

                                translation.ContainerName = folder;

                                if (type == ElementToTranslate.Module)
                                {
                                    translation.ContainerType = "M";
                                }
                                else if (type == ElementToTranslate.Theme)
                                {
                                    translation.ContainerType = "T";
                                }

                                translation.Context           = match.Groups[1].Value;
                                translation.Message           = match.Groups[2].Value;
                                translation.TranslatedMessage = match.Groups[3].Value;
                                translation.Language          = language;

                                _translatorServices.TryAddOrUpdateTranslation(translation);
                            }
                        }
                    }
                }
            }
        }
示例#8
0
 public bool DeleteTranslation(TranslationRecord record)
 {
     try {
         _translationRecordRepository.Delete(record);
         return(true);
     } catch (Exception) {
         return(false);
     }
 }
示例#9
0
        public async void makeInsertCall()
        {
            TranslationRecord record = new TranslationRecord();

            record.character = PhonewordText.Text;
            record.number    = translatedNumber;
            var response = await apiService.AddTranslation(record);

            System.Diagnostics.Debug.Write(response.affectedRows);
        }
示例#10
0
 public bool TryAddOrUpdateTranslation(TranslationRecord translation)
 {
     try {
         AddOrUpdateTranslation(translation);
         return(true);
     } catch (Exception ex) {
         Log.Error(ex, "TranslatorServices.TryAddOrUpdateTranslation error.");
         return(false);
     }
 }
 public TranslationRecordViewModel(TranslationRecord tr)
 {
     Id                   = tr.Id;
     ContainerName        = tr.ContainerName;
     ContainerType        = tr.ContainerType;
     ContainerTypeAndName = tr.ContainerType + tr.ContainerName;
     Context              = tr.Context;
     Message              = tr.Message;
     TranslatedMessage    = tr.TranslatedMessage;
     Language             = tr.Language;
     OriginalLanguage     = tr.Language;
 }
        public TranslationRecord ToTranslationRecord()
        {
            TranslationRecord tr = new TranslationRecord {
                Id                = Id,
                ContainerName     = ContainerTypeAndName.Substring(1),
                ContainerType     = ContainerTypeAndName.Substring(0, 1),
                Context           = Context,
                Message           = Message,
                TranslatedMessage = TranslatedMessage,
                Language          = Language
            };

            return(tr);
        }
示例#13
0
        public ActionResult TranslatorForm(int id)
        {
            TranslationRecord messageRecord = _translatorServices.GetTranslations().Where(m => m.Id == id).FirstOrDefault();

            if (messageRecord != null)
            {
                ViewBag.SuggestedTranslations = _translatorServices.GetSuggestedTranslations(messageRecord.Message, messageRecord.Language);
                return(View(messageRecord));
            }
            else
            {
                return(View(new TranslationRecord()));
            }
        }
示例#14
0
        private void AddOrUpdateTranslation(TranslationRecord translation)
        {
            List <TranslationRecord> existingTranslations = new List <TranslationRecord>();
            bool searchById = translation.Id != 0;

            if (translation.TranslatedMessage != null)
            {
                translation.TranslatedMessage = translation.TranslatedMessage.Trim();
            }

            if (searchById)
            {
                existingTranslations = GetTranslations().Where(t => t.Id == translation.Id).ToList();
            }
            else
            {
                existingTranslations = GetTranslations().Where(t => t.Language == translation.Language &&
                                                               t.ContainerName == translation.ContainerName &&
                                                               t.ContainerType == translation.ContainerType &&
                                                               t.Context == translation.Context &&
                                                               t.Message == translation.Message).ToList();
            }

            if (existingTranslations.Any())
            {
                TranslationRecord existingTranslation = existingTranslations.FirstOrDefault();

                existingTranslation.Context           = translation.Context;
                existingTranslation.TranslatedMessage = translation.TranslatedMessage;
                existingTranslation.Message           = translation.Message; // #GM 2015-09-22

                _translationRecordRepository.Update(existingTranslation);
                _translationRecordRepository.Flush();
            }
            else
            {
                if (searchById)
                {
                    throw new Exception(T("The requested translation does not exists.").ToString());
                }
                else
                {
                    _translationRecordRepository.Create(translation);
                    _translationRecordRepository.Flush();
                }
            }
        }
示例#15
0
        public ActionResult SaveTranslation(TranslationRecord translation)
        {
            bool success = _translatorServices.TryAddOrUpdateTranslation(translation);

            ViewBag.SuggestedTranslations = _translatorServices.GetSuggestedTranslations(translation.Message, translation.Language);

            if (!success)
            {
                ModelState.AddModelError("SaveTranslationError", T("An error occurred while saving the translation. Please reload the page and retry.").ToString());
                ViewBag.SaveSuccess = false;
            }
            else
            {
                ViewBag.SaveSuccess = true;
            }

            return(View(translation));
        }
示例#16
0
        public ActionResult DeleteTranslation(int id)
        {
            TranslationRecord messageRecord = _translatorServices.GetTranslations().Where(m => m.Id == id).FirstOrDefault();

            bool success = _translatorServices.DeleteTranslation(messageRecord);

            if (!success)
            {
                ModelState.AddModelError("DeleteTranslationError", T("Unable to delete the translation.").ToString());
                ViewBag.DeleteSuccess = false;
                return(View(messageRecord));
            }
            else
            {
                ViewBag.DeleteSuccess = true;
                return(View(new TranslationRecord {
                    Id = 0
                }));
            }
        }
示例#17
0
        private async void TranslateButton_Click(object sender, System.EventArgs e)
        {
            TranslatedNumber = PhonewordTranslator.ToNumber(phoneNumberText.Text);
            if (string.IsNullOrWhiteSpace(TranslatedNumber))
            {
                callButton.Text    = "Call";
                callButton.Enabled = false;
            }
            else
            {
                callButton.Text    = "Call " + TranslatedNumber;
                callButton.Enabled = true;

                TranslationRecord record = new TranslationRecord();
                record.character = phoneNumberText.Text;
                record.number    = TranslatedNumber;
                var response = await apiService.AddTranslation(record);

                System.Diagnostics.Debug.Write(response.affectedRows);
            }
        }
        public ActionResult DeleteTranslation(int id)
        {
            TranslationRecord messageRecord = _translatorServices.GetTranslations().Where(m => m.Id == id).FirstOrDefault();

            bool success = _translatorServices.DeleteTranslation(messageRecord);

            if (!success)
            {
                ModelState.AddModelError("DeleteTranslationError", T("Unable to delete the translation.").ToString());
                ViewBag.RefreshParent = false;
                var viewModel = new TranslationRecordViewModel(messageRecord)
                {
                    CultureList = _translatorServices.GetCultureList()
                };
                return(View(viewModel));
            }
            else
            {
                ViewBag.RefreshParent = true;
                return(Content(T("The translation has been deleted.").Text));
            }
        }
        private void AddOrUpdateTranslation(TranslationRecord translation, bool forceUpdate = true)
        {
            //Validate the translations
            if (string.IsNullOrWhiteSpace(translation.ContainerName) ||
                string.IsNullOrWhiteSpace(translation.ContainerType) ||
                string.IsNullOrWhiteSpace(translation.Language) ||
                string.IsNullOrWhiteSpace(translation.Message))
            {
                throw new ArgumentNullException();
            }

            List <TranslationRecord> existingTranslations = new List <TranslationRecord>();
            bool searchById = translation.Id != 0;

            if (translation.TranslatedMessage != null)
            {
                translation.TranslatedMessage = translation.TranslatedMessage.Trim();
            }

            if (searchById)
            {
                existingTranslations = GetTranslations().Where(t => t.Id == translation.Id).ToList();
            }
            else
            {
                existingTranslations = GetTranslations().Where(t => t.Language == translation.Language &&
                                                               t.ContainerName == translation.ContainerName &&
                                                               t.ContainerType == translation.ContainerType &&
                                                               t.Context == translation.Context &&
                                                               t.Message == translation.Message).ToList();
            }

            var updateRecord = false;
            TranslationRecord existingTranslation = new TranslationRecord();

            if (existingTranslations.Any())
            {
                // nel caso in cui dall'api controller viene richiesto l'inserimento o l'aggiornamento
                // faccio un ulteriore verifica maiuscole/minuscole del message
                foreach (var item in existingTranslations)
                {
                    if (translation.Message.Equals(item.Message, StringComparison.InvariantCulture) ||
                        searchById)
                    {
                        if (forceUpdate || string.IsNullOrWhiteSpace(item.TranslatedMessage))
                        {
                            existingTranslation = item;
                            updateRecord        = true;
                        }
                        break;
                    }
                }
            }

            if (updateRecord && existingTranslation.Id > 0)
            {
                existingTranslation.Context           = translation.Context;
                existingTranslation.TranslatedMessage = translation.TranslatedMessage;
                existingTranslation.Message           = translation.Message; // #GM 2015-09-22

                existingTranslation.ContainerName = translation.ContainerName;
                existingTranslation.ContainerType = translation.ContainerType;

                _translationRecordRepository.Update(existingTranslation);
                _translationRecordRepository.Flush();
            }
            else
            {
                if (searchById)
                {
                    throw new Exception(T("The requested translation does not exists.").ToString());
                }
                else
                {
                    _translationRecordRepository.Create(translation);
                    _translationRecordRepository.Flush();
                }
            }
        }
示例#20
0
        private void ImportFromPO(List <string> foldersToImport, ElementToTranslate type)
        {
            string parentFolder  = "";
            string fileName      = "";
            string containerType = "";

            switch (type)
            {
            case ElementToTranslate.Module:
                parentFolder  = "Modules";
                fileName      = "orchard.module.po";
                containerType = "M";
                break;

            case ElementToTranslate.Theme:
                parentFolder  = "Themes";
                fileName      = "orchard.theme.po";
                containerType = "T";
                break;

            case ElementToTranslate.Tenant:
            // TODO: verify that the import feature for translations
            // works and figure out what should happen for alternates
            // that are specific for tenants
            //parentFolder = "Themes";
            //fileName = "orchard.po";
            //containerType = "A";
            //break;
            default:
                return;
            }


            foreach (var folder in foldersToImport)
            {
                var path = Path.Combine(_utilsServices.TenantPath, parentFolder, folder, "App_Data", "Localization");
                if (Directory.Exists(path))
                {
                    var languages = Directory.GetDirectories(path).Select(d => new DirectoryInfo(d).Name);
                    foreach (var language in languages)
                    {
                        var filePath = Path.Combine(path, language, fileName);
                        if (System.IO.File.Exists(filePath))
                        {
                            string fileContent = System.IO.File.ReadAllText(filePath);
                            foreach (Match match in Regex.Matches(fileContent, pattern, RegexOptions.IgnoreCase))
                            {
                                TranslationRecord translation = new TranslationRecord();

                                translation.ContainerName = folder;

                                translation.ContainerType = containerType;

                                translation.Context           = match.Groups[1].Value;
                                translation.Message           = match.Groups[2].Value;
                                translation.TranslatedMessage = match.Groups[3].Value;
                                translation.Language          = language;

                                _translatorServices.TryAddOrUpdateTranslation(translation);
                            }
                        }
                    }
                }
            }
        }