git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/languages/languages.go (about)

     1  package languages
     2  
     3  import (
     4  	_ "embed"
     5  	"encoding/json"
     6  	"fmt"
     7  )
     8  
     9  //go:embed languages.json
    10  var Bytes []byte
    11  
    12  type Language struct {
    13  	Code       string `json:"code"`
    14  	Name       string `json:"name"`
    15  	NativeName string `json:"native_name"`
    16  }
    17  
    18  var langs map[string]Language
    19  
    20  func Get() map[string]Language {
    21  	var err error
    22  
    23  	if langs == nil {
    24  		langs = map[string]Language{}
    25  		err = json.Unmarshal(Bytes, &langs)
    26  		if err != nil {
    27  			err = fmt.Errorf("languages: parsing languages JSON file: %w", err)
    28  			panic(err)
    29  		}
    30  	}
    31  
    32  	return langs
    33  }