1package i18n
2
3import (
4 "fmt"
5 "strings"
6)
7
8// Interpolate replaces placeholders in a template string with values from data.
9// Supports {key} syntax for variable interpolation.
10func Interpolate(template string, data map[string]interface{}) string {
11 if len(data) == 0 {
12 return template
13 }
14
15 result := template
16 for key, value := range data {
17 placeholder := "{" + key + "}"
18 replacement := formatValue(value)
19 result = strings.ReplaceAll(result, placeholder, replacement)
20 }
21
22 return result
23}
24
25// formatValue converts a value to its string representation.
26func formatValue(v interface{}) string {
27 if v == nil {
28 return ""
29 }
30
31 switch val := v.(type) {
32 case string:
33 return val
34 case int:
35 return fmt.Sprintf("%d", val)
36 case int64:
37 return fmt.Sprintf("%d", val)
38 case float64:
39 return fmt.Sprintf("%g", val)
40 case bool:
41 return fmt.Sprintf("%t", val)
42 default:
43 return fmt.Sprintf("%v", val)
44 }
45}