From 65dea1d4c9c68e6f079edea0d8f4a1651d760221 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 07:30:15 +0000 Subject: [PATCH] refactor(i18n): centralise formality parsing Co-Authored-By: Virgil --- service.go | 65 +++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/service.go b/service.go index 2cc88a5..09538c8 100644 --- a/service.go +++ b/service.go @@ -511,20 +511,8 @@ func (s *Service) getEffectiveContextGenderLocationAndFormality(data any) (strin if v, ok := mapValueString(m, "Location"); ok { location = v } - if v, ok := m["Formality"]; ok { - switch f := v.(type) { - case Formality: - if f != FormalityNeutral { - formality = f - } - case string: - switch core.Lower(f) { - case "formal": - formality = FormalityFormal - case "informal": - formality = FormalityInformal - } - } + if f, ok := parseFormalityValue(m["Formality"]); ok { + formality = f } return context, gender, location, formality } @@ -542,13 +530,8 @@ func (s *Service) getEffectiveContextGenderLocationAndFormality(data any) (strin if v, ok := mapValueString(m, "Location"); ok { location = v } - if v, ok := mapValueString(m, "Formality"); ok { - switch core.Lower(v) { - case "formal": - formality = FormalityFormal - case "informal": - formality = FormalityInformal - } + if f, ok := parseFormalityValue(m["Formality"]); ok { + formality = f } return context, gender, location, formality } @@ -606,33 +589,35 @@ func (s *Service) getEffectiveFormality(data any) Formality { } } if m, ok := data.(map[string]any); ok { - switch f := m["Formality"].(type) { - case Formality: - if f != FormalityNeutral { - return f - } - case string: - switch core.Lower(f) { - case "formal": - return FormalityFormal - case "informal": - return FormalityInformal - } + if f, ok := parseFormalityValue(m["Formality"]); ok { + return f } } if m, ok := data.(map[string]string); ok { - if f, ok := mapValueString(m, "Formality"); ok { - switch core.Lower(f) { - case "formal": - return FormalityFormal - case "informal": - return FormalityInformal - } + if f, ok := parseFormalityValue(m["Formality"]); ok { + return f } } return s.formality } +func parseFormalityValue(value any) (Formality, bool) { + switch f := value.(type) { + case Formality: + if f != FormalityNeutral { + return f, true + } + case string: + switch core.Lower(f) { + case "formal": + return FormalityFormal, true + case "informal": + return FormalityInformal, true + } + } + return FormalityNeutral, false +} + func lookupVariants(key, context, gender, location string, formality Formality, extra map[string]any) []string { var variants []string if context != "" {