github.com/sleungcy-sap/cli@v7.1.0+incompatible/util/ui/i18n_test.go (about)

     1  package ui_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/util/ui"
     5  	"code.cloudfoundry.org/cli/util/ui/uifakes"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("i18n", func() {
    13  	var fakeConfig *uifakes.FakeConfig
    14  
    15  	BeforeEach(func() {
    16  		fakeConfig = new(uifakes.FakeConfig)
    17  	})
    18  
    19  	Describe("GetTranslationFunc", func() {
    20  		DescribeTable("defaults to english",
    21  			func(locale string) {
    22  				fakeConfig.LocaleReturns(locale)
    23  
    24  				translationFunc, err := GetTranslationFunc(fakeConfig)
    25  				Expect(err).ToNot(HaveOccurred())
    26  
    27  				Expect(translationFunc("\nApp started\n")).To(Equal("\nApp started\n"))
    28  			},
    29  
    30  			Entry("when left blank", ""),
    31  			Entry("when given gibberish", "asdfasfsadfsadfsadfa"),
    32  			Entry("when given an unsupported language", "pt-PT"),
    33  		)
    34  
    35  		When("the config file is set", func() {
    36  			DescribeTable("returns the correct language translationFunc",
    37  				func(locale string, expectedTranslation string) {
    38  					fakeConfig.LocaleReturns(locale)
    39  
    40  					translationFunc, err := GetTranslationFunc(fakeConfig)
    41  					Expect(err).ToNot(HaveOccurred())
    42  
    43  					Expect(translationFunc("\nApp started\n")).To(Equal(expectedTranslation))
    44  				},
    45  
    46  				Entry("German", "de-DE", "\nApp gestartet\n"),
    47  				Entry("English", "en-US", "\nApp started\n"),
    48  				Entry("Spanish", "es-ES", "\nApp iniciada\n"),
    49  				Entry("French", "fr-FR", "\nApplication démarrée\n"),
    50  				Entry("Italian", "it-IT", "\nApplicazione avviata\n"),
    51  				Entry("Japanese", "ja-JP", "\nアプリが開始されました\n"),
    52  				Entry("Korean", "ko-KR", "\n앱 시작됨\n"),
    53  				Entry("Brazilian Portuguese", "pt-BR", "\nApp iniciado\n"),
    54  				Entry("Chinese (Simplified)", "zh-HANS", "\n应用程序已启动\n"),
    55  				Entry("Chinese (Traditional)", "zh-HANT", "\n已啟動應用程式\n"),
    56  
    57  				// The following locales use the zh-hant translations
    58  				Entry("Chinese (Traditional and using Taiwanese terms)", "zh-TW", "\n已啟動應用程式\n"),
    59  				Entry("Chinese (Traditional and using Hong Kong terms)", "zh-HK", "\n已啟動應用程式\n"),
    60  			)
    61  
    62  			When("provided keys to iterpolate", func() {
    63  				BeforeEach(func() {
    64  					fakeConfig.LocaleReturns("fr-FR")
    65  				})
    66  
    67  				It("interpolates them properly", func() {
    68  					translationFunc, err := GetTranslationFunc(fakeConfig)
    69  					Expect(err).ToNot(HaveOccurred())
    70  					translated := translationFunc("\nApp {{.AppName}} was started using this command `{{.Command}}`\n", map[string]interface{}{
    71  						"AppName": "some-app-name",
    72  						"Command": "some-command-name",
    73  					})
    74  					Expect(translated).To(Equal("\nL'application some-app-name a été démarrée avec la commande `some-command-name`\n"))
    75  				})
    76  			})
    77  		})
    78  
    79  		When("the translation does not have a value", func() {
    80  			It("uses the id for the translation", func() {
    81  				translationFunc, err := GetTranslationFunc(fakeConfig)
    82  				Expect(err).ToNot(HaveOccurred())
    83  				translated := translationFunc("api version:")
    84  				Expect(translated).To(Equal("api version:"))
    85  			})
    86  		})
    87  	})
    88  
    89  	Describe("ParseLocale", func() {
    90  		DescribeTable("returns the correct language translationFunc",
    91  			func(locale string, expectedLocale string) {
    92  				Expect(ParseLocale(locale)).To(Equal(expectedLocale))
    93  			},
    94  
    95  			Entry("German with underscore and caps", "DE_DE", "de-de"),
    96  			Entry("German", "de-DE", "de-de"),
    97  			Entry("English", "en-US", "en-us"),
    98  			Entry("Spanish", "es-ES", "es-es"),
    99  			Entry("French", "fr-FR", "fr-fr"),
   100  			Entry("Italian", "it-IT", "it-it"),
   101  			Entry("Japanese", "ja-JP", "ja-jp"),
   102  			Entry("Korean", "ko-KR", "ko-kr"),
   103  			Entry("Brazilian Portuguese", "pt-BR", "pt-br"),
   104  			Entry("Chinese (Simplified)", "zh-HANS", "zh-hans"),
   105  			Entry("Chinese (Traditional)", "zh-HANT", "zh-hant"),
   106  
   107  			// The following locales use the zh-hant translations
   108  			Entry("Chinese (Traditional and using Taiwanese terms)", "zh-TW", "zh-hant"),
   109  			Entry("Chinese (Traditional and using Hong Kong terms)", "zh-HK", "zh-hant"),
   110  		)
   111  	})
   112  })