35 lines
750 B
Go
35 lines
750 B
Go
|
|
package lang
|
||
|
|
|
||
|
|
import "fmt"
|
||
|
|
|
||
|
|
// Supported language codes
|
||
|
|
const (
|
||
|
|
English = "en"
|
||
|
|
Spanish = "es"
|
||
|
|
)
|
||
|
|
|
||
|
|
// All returns all supported language codes
|
||
|
|
func All() []string {
|
||
|
|
return []string{English, Spanish}
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsValid checks if a language code is supported
|
||
|
|
func IsValid(lang string) bool {
|
||
|
|
return lang == English || lang == Spanish
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validate returns an error if the language code is unsupported.
|
||
|
|
// It provides helpful error messages showing all supported languages.
|
||
|
|
//
|
||
|
|
// Example:
|
||
|
|
//
|
||
|
|
// if err := lang.Validate("fr"); err != nil {
|
||
|
|
// // err: unsupported language: fr (supported: [en es])
|
||
|
|
// }
|
||
|
|
func Validate(lang string) error {
|
||
|
|
if !IsValid(lang) {
|
||
|
|
return fmt.Errorf("unsupported language: %s (supported: %v)", lang, All())
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|