Example #1
0
func (fix *Fixup) findI18nStrings(i18nFile string) (i18nStrings map[string]common.I18nStringInfo, err error) {
	i18nStrings = make(map[string]common.I18nStringInfo)

	stringInfos, err := common.LoadI18nStringInfos(i18nFile)

	if err != nil {
		return nil, err
	}

	return common.CreateI18nStringInfoMap(stringInfos)
}
func (rp *rewritePackage) loadStringsToBeTranslated(fileName string) error {
	if fileName != "" {
		stringList, err := common.LoadI18nStringInfos(fileName)
		if err != nil {
			return err
		}

		rp.ExtractedStrings, err = common.CreateI18nStringInfoMap(stringList)
		if err != nil {
			return err
		}

		rp.UpdatedExtractedStrings = common.CopyI18nStringInfoMap(rp.ExtractedStrings)
	}

	return nil
}
func (vs *verifyStrings) verify(inputFilename string, targetFilename string) error {
	common.CheckFile(targetFilename)

	inputI18nStringInfos, err := common.LoadI18nStringInfos(inputFilename)
	if err != nil {
		vs.Println("i18n4go: Error loading the i18n strings from input filename:", inputFilename)
		return err
	}

	if len(inputI18nStringInfos) == 0 {
		return fmt.Errorf("i18n4go: Error input file: %s is empty", inputFilename)
	}

	inputMap, err := common.CreateI18nStringInfoMap(inputI18nStringInfos)
	if err != nil {
		return fmt.Errorf("File has duplicated key: %s\n%s", inputFilename, err)
	}

	targetI18nStringInfos, err := common.LoadI18nStringInfos(targetFilename)
	if err != nil {
		vs.Println("i18n4go: Error loading the i18n strings from target filename:", targetFilename)
		return err
	}

	var targetExtraStringInfos, targetInvalidStringInfos []common.I18nStringInfo
	for _, stringInfo := range targetI18nStringInfos {
		if _, ok := inputMap[stringInfo.ID]; ok {
			if common.IsTemplatedString(stringInfo.ID) && vs.isTemplatedStringTranslationInvalid(stringInfo) {
				vs.Println("i18n4go: WARNING target file has invalid templated translations with key ID: ", stringInfo.ID)
				targetInvalidStringInfos = append(targetInvalidStringInfos, stringInfo)
			}
			delete(inputMap, stringInfo.ID)
		} else {
			vs.Println("i18n4go: WARNING target file has extra key with ID: ", stringInfo.ID)
			targetExtraStringInfos = append(targetExtraStringInfos, stringInfo)
		}
	}

	var verficationError error
	if len(targetExtraStringInfos) > 0 {
		vs.Println("i18n4go: WARNING target file contains total of extra keys:", len(targetExtraStringInfos))

		diffFilename, err := vs.generateExtraKeysDiffFile(targetExtraStringInfos, targetFilename)
		if err != nil {
			vs.Println("i18n4go: ERROR could not create the diff file:", err)
			return err
		}
		vs.Println("i18n4go: generated diff file:", diffFilename)
		verficationError = fmt.Errorf("i18n4go: target file has extra i18n strings with IDs: %s", strings.Join(keysForI18nStringInfos(targetExtraStringInfos), ","))
	}

	if len(targetInvalidStringInfos) > 0 {
		vs.Println("i18n4go: WARNING target file contains total of invalid translations:", len(targetInvalidStringInfos))

		diffFilename, err := vs.generateInvalidTranslationDiffFile(targetInvalidStringInfos, targetFilename)
		if err != nil {
			vs.Println("i18n4go: ERROR could not create the diff file:", err)
			return err
		}
		vs.Println("i18n4go: generated diff file:", diffFilename)
		verficationError = fmt.Errorf("i18n4go: target file has invalid i18n strings with IDs: %s", strings.Join(keysForI18nStringInfos(targetInvalidStringInfos), ","))
	}

	if len(inputMap) > 0 {
		vs.Println("i18n4go: ERROR input file does not match target file:", targetFilename)

		diffFilename, err := vs.generateMissingKeysDiffFile(valuesForI18nStringInfoMap(inputMap), targetFilename)
		if err != nil {
			vs.Println("i18n4go: ERROR could not create the diff file:", err)
			return err
		}
		vs.Println("i18n4go: generated diff file:", diffFilename)
		verficationError = fmt.Errorf("i18n4go: target file is missing i18n strings with IDs: %s", strings.Join(keysForI18nStringInfoMap(inputMap), ","))
	}

	return verficationError
}
Example #4
0
			Ω(exitCode).Should(BeNil())
		})

		Context("When the user says the translation was updated", func() {
			JustBeforeEach(func() {
				Ω(getNextOutputLine(stdoutReader)).Should(ContainSubstring("Is the string \"I like apples.\" a new or updated string? [new/upd]"))
				stdinPipe.Write([]byte("upd\n"))
				stdinPipe.Write([]byte("1\n"))
			})

			It("Updates the keys for all translation files", func() {
				cmd.Wait()

				translations, err := common.LoadI18nStringInfos(filepath.Join(".", "translations", "en_US.all.json"))
				Ω(err).ShouldNot(HaveOccurred())
				mappedTranslations, err := common.CreateI18nStringInfoMap(translations)
				Ω(err).ShouldNot(HaveOccurred())
				Ω(mappedTranslations["I like bananas."]).Should(Equal(common.I18nStringInfo{}))
				Ω(mappedTranslations["I like apples."]).ShouldNot(Equal(common.I18nStringInfo{}))

				translations, err = common.LoadI18nStringInfos(filepath.Join(".", "translations", "zh_CN.all.json"))
				Ω(err).ShouldNot(HaveOccurred())
				mappedTranslations, err = common.CreateI18nStringInfoMap(translations)
				Ω(err).ShouldNot(HaveOccurred())
				Ω(mappedTranslations["I like bananas."]).Should(Equal(common.I18nStringInfo{}))
				Ω(mappedTranslations["I like apples."]).ShouldNot(Equal(common.I18nStringInfo{}))
			})

			It("Updates all the translation", func() {
				cmd.Wait()