github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/language_test.go (about)

     1  // Copyright 2020 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/gohugoio/hugo/htesting"
    22  
    23  	qt "github.com/frankban/quicktest"
    24  )
    25  
    26  func TestI18n(t *testing.T) {
    27  	c := qt.New(t)
    28  
    29  	//https://github.com/gohugoio/hugo/issues/7804
    30  	c.Run("pt-br should be case insensitive", func(c *qt.C) {
    31  		b := newTestSitesBuilder(c)
    32  		langCode := func() string {
    33  			c := "pt-br"
    34  			if htesting.RandBool() {
    35  				c = strings.ToUpper(c)
    36  			}
    37  			return c
    38  		}
    39  
    40  		b.WithConfigFile(`toml`, fmt.Sprintf(`
    41  baseURL = "https://example.com"
    42  defaultContentLanguage = "%s"
    43  
    44  [languages]
    45  [languages.%s]
    46  weight = 1
    47  `, langCode(), langCode()))
    48  
    49  		b.WithI18n(fmt.Sprintf("i18n/%s.toml", langCode()), `hello.one = "Hello"`)
    50  		b.WithTemplates("index.html", `Hello: {{ i18n "hello" 1 }}`)
    51  		b.WithContent("p1.md", "")
    52  		b.Build(BuildCfg{})
    53  
    54  		b.AssertFileContent("public/index.html", "Hello: Hello")
    55  	})
    56  }
    57  
    58  func TestLanguageBugs(t *testing.T) {
    59  	c := qt.New(t)
    60  
    61  	// Issue #8672
    62  	c.Run("Config with language, menu in root only", func(c *qt.C) {
    63  		b := newTestSitesBuilder(c)
    64  		b.WithConfigFile("toml", `
    65  theme = "test-theme"
    66  [[menus.foo]]
    67  name = "foo-a"
    68  [languages.en]
    69  
    70  `,
    71  		)
    72  
    73  		b.WithThemeConfigFile("toml", `[languages.en]`)
    74  
    75  		b.Build(BuildCfg{})
    76  
    77  		menus := b.H.Sites[0].Menus()
    78  		c.Assert(menus, qt.HasLen, 1)
    79  
    80  	})
    81  }
    82  
    83  func TestLanguageNumberFormatting(t *testing.T) {
    84  
    85  	b := newTestSitesBuilder(t)
    86  	b.WithConfigFile("toml", `
    87  baseURL = "https://example.org"
    88  
    89  defaultContentLanguage = "en"
    90  defaultContentLanguageInSubDir = true
    91  
    92  [languages]
    93  [languages.en]
    94  timeZone="UTC"
    95  weight=10
    96  [languages.nn]
    97  weight=20
    98  	
    99  `)
   100  
   101  	b.WithTemplates("index.html", `
   102  
   103  FormatNumber: {{ 512.5032 | lang.FormatNumber 2 }}
   104  FormatPercent: {{ 512.5032 | lang.FormatPercent 2 }}
   105  FormatCurrency: {{ 512.5032 | lang.FormatCurrency 2 "USD" }}
   106  FormatAccounting: {{ 512.5032 | lang.FormatAccounting 2 "NOK" }}
   107  FormatNumberCustom: {{ lang.FormatNumberCustom 2 12345.6789 }}
   108  
   109  # We renamed this to FormatNumberCustom in 0.87.0.
   110  NumFmt: {{ -98765.4321 | lang.NumFmt 2 }}
   111  
   112  	
   113  `)
   114  	b.WithContent("p1.md", "")
   115  
   116  	b.Build(BuildCfg{})
   117  
   118  	b.AssertFileContent("public/en/index.html", `
   119  FormatNumber: 512.50
   120  FormatPercent: 512.50%
   121  FormatCurrency: $512.50
   122  FormatAccounting: NOK512.50
   123  FormatNumberCustom: 12,345.68
   124          
   125  NumFmt: -98,765.43
   126  `,
   127  	)
   128  
   129  	b.AssertFileContent("public/nn/index.html", `
   130  FormatNumber: 512,50
   131  FormatPercent: 512,50 %
   132  FormatCurrency: 512,50 USD
   133  FormatAccounting: 512,50 kr
   134  FormatNumberCustom: 12,345.68
   135  
   136  # We renamed this to FormatNumberCustom in 0.87.0.
   137  NumFmt: -98,765.43
   138  `)
   139  }