2023-07-05 20:52:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2023-10-05 13:29:06 +00:00
|
|
|
"sort"
|
2023-07-05 20:52:09 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var langKeyFormat = regexp.MustCompile(`^[a-z]{2}(-[A-Z]{2})?$`)
|
|
|
|
|
|
|
|
func verify(tf translationFile) error {
|
2023-10-05 13:29:06 +00:00
|
|
|
var err error
|
2023-07-05 20:52:09 +00:00
|
|
|
|
|
|
|
if !langKeyFormat.MatchString(tf.Reference.LanguageKey) {
|
|
|
|
return errors.New("reference contains invalid languageKey")
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
if len(tf.Reference.Translations) == 0 {
|
2023-07-05 20:52:09 +00:00
|
|
|
return errors.New("reference does not contain translations")
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
logrus.Infof("found %d translation keys in reference", len(tf.Reference.Translations))
|
2023-07-05 20:52:09 +00:00
|
|
|
|
|
|
|
if tf.Reference.FormalTranslations != nil {
|
2023-10-05 13:29:06 +00:00
|
|
|
if verifyTranslationKeys(
|
|
|
|
logrus.NewEntry(logrus.StandardLogger()),
|
|
|
|
tf.Reference.FormalTranslations,
|
|
|
|
tf.Reference.Translations,
|
|
|
|
false,
|
|
|
|
); err != nil {
|
2023-07-05 20:52:09 +00:00
|
|
|
return errors.New("reference contains error in formalTranslations")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var hadErrors bool
|
|
|
|
for lk, tm := range tf.Translations {
|
|
|
|
logger := logrus.WithField("lang", lk)
|
|
|
|
logger.Info("validating language...")
|
|
|
|
|
|
|
|
if !langKeyFormat.MatchString(lk) {
|
|
|
|
hadErrors = true
|
|
|
|
logger.Error("language key is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tm.DeeplLanguage == "" {
|
|
|
|
logger.Info("no deeplLanguage is set")
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
hadErrors = hadErrors || verifyTranslationKeys(
|
|
|
|
logger,
|
|
|
|
tm.Translations,
|
|
|
|
tf.Reference.Translations,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
hadErrors = hadErrors || verifyTranslationKeys(
|
|
|
|
logger,
|
|
|
|
tm.FormalTranslations,
|
|
|
|
tf.Reference.Translations,
|
|
|
|
false,
|
|
|
|
)
|
2023-07-05 20:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if hadErrors {
|
|
|
|
return errors.New("translation file has errors")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
//revive:disable-next-line:flag-parameter
|
|
|
|
func verifyTranslationKeys(logger *logrus.Entry, t, ref translation, warnMissing bool) (hadErrors bool) {
|
|
|
|
missing, extra, wrongType := t.GetErrorKeys(ref)
|
2023-07-05 20:52:09 +00:00
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
sort.Strings(extra)
|
|
|
|
sort.Strings(missing)
|
|
|
|
sort.Strings(wrongType)
|
2023-07-05 20:52:09 +00:00
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
for _, k := range extra {
|
|
|
|
logger.WithField("translation_key", k).Error("extra key found")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range wrongType {
|
|
|
|
logger.WithField("translation_key", k).Error("key has invalid type")
|
2023-07-05 20:52:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
if warnMissing {
|
|
|
|
for _, k := range missing {
|
2023-07-05 20:52:09 +00:00
|
|
|
logger.WithField("translation_key", k).Warn("missing translation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:29:06 +00:00
|
|
|
return len(extra)+len(wrongType) > 0
|
2023-07-05 20:52:09 +00:00
|
|
|
}
|