Esempio n. 1
0
func (ct *createTranslations) createTranslationFileWithGoogleTranslate(language string) (string, error) {
	fileName, _, err := common.CheckFile(ct.Filename)
	if err != nil {
		return "", err
	}

	err = common.CreateOutputDirsIfNeeded(ct.OutputDirname)
	if err != nil {
		ct.Println(err)
		return "", fmt.Errorf("i18n4go: could not create output directory: %s", ct.OutputDirname)
	}

	destFilename := filepath.Join(ct.OutputDirname, strings.Replace(fileName, ct.options.SourceLanguageFlag, language, -1))

	i18nStringInfos, err := common.LoadI18nStringInfos(ct.Filename)
	if err != nil {
		ct.Println(err)
		return "", fmt.Errorf("i18n4go: could not load i18n strings from file: %s", ct.Filename)
	}

	if len(i18nStringInfos) == 0 {
		return "", fmt.Errorf("i18n4go: input file: %s is empty", ct.Filename)
	}

	ct.Println("i18n4go: attempting to use Google Translate to translate source strings in: ", language)
	modifiedI18nStringInfos := make([]common.I18nStringInfo, len(i18nStringInfos))
	for i, i18nStringInfo := range i18nStringInfos {
		translation, _, err := ct.googleTranslate(i18nStringInfo.Translation, language)
		if err != nil {
			ct.Println("i18n4go: error invoking Google Translate for string:", i18nStringInfo.Translation)
		} else {
			modifiedI18nStringInfos[i] = common.I18nStringInfo{ID: i18nStringInfo.ID, Translation: translation}
		}
	}

	err = common.SaveI18nStringInfos(ct, ct.Options(), modifiedI18nStringInfos, destFilename)
	if err != nil {
		ct.Println(err)
		return "", fmt.Errorf("i18n4go: could not save Google Translate i18n strings to file: %s", destFilename)
	}

	if ct.options.PoFlag {
		poFilename := destFilename[:len(destFilename)-len(".json")] + ".po"
		err = common.SaveI18nStringsInPo(ct, ct.Options(), modifiedI18nStringInfos, poFilename)
		if err != nil {
			ct.Println(err)
			return "", fmt.Errorf("i18n4go: could not save PO file: %s", poFilename)
		}
	}

	ct.Println()

	return destFilename, nil
}
Esempio n. 2
0
func (rp *rewritePackage) addInitFuncToPackage(packageName, outputDir, importPath string) error {
	rp.Println("i18n4go: adding init func to package:", packageName, " to output dir:", outputDir)

	common.CreateOutputDirsIfNeeded(outputDir)

	pieces := strings.Split(importPath, "/")
	for index, str := range pieces {
		pieces[index] = `"` + str + `"`
	}

	joinedImportPath := "filepath.Join(" + strings.Join(pieces, ", ") + ")"
	content := rp.getInitFuncCodeSnippetContent(packageName, joinedImportPath)

	return ioutil.WriteFile(filepath.Join(outputDir, "i18n_init.go"), []byte(content), 0666)
}
Esempio n. 3
0
func (vs *verifyStrings) generateInvalidTranslationDiffFile(invalidStringInfos []common.I18nStringInfo, fileName string) (string, error) {
	name, pathName, err := common.CheckFile(fileName)
	if err != nil {
		return "", err
	}

	diffFilename := name + ".invalid.diff.json"
	if vs.OutputDirname != "" {
		common.CreateOutputDirsIfNeeded(vs.OutputDirname)
		diffFilename = filepath.Join(vs.OutputDirname, diffFilename)
	} else {
		diffFilename = filepath.Join(pathName, diffFilename)
	}

	return diffFilename, common.SaveI18nStringInfos(vs, vs.Options(), invalidStringInfos, diffFilename)
}
Esempio n. 4
0
func (rp *rewritePackage) saveASTFile(relativeFilePath, fileName string, astFile *ast.File, fileSet *token.FileSet) error {
	var buffer bytes.Buffer
	if err := format.Node(&buffer, fileSet, astFile); err != nil {
		return err
	}

	pathToFile := filepath.Join(rp.OutputDirname, relativeFilePath)
	fileInfo, err := os.Stat(fileName)
	if err != nil {
		return err
	}

	common.CreateOutputDirsIfNeeded(filepath.Dir(pathToFile))

	rp.Println("saving file to path", pathToFile)
	ioutil.WriteFile(pathToFile, buffer.Bytes(), fileInfo.Mode())

	return nil
}
Esempio n. 5
0
func (es *extractStrings) saveExtractedStrings(outputDirname string) error {
	if len(es.ExtractedStrings) != 0 {
		es.Println("Saving extracted strings to file:", es.Filename)
	}

	if !es.options.DryRunFlag {
		err := common.CreateOutputDirsIfNeeded(outputDirname)
		if err != nil {
			es.Println(err)
			return err
		}
	}

	stringInfos := make([]common.StringInfo, 0)
	for _, stringInfo := range es.ExtractedStrings {
		stringInfo.Filename = strings.Split(es.Filename, ".extracted.json")[0]

		stringInfos = append(stringInfos, stringInfo)
	}

	jsonData, err := json.MarshalIndent(stringInfos, "", "   ")
	if err != nil {
		es.Println(err)
		return err
	}
	jsonData = common.UnescapeHTML(jsonData)

	if !es.options.DryRunFlag && len(stringInfos) != 0 {
		file, err := os.Create(filepath.Join(outputDirname, es.Filename[strings.LastIndex(es.Filename, string(os.PathSeparator))+1:len(es.Filename)]))
		defer file.Close()
		if err != nil {
			es.Println(err)
			return err
		}

		file.Write(jsonData)
	}

	return nil
}