go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

lang.go (856B)


      1 package lang
      2 
      3 import (
      4 	"fmt"
      5 
      6 	"github.com/barbashov/iso639-3"
      7 )
      8 
      9 var (
     10 	// Default language (hard to get around)
     11 	Default = "eng" // ISO639-3
     12 )
     13 
     14 // Language is used to set and get language translation to be used for rendering output.
     15 type Language struct {
     16 	Code string
     17 	Name string
     18 }
     19 
     20 // LanguageFromCode returns a Language object from the given ISO-639-3 (three-letter) code.
     21 //
     22 // Will fail if an unknown code is provided.
     23 func LanguageFromCode(code string) (Language, error) {
     24 	r := iso639_3.FromAnyCode(code)
     25 	if r == nil {
     26 		return Language{}, fmt.Errorf("invalid language code: %s", code)
     27 	}
     28 	return Language{
     29 		Code: r.Part3,
     30 		Name: r.Name,
     31 	}, nil
     32 }
     33 
     34 // String implements the String interface.
     35 //
     36 // Returns a representation of the Language fit for debugging.
     37 func(l Language) String() string {
     38 	return fmt.Sprintf("%s (%s)", l.Code, l.Name)
     39 }