`gettext` and checking which locales are missing keys
package main
import (
"fmt"
"path/filepath"
"github.com/leonelquinteros/gotext"
)
func main() {
matches, _ := filepath.Glob("**/*.po")
var allLocales []string
for _, v := range matches {
allLocales = append(allLocales, filepath.Base(v))
}
translationKeys := make(map[string][]string)
for _, v := range matches {
po := gotext.NewPo()
po.ParseFile(v)
for k := range po.GetDomain().GetTranslations() {
// keys = append(keys, k)
if _, ok := translationKeys[k]; !ok {
translationKeys[k] = make([]string, 0)
}
translationKeys[k] = append(translationKeys[k], filepath.Base(v))
}
}
for k, v := range translationKeys {
if len(v) != len(allLocales) {
fmt.Println(k, "does not have a translation in every locale, missing ", missing(v, allLocales))
}
}
}
// https://stackoverflow.com/a/57448769
// empty struct (0 bytes)
type void struct{}
// missing compares two slices and returns slice of differences
func missing(a, b []string) []string {
// create map with length of the 'a' slice
ma := make(map[string]void, len(a))
diffs := []string{}
// Convert first slice to map with empty struct (0 bytes)
for _, ka := range a {
ma[ka] = void{}
}
// find missing values in a
for _, kb := range b {
if _, ok := ma[kb]; !ok {
diffs = append(diffs, kb)
}
}
return diffs
}
// end https://stackoverflow.com/a/57448769
Edited by Jamie Tanna