Skip to content

Commit

Permalink
extract: support typed string constants (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorj-thetreep authored Oct 5, 2023
1 parent 8b7b3cb commit 5ee16aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions v2/goi18n/extract_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ func extractStringLiteral(expr ast.Expr) (string, bool) {
}
return s, true
}
return "", false
default:
return "", false
case *ast.CallExpr:
if fun, ok := v.Fun.(*ast.Ident); ok && fun.Name == "string" {
return extractStringLiteral(v.Args[0])
}
}
return "", false
}

func i18nPackageName(file *ast.File) string {
Expand Down
19 changes: 19 additions & 0 deletions v2/goi18n/extract_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ zero = "Zero translation"
}
`,
},
{
name: "casted const",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
type ConstType string
const Const ConstType = "my const"
var m = &i18n.LocalizeConfig{
ID: "id",
Other: string(Const),
}
`,
activeFile: []byte(`id = "my const"
`),
},
}

for _, test := range tests {
Expand Down

0 comments on commit 5ee16aa

Please sign in to comment.