github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/util/ui/i18n.go (about)

     1  package ui
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"path"
     7  	"strings"
     8  	"text/template"
     9  
    10  	"code.cloudfoundry.org/cli/cf/resources"
    11  	"github.com/nicksnyder/go-i18n/i18n"
    12  	"github.com/nicksnyder/go-i18n/i18n/language"
    13  )
    14  
    15  const (
    16  	defaultLocale  = "en-us"
    17  	resourceSuffix = ".all.json"
    18  	zhTW           = "zh-tw"
    19  	zhHK           = "zh-hk"
    20  	zhHant         = "zh-hant"
    21  	hyphen         = "-"
    22  	underscore     = "_"
    23  )
    24  
    25  // GetTranslationFunc will return back a function that can be used to translate
    26  // strings into the currently set locale.
    27  func GetTranslationFunc(config Config) (i18n.TranslateFunc, error) {
    28  	t, err := getConfiguredLocal(config)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	if t == nil {
    34  		t, err = getDefaultLocal()
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  	}
    39  
    40  	return translationWrapper(t), nil
    41  }
    42  
    43  func translationWrapper(translationFunc i18n.TranslateFunc) i18n.TranslateFunc {
    44  	return func(translationID string, args ...interface{}) string {
    45  		var keys interface{}
    46  		if len(args) > 0 {
    47  			keys = args[0]
    48  		}
    49  
    50  		if translated := translationFunc(translationID, keys); translated != translationID {
    51  			return translated
    52  		}
    53  
    54  		var buffer bytes.Buffer
    55  		formattedTemplate := template.Must(template.New("Display Text").Parse(translationID))
    56  		formattedTemplate.Execute(&buffer, keys)
    57  
    58  		return buffer.String()
    59  	}
    60  }
    61  
    62  func getConfiguredLocal(config Config) (i18n.TranslateFunc, error) {
    63  	source := config.Locale()
    64  	assetNames := resources.AssetNames()
    65  
    66  	for _, l := range language.Parse(source) {
    67  		if l.Tag == zhTW || l.Tag == zhHK {
    68  			l.Tag = zhHant
    69  		}
    70  
    71  		for _, assetName := range assetNames {
    72  			assetLocale := strings.ToLower(strings.Replace(path.Base(assetName), underscore, hyphen, -1))
    73  			if strings.HasPrefix(assetLocale, l.Tag) {
    74  				err := loadAsset(assetName)
    75  				if err != nil {
    76  					return nil, err
    77  				}
    78  
    79  				return i18n.MustTfunc(l.Tag), nil
    80  			}
    81  		}
    82  	}
    83  
    84  	return nil, nil
    85  }
    86  
    87  func getDefaultLocal() (i18n.TranslateFunc, error) {
    88  	err := loadAsset("cf/i18n/resources/" + defaultLocale + resourceSuffix)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return i18n.MustTfunc(defaultLocale), nil
    94  }
    95  
    96  func loadAsset(assetName string) error {
    97  	assetBytes, err := resources.Asset(assetName)
    98  	if err != nil {
    99  		return fmt.Errorf("Could not load asset '%s': %s", assetName, err.Error())
   100  	}
   101  
   102  	err = i18n.ParseTranslationFileBytes(assetName, assetBytes)
   103  	if err != nil {
   104  		return fmt.Errorf("Could not load translations '%s': %s", assetName, err.Error())
   105  	}
   106  	return nil
   107  }