Example #1
0
func (vs *verifyStrings) isTemplatedStringTranslationInvalid(stringInfo common.I18nStringInfo) bool {
	if !common.IsTemplatedString(stringInfo.ID) || !common.IsTemplatedString(stringInfo.Translation) {
		return false
	}

	translationArgs := common.GetTemplatedStringArgs(stringInfo.Translation)
	argsMap := make(map[string]string)
	for _, translationArg := range translationArgs {
		argsMap[translationArg] = translationArg
	}

	var missingArgs []string
	idArgs := common.GetTemplatedStringArgs(stringInfo.ID)
	for _, idArg := range idArgs {
		if _, ok := argsMap[idArg]; !ok {
			missingArgs = append(missingArgs, idArg)
		}
	}

	if len(missingArgs) > 0 {
		vs.Println("i18n4go: templated string is invalid, missing args in translation:", strings.Join(missingArgs, ","))
		return true
	}

	return false
}
Example #2
0
func (rp *rewritePackage) wrapBasicLitWithTemplatedT(basicLit *ast.BasicLit, args []ast.Expr, callExpr *ast.CallExpr, argIndex int) ast.Expr {
	valueWithoutQuotes, _ := strconv.Unquote(basicLit.Value) //basicLit.Value[1 : len(basicLit.Value)-1]

	_, ok := rp.ExtractedStrings[valueWithoutQuotes]
	if !ok && rp.ExtractedStrings != nil {
		return callExpr
	}

	rp.TotalStrings++
	tIdent := &ast.Ident{Name: "T"}
	argNames := common.GetTemplatedStringArgs(valueWithoutQuotes)

	compositeExpr := []ast.Expr{}
	processedArgsMap := make(map[string]bool)

	for i, argName := range argNames {
		if callExpr, ok := args[argIndex+i+1].(*ast.CallExpr); ok {
			rp.callExprTFunc(callExpr)
		} else if basicLit, ok := args[argIndex+i+1].(*ast.BasicLit); ok {
			args[argIndex+i] = rp.wrapBasicLitWithT(basicLit)
		}

		if processedArgsMap[argName] != true {
			quotedArgName := "\"" + argName + "\""
			basicLit.ValuePos = 0

			valueExpr := args[argIndex+i+1]
			if basicLit, ok := args[argIndex+i+1].(*ast.BasicLit); ok {
				valueExpr = rp.wrapBasicLitWithT(basicLit)
			}

			keyValueExpr := &ast.KeyValueExpr{Key: &ast.BasicLit{Kind: 9, Value: quotedArgName}, Value: valueExpr}
			processedArgsMap[argName] = true
			compositeExpr = append(compositeExpr, keyValueExpr)
		}
	}

	mapInterfaceType := &ast.InterfaceType{Interface: 142, Methods: &ast.FieldList{List: nil, Opening: 1, Closing: 2}, Incomplete: false}
	mapType := &ast.MapType{Map: 131, Key: &ast.Ident{Name: "string"}, Value: mapInterfaceType}
	compositeLit := &ast.CompositeLit{Type: mapType, Elts: compositeExpr}

	return &ast.CallExpr{Fun: tIdent, Args: []ast.Expr{basicLit, compositeLit}}
}