github.com/neohugo/neohugo@v0.123.8/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/neohugo/neohugo/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  func TestLanguageNumberFormatting(t *testing.T) {
    83  	b := newTestSitesBuilder(t)
    84  	b.WithConfigFile("toml", `
    85  baseURL = "https://example.org"
    86  
    87  defaultContentLanguage = "en"
    88  defaultContentLanguageInSubDir = true
    89  
    90  [languages]
    91  [languages.en]
    92  timeZone="UTC"
    93  weight=10
    94  [languages.nn]
    95  weight=20
    96  	
    97  `)
    98  
    99  	b.WithTemplates("index.html", `
   100  
   101  FormatNumber: {{ 512.5032 | lang.FormatNumber 2 }}
   102  FormatPercent: {{ 512.5032 | lang.FormatPercent 2 }}
   103  FormatCurrency: {{ 512.5032 | lang.FormatCurrency 2 "USD" }}
   104  FormatAccounting: {{ 512.5032 | lang.FormatAccounting 2 "NOK" }}
   105  FormatNumberCustom: {{ lang.FormatNumberCustom 2 12345.6789 }}
   106  
   107  # We renamed this to FormatNumberCustom in 0.87.0.
   108  NumFmt: {{ -98765.4321 | lang.NumFmt 2 }}
   109  
   110  	
   111  `)
   112  	b.WithContent("p1.md", "")
   113  
   114  	b.Build(BuildCfg{})
   115  
   116  	b.AssertFileContent("public/en/index.html", `
   117  FormatNumber: 512.50
   118  FormatPercent: 512.50%
   119  FormatCurrency: $512.50
   120  FormatAccounting: NOK512.50
   121  FormatNumberCustom: 12,345.68
   122          
   123  NumFmt: -98,765.43
   124  `,
   125  	)
   126  
   127  	b.AssertFileContent("public/nn/index.html", `
   128  FormatNumber: 512,50
   129  FormatPercent: 512,50 %
   130  FormatCurrency: 512,50 USD
   131  FormatAccounting: 512,50 kr
   132  FormatNumberCustom: 12,345.68
   133  
   134  # We renamed this to FormatNumberCustom in 0.87.0.
   135  NumFmt: -98,765.43
   136  `)
   137  }
   138  
   139  // Issue 11993.
   140  func TestI18nDotFile(t *testing.T) {
   141  	files := `
   142  -- hugo.toml --{}
   143  baseURL = "https://example.com"
   144  -- i18n/.keep --
   145  -- data/.keep --
   146  `
   147  	Test(t, files)
   148  }