docs(i18n): clarify Set* behaviour and fix CurrentLanguage default

- CurrentLanguage() now returns "en-GB" (fallback) instead of "" when
  service is nil, consistent with other getters returning defaults
- Document why SetLanguage returns error (validates language tag) while
  SetMode, SetFormality, SetDebug do not (just set values)
- Add "Does nothing if service not initialized" to Set* doc comments

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Snider 2026-01-30 18:11:03 +00:00
parent e59f7f8cfd
commit 1f9321b34b
3 changed files with 10 additions and 2 deletions

View file

@ -13,6 +13,7 @@ package i18n
// to find and update translations during development.
// SetDebug enables or disables debug mode on the default service.
// Does nothing if the service is not initialized.
// In debug mode, translations show their keys: [key] translation
//
// SetDebug(true)

View file

@ -61,7 +61,11 @@ func Raw(messageID string, args ...any) string {
var ErrServiceNotInitialized = errors.New("i18n: service not initialized")
// SetLanguage sets the language for the default service.
// Returns ErrServiceNotInitialized if the service has not been initialized.
// Returns ErrServiceNotInitialized if the service has not been initialized,
// or an error if the language tag is invalid or unsupported.
//
// Unlike other Set* functions, this returns an error because it validates
// the language tag against available locales.
func SetLanguage(lang string) error {
svc := Default()
if svc == nil {
@ -71,14 +75,16 @@ func SetLanguage(lang string) error {
}
// CurrentLanguage returns the current language code from the default service.
// Returns "en-GB" (the fallback language) if the service is not initialized.
func CurrentLanguage() string {
if svc := Default(); svc != nil {
return svc.Language()
}
return ""
return "en-GB"
}
// SetMode sets the translation mode for the default service.
// Does nothing if the service is not initialized.
func SetMode(m Mode) {
if svc := Default(); svc != nil {
svc.SetMode(m)

View file

@ -9,6 +9,7 @@ import (
)
// SetFormality sets the default formality level on the default service.
// Does nothing if the service is not initialized.
//
// SetFormality(FormalityFormal) // Use formal address (Sie, vous)
func SetFormality(f Formality) {