github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/i18n/locale.go (about)

     1  package i18n
     2  
     3  import (
     4  	"path"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/i18n/resources"
     8  	"code.cloudfoundry.org/cli/util/ui"
     9  )
    10  
    11  const resourceSuffix = ".all.json"
    12  
    13  func SupportedLocales() []string {
    14  	languages := supportedLanguages()
    15  	localeNames := make([]string, len(languages))
    16  
    17  	for i, l := range languages {
    18  		localeParts := strings.Split(l, "-")
    19  		lang := localeParts[0]
    20  		regionOrScript := localeParts[1]
    21  
    22  		switch len(regionOrScript) {
    23  		case 2: // Region
    24  			localeNames[i] = lang + "-" + strings.ToUpper(regionOrScript)
    25  		case 4: // Script
    26  			localeNames[i] = lang + "-" + strings.Title(regionOrScript)
    27  		default:
    28  			localeNames[i] = l
    29  		}
    30  	}
    31  
    32  	return localeNames
    33  }
    34  
    35  func IsSupportedLocale(locale string) bool {
    36  	sanitizedLocale, err := ui.ParseLocale(locale)
    37  	if err != nil {
    38  		return false
    39  	}
    40  
    41  	for _, supportedLanguage := range supportedLanguages() {
    42  		if supportedLanguage == sanitizedLocale {
    43  			return true
    44  		}
    45  	}
    46  
    47  	return false
    48  }
    49  
    50  func supportedLanguages() []string {
    51  	assetNames := resources.AssetNames()
    52  
    53  	var languages []string
    54  	for _, assetName := range assetNames {
    55  		assetLocale := strings.TrimSuffix(path.Base(assetName), resourceSuffix)
    56  		locale, _ := ui.ParseLocale(assetLocale)
    57  		languages = append(languages, locale)
    58  	}
    59  
    60  	return languages
    61  }