Ejemplo n.º 1
0
        internal List <StringTranslation> GetTranslationsForCulture(string cultureCode, bool includeTranslated,
                                                                    string assemblyFileName = "")
        {
            DataTable originalString;

            if (assemblyFileName == "")
            {
                originalString = GetTranslationsDataTableForCulture(cultureCode);
            }
            else
            {
                originalString = __DB.SelectAll(@"SELECT string_ids.id, string_ids.filename, 
                                                         string_ids.baml, string_ids.max_revision, 
                                                         string_ids.uid, string_ids.category, 
                                                         string_ids.readability, string_ids.localizability,
                                                         string_ids.description,
                                                         original_strings.string, 
                                                                (SELECT translation FROM translated_strings WHERE string_id=string_ids.id 
                                                                                                              AND revision=string_ids.max_revision 
                                                                                                              AND culture_code=@cultureCode) as translation
                                                          FROM string_ids, original_strings 
                                                          WHERE string_ids.id = original_strings.string_id 
                                                            AND string_ids.filename = @assemblyFileName
                                                            AND original_strings.is_deleted = 0 
                                                            AND original_strings.revision = string_ids.max_revision 
                                                            AND string <> ''",
                                                new Dictionary <string, object>()
                {
                    { "@cultureCode", cultureCode },
                    { "@assemblyFileName", assemblyFileName.ToLower() }
                });
            }


            List <StringTranslation> result = new List <StringTranslation>();

            foreach (DataRow row in originalString.Rows)
            {
                StringRow stringRow = new StringRow();
                stringRow.Sid            = (int)UnboxNumeric(row["id"]);
                stringRow.DllFile        = (string)row["filename"];
                stringRow.BamlFile       = (string)row["baml"];
                stringRow.ResourceKey    = (string)row["uid"];
                stringRow.Localizability = (string)row["localizability"];
                stringRow.Readability    = (string)row["readability"];
                stringRow.Category       = (string)row["category"];
                stringRow.Comments       = (string)row["description"];
                stringRow.Content        = (string)row["string"];
                stringRow.MaxRevision    = (int)UnboxNumeric(row["max_revision"]);

                string translation = null;
                if (!(row["translation"] is DBNull))
                {
                    translation = (string)row["translation"];
                }

                StringTranslation translationRow = new StringTranslation(stringRow, translation);

                if (!String.IsNullOrEmpty(translation) && includeTranslated)
                {
                    result.Add(translationRow);
                }

                if (String.IsNullOrEmpty(translation))
                {
                    result.Add(translationRow);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ImportTranslationResult> ImportTranslation(string fileName, string cultureCode)
        {
            ImportTranslationResult result = new ImportTranslationResult(fileName, cultureCode);

            FileStream fileStream;

            try
            {
                fileStream = new FileStream(fileName, FileMode.Open);
            }
            catch (Exception e)
            {
                result.ErrorMessage = e.Message;
                return(result);
            }

            await Task.Run(() =>
            {
                List <StringTranslation> originalAndTranslations = __DB.GetTranslationsForCulture(cultureCode, true);

                CsvReader reader = new CsvReader(fileStream, Encoding.UTF8);
                var headerRecord = reader.ReadHeaderRecord();

                var rows       = reader.ReadDataRecords();
                result.Records = rows;

                List <int> allSids        = new List <int>();
                List <int> sidsUpdated    = new List <int>();
                List <int> sidsWithErrors = new List <int>();

                __DB.BeginTransaction();
                try
                {
                    foreach (DataRecord row in rows)
                    {
                        int sid            = int.Parse(row[0]);
                        string original    = row[1];
                        string translation = row[2];

                        allSids.Add(sid);

                        StringTranslation stringRow = originalAndTranslations.Find(x => x.Row.Sid == sid);

                        if (stringRow == null)
                        {
                            sidsWithErrors.Add(sid);
                            result.Log.Add(new ImportTranslationResultLogLine(sid, "",
                                                                              original, translation, ImportTranslationStringErrorReason.StringNotFound));
                        }
                        else
                        if (stringRow.Row.Content != original)
                        {
                            sidsWithErrors.Add(sid);
                            result.Log.Add(new ImportTranslationResultLogLine(sid, stringRow.Row.Content,
                                                                              original, translation, ImportTranslationStringErrorReason.OriginalStringChanged));
                        }
                        else
                        if (stringRow.Translation == translation ||
                            (String.IsNullOrEmpty(stringRow.Translation) && String.IsNullOrEmpty(translation)))
                        {
                            // now do nothing
                        }
                        else
                        {
                            __DB.SetTranslation(sid, stringRow.Row.MaxRevision, cultureCode, stringRow.Translation, translation);
                            sidsUpdated.Add(sid);
                        }
                    }

                    __DB.Commit();
                }
                catch
                {
                    __DB.Rollback();
                    throw;
                }

                result.AllSids        = allSids;
                result.SidsUpdated    = sidsUpdated;
                result.SidsWithErrors = sidsWithErrors;
            }
                           );

            return(result);
        }