示例#1
0
        private static void InsertLabels(ILabelService labelService, Project project, string webRootPath)
        {
            var labelsFilePath = Path.Combine(webRootPath, "files", "projectTranslations.csv");

            if (!File.Exists(labelsFilePath))
            {
                return;
            }

            var labelListInfos = new List <LabelListInfo>();
            var lines          = File.ReadAllLines(labelsFilePath, Encoding.UTF8);

            for (var i = 1; i < lines.Length; i++)
            {
                var values = lines[i].Split(',');
                if (values.Length != 3)
                {
                    throw new Exception("projectTranslations.csv is not valid!");
                }

                labelListInfos.Add(new LabelListInfo
                {
                    LabelKey         = values[0],
                    LanguageIsoCode2 = values[1],
                    Translation      = values[2]
                });
            }

            var request  = new LabelCreateListRequest(project.CreatedBy, project.OrganizationUid, project.Uid, labelListInfos);
            var response = labelService.CreateLabelFromList(request).Result;

            if (response.Status.IsNotSuccess)
            {
                throw new Exception("couldn't add project translations!");
            }
        }
示例#2
0
        public async Task <IActionResult> UploadLabelFromCSVFile(LabelUploadFromCSVModel model)
        {
            if (model.IsNotValid())
            {
                model.SetInputModelValues();
                return(View(model));
            }

            var labelListInfos = new List <LabelListInfo>();

            var lines = new List <string>();

            using (var reader = new StreamReader(model.CSVFile.OpenReadStream()))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }

            for (var i = 1; i < lines.Count; i++)
            {
                var values = lines[i].Split(',');
                if (values.Length != 3)
                {
                    model.ErrorMessages.Add("file_has_more_columns_than_expected");
                    model.ErrorMessages.Add("error line : " + i);
                    model.SetInputModelValues();
                    return(View(model));
                }

                labelListInfos.Add(new LabelListInfo
                {
                    LabelKey         = values[0],
                    LanguageIsoCode2 = values[1],
                    Translation      = values[2]
                });
            }

            var request  = new LabelCreateListRequest(CurrentUser.Id, model.OrganizationUid, model.ProjectUid, labelListInfos);
            var response = await _labelService.CreateLabelFromList(request);

            if (response.Status.IsNotSuccess)
            {
                model.MapMessages(response);
                model.SetInputModelValues();
                return(View(model));
            }

            var doneModel = new LabelUploadFromCSVDoneModel();

            doneModel.MapMessages(response);
            doneModel.ProjectUid                       = model.ProjectUid;
            doneModel.ProjectName                      = model.ProjectName;
            doneModel.AddedLabelCount                  = response.AddedLabelCount;
            doneModel.CanNotAddedLabelCount            = response.CanNotAddedLabelCount;
            doneModel.AddedLabelTranslationCount       = response.AddedLabelTranslationCount;
            doneModel.CanNotAddedLabelTranslationCount = response.CanNotAddedLabelTranslationCount;
            doneModel.TotalRowsProcessed               = lines.Count - 1;

            CurrentUser.IsActionSucceed = true;
            return(View("UploadLabelFromCSVFileDone", doneModel));
        }