Пример #1
0
func (ms *MergeStrings) combineStringInfosPerDirectory(directory string) error {
	files, directories := getFilesAndDir(directory)
	fileList := ms.matchFileToSourceLanguage(files, ms.SourceLanguage)

	combinedMap := map[string]common.I18nStringInfo{}
	for _, file := range fileList {
		StringInfos, err := common.LoadI18nStringInfos(file)
		if err != nil {
			return err
		}

		combineStringInfo(StringInfos, combinedMap)
	}

	filePath := filepath.Join(directory, ms.SourceLanguage+".all.json")
	ms.I18nStringInfos = common.I18nStringInfoMapValues2Array(combinedMap)
	sort.Sort(ms)
	common.SaveI18nStringInfos(ms, ms.Options(), ms.I18nStringInfos, filePath)
	ms.Println("i18n4go: saving combined language file: " + filePath)

	if ms.Recurse {
		for _, directory = range directories {
			err := ms.combineStringInfosPerDirectory(directory)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
Пример #2
0
func writeStringInfoMapToJSON(localeMap map[string]common.I18nStringInfo, localeFile string) error {
	localeArray := common.I18nStringInfoMapValues2Array(localeMap)

	sort.Sort(array(localeArray))

	encodedLocale, err := json.MarshalIndent(localeArray, "", "  ")
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(localeFile, encodedLocale, 0644)
	if err != nil {
		return err
	}
	return nil
}
Пример #3
0
func (rp *rewritePackage) processFilename(fileName string) error {
	rp.TotalFiles += 1
	rp.Println("i18n4go: rewriting strings for source file:", fileName)

	fileSet := token.NewFileSet()

	var absFilePath = fileName
	if !filepath.IsAbs(absFilePath) {
		absFilePath = filepath.Join(os.Getenv("PWD"), absFilePath)
	}

	astFile, err := parser.ParseFile(fileSet, absFilePath, nil, parser.ParseComments|parser.AllErrors)
	if err != nil {
		rp.Println(err)
		return err
	}

	if strings.HasSuffix(fileName, "_test.go") {
		rp.Println("cowardly refusing to translate the strings in test file:", fileName)
		return nil
	}

	importPath, err := rp.determineImportPath(absFilePath)
	if err != nil {
		rp.Println("i18n4go: error determining the import path:", err.Error())
		return err
	}

	if rp.OutputDirname == "" {
		rp.OutputDirname = filepath.Dir(fileName)
	}

	outputDir := filepath.Join(rp.OutputDirname, filepath.Dir(rp.relativePathForFile(fileName)))
	err = rp.addInitFuncToPackage(astFile.Name.Name, outputDir, importPath)
	if err != nil {
		rp.Println("i18n4go: error adding init() func to package:", err.Error())
		return err
	}

	err = rp.insertTFuncCall(astFile)
	if err != nil {
		rp.Println("i18n4go: error appending T() to AST file:", err.Error())
		return err
	}

	relativeFilePath := rp.relativePathForFile(fileName)
	err = rp.saveASTFile(relativeFilePath, fileName, astFile, fileSet)
	if err != nil {
		rp.Println("i18n4go: error saving AST file:", err.Error())
		return err
	}

	if rp.SaveExtractedStrings {
		i18nStringInfos := common.I18nStringInfoMapValues2Array(rp.UpdatedExtractedStrings)
		err := common.SaveI18nStringInfos(rp, rp.Options(), i18nStringInfos, rp.I18nStringsFilename)
		if err != nil {
			rp.Println("i18n4go: error saving updated i18n strings file:", err.Error())
			return err
		}
	}

	return err
}