github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/i18n/init_unix_test.go (about)

     1  // +build darwin freebsd linux netbsd openbsd
     2  
     3  package i18n_test
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    10  	"github.com/cloudfoundry/cli/cf/i18n"
    11  	"github.com/cloudfoundry/cli/cf/i18n/detection"
    12  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	go_i18n "github.com/nicksnyder/go-i18n/i18n"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("i18n.Init() function", func() {
    20  	var (
    21  		oldResourcesPath string
    22  		configRepo       core_config.ReadWriter
    23  		detector         detection.Detector
    24  
    25  		T go_i18n.TranslateFunc
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		configRepo = testconfig.NewRepositoryWithDefaults()
    30  		oldResourcesPath = i18n.GetResourcesPath()
    31  		i18n.Resources_path = filepath.Join("cf", "i18n", "test_fixtures")
    32  		detector = &detection.JibberJabberDetector{}
    33  	})
    34  
    35  	JustBeforeEach(func() {
    36  		T = i18n.Init(configRepo, detector)
    37  	})
    38  
    39  	Describe("When a user has a locale configuration set", func() {
    40  		It("panics when the translation files cannot be loaded", func() {
    41  			i18n.Resources_path = filepath.Join("should", "not", "be_valid")
    42  			configRepo.SetLocale("en_us")
    43  
    44  			init := func() { i18n.Init(configRepo, detector) }
    45  			Ω(init).Should(Panic(), "loading translations from an invalid path should panic")
    46  		})
    47  
    48  		It("Panics if the locale is not valid", func() {
    49  			configRepo.SetLocale("abc_def")
    50  
    51  			init := func() { i18n.Init(configRepo, detector) }
    52  			Ω(init).Should(Panic(), "loading translations from an invalid path should panic")
    53  		})
    54  
    55  		Context("when the locale is set to french", func() {
    56  			BeforeEach(func() {
    57  				configRepo.SetLocale("fr_FR")
    58  			})
    59  
    60  			It("translates into french correctly", func() {
    61  				translation := T("No buildpacks found")
    62  				Ω(translation).Should(Equal("Pas buildpacks trouvés"))
    63  			})
    64  		})
    65  
    66  		Context("creates a valid T function", func() {
    67  			BeforeEach(func() {
    68  				configRepo.SetLocale("en_US")
    69  			})
    70  
    71  			It("returns a usable T function for simple strings", func() {
    72  				Ω(T).ShouldNot(BeNil())
    73  
    74  				translation := T("Hello world!")
    75  				Ω("Hello world!").Should(Equal(translation))
    76  			})
    77  
    78  			It("returns a usable T function for complex strings (interpolated)", func() {
    79  				Ω(T).ShouldNot(BeNil())
    80  
    81  				translation := T("Deleting domain {{.DomainName}} as {{.Username}}...", map[string]interface{}{"DomainName": "foo.com", "Username": "Anand"})
    82  				Ω("Deleting domain foo.com as Anand...").Should(Equal(translation))
    83  			})
    84  		})
    85  	})
    86  
    87  	Describe("When the user does not have a locale configuration set", func() {
    88  		AfterEach(func() {
    89  			i18n.Resources_path = oldResourcesPath
    90  			os.Setenv("LC_ALL", "")
    91  			os.Setenv("LANG", "en_US.UTF-8")
    92  		})
    93  
    94  		It("panics when the translation files cannot be loaded", func() {
    95  			os.Setenv("LANG", "en")
    96  			i18n.Resources_path = filepath.Join("should", "not", "be_valid")
    97  
    98  			init := func() { i18n.Init(configRepo, detector) }
    99  			Ω(init).Should(Panic(), "loading translations from an invalid path should panic")
   100  		})
   101  
   102  		Context("loads correct locale", func() {
   103  			It("defaults to en_US when LC_ALL and LANG not set", func() {
   104  				os.Setenv("LC_ALL", "")
   105  				os.Setenv("LANG", "")
   106  
   107  				translation := T("Hello world!")
   108  				Ω("Hello world!").Should(Equal(translation))
   109  			})
   110  
   111  			Context("when there is no territory set", func() {
   112  				BeforeEach(func() {
   113  					os.Setenv("LANG", "en")
   114  				})
   115  
   116  				It("still loads the english translation", func() {
   117  					translation := T("Hello world!")
   118  					Ω("Hello world!").Should(Equal(translation))
   119  				})
   120  			})
   121  
   122  			Context("when the desired language is not supported", func() {
   123  				BeforeEach(func() {
   124  					os.Setenv("LC_ALL", "zz_FF.UTF-8")
   125  				})
   126  
   127  				It("defaults to en_US when langauge is not supported", func() {
   128  					translation := T("Hello world!")
   129  					Ω("Hello world!").Should(Equal(translation))
   130  
   131  					translation = T("No buildpacks found")
   132  					Ω("No buildpacks found").Should(Equal(translation))
   133  				})
   134  
   135  				Context("because we don't have the territory", func() {
   136  					BeforeEach(func() {
   137  						os.Setenv("LC_ALL", "fr_CA.UTF-8")
   138  					})
   139  
   140  					It("defaults to same language in supported territory", func() {
   141  						translation := T("No buildpacks found")
   142  						Ω("Pas buildpacks trouvés").Should(Equal(translation))
   143  					})
   144  				})
   145  			})
   146  
   147  			Context("translates correctly", func() {
   148  				BeforeEach(func() {
   149  					os.Setenv("LC_ALL", "fr_FR.UTF-8")
   150  				})
   151  
   152  				It("T function should return translation if string key exists", func() {
   153  					translation := T("No buildpacks found")
   154  					Ω("Pas buildpacks trouvés").Should(Equal(translation))
   155  				})
   156  			})
   157  
   158  			Context("matches zh_CN to simplified Chinese", func() {
   159  				BeforeEach(func() {
   160  					os.Setenv("LC_ALL", "zh_CN.UTF-8")
   161  				})
   162  
   163  				It("matches to zh_Hans", func() {
   164  					translation := T("No buildpacks found")
   165  					Ω("buildpack未找到").Should(Equal(translation))
   166  				})
   167  			})
   168  
   169  			Context("matches zh_TW locale to traditional Chinese", func() {
   170  				BeforeEach(func() {
   171  					os.Setenv("LC_ALL", "zh_TW.UTF-8")
   172  				})
   173  
   174  				It("matches to zh_Hant", func() {
   175  					translation := T("No buildpacks found")
   176  					Ω("(Hant)No buildpacks found").Should(Equal(translation))
   177  				})
   178  			})
   179  
   180  			Context("matches zh_HK locale to traditional Chinese", func() {
   181  				BeforeEach(func() {
   182  					os.Setenv("LC_ALL", "zh_HK.UTF-8")
   183  				})
   184  
   185  				It("matches to zh_Hant", func() {
   186  					translation := T("No buildpacks found")
   187  					Ω("(Hant)No buildpacks found").Should(Equal(translation))
   188  				})
   189  			})
   190  		})
   191  
   192  		Context("creates a valid T function", func() {
   193  			BeforeEach(func() {
   194  				os.Setenv("LC_ALL", "en_US.UTF-8")
   195  			})
   196  
   197  			It("returns a usable T function for simple strings", func() {
   198  				Ω(T).ShouldNot(BeNil())
   199  
   200  				translation := T("Hello world!")
   201  				Ω("Hello world!").Should(Equal(translation))
   202  			})
   203  
   204  			It("returns a usable T function for complex strings (interpolated)", func() {
   205  				Ω(T).ShouldNot(BeNil())
   206  
   207  				translation := T("Deleting domain {{.DomainName}} as {{.Username}}...", map[string]interface{}{"DomainName": "foo.com", "Username": "Anand"})
   208  				Ω("Deleting domain foo.com as Anand...").Should(Equal(translation))
   209  			})
   210  		})
   211  	})
   212  
   213  	Describe("when the config is set to a non-english language and the LANG environamnt variable is en_US", func() {
   214  		BeforeEach(func() {
   215  			configRepo.SetLocale("fr_FR")
   216  			os.Setenv("LANG", "en_US")
   217  		})
   218  
   219  		AfterEach(func() {
   220  			i18n.Resources_path = oldResourcesPath
   221  			os.Setenv("LANG", "en_US.UTF-8")
   222  		})
   223  
   224  		It("ignores the english LANG enviornmant variable", func() {
   225  			translation := T("No buildpacks found")
   226  			Ω(translation).Should(Equal("Pas buildpacks trouvés"))
   227  		})
   228  	})
   229  })