github.com/neohugo/neohugo@v0.123.8/tpl/lang/init.go (about)

     1  // Copyright 2017 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 lang
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/neohugo/neohugo/deps"
    20  	"github.com/neohugo/neohugo/langs"
    21  	"github.com/neohugo/neohugo/tpl/internal"
    22  )
    23  
    24  const name = "lang"
    25  
    26  func init() {
    27  	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
    28  		ctx := New(d, langs.GetTranslator(d.Conf.Language()))
    29  
    30  		ns := &internal.TemplateFuncsNamespace{
    31  			Name:    name,
    32  			Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
    33  		}
    34  
    35  		ns.AddMethodMapping(ctx.Translate,
    36  			[]string{"i18n", "T"},
    37  			[][2]string{},
    38  		)
    39  
    40  		ns.AddMethodMapping(ctx.FormatNumber,
    41  			nil,
    42  			[][2]string{
    43  				{`{{ 512.5032 | lang.FormatNumber 2 }}`, `512.50`},
    44  			},
    45  		)
    46  
    47  		ns.AddMethodMapping(ctx.FormatPercent,
    48  			nil,
    49  			[][2]string{
    50  				{`{{ 512.5032 | lang.FormatPercent 2 }}`, `512.50%`},
    51  			},
    52  		)
    53  
    54  		ns.AddMethodMapping(ctx.FormatCurrency,
    55  			nil,
    56  			[][2]string{
    57  				{`{{ 512.5032 | lang.FormatCurrency 2 "USD" }}`, `$512.50`},
    58  			},
    59  		)
    60  
    61  		ns.AddMethodMapping(ctx.FormatAccounting,
    62  			nil,
    63  			[][2]string{
    64  				{`{{ 512.5032 | lang.FormatAccounting 2 "NOK" }}`, `NOK512.50`},
    65  			},
    66  		)
    67  
    68  		ns.AddMethodMapping(ctx.FormatNumberCustom,
    69  			nil,
    70  			[][2]string{
    71  				{`{{ lang.FormatNumberCustom 2 12345.6789 }}`, `12,345.68`},
    72  				{`{{ lang.FormatNumberCustom 2 12345.6789 "- , ." }}`, `12.345,68`},
    73  				{`{{ lang.FormatNumberCustom 6 -12345.6789 "- ." }}`, `-12345.678900`},
    74  				{`{{ lang.FormatNumberCustom 0 -12345.6789 "- . ," }}`, `-12,346`},
    75  				{`{{ lang.FormatNumberCustom 0 -12345.6789 "-|.| " "|" }}`, `-12 346`},
    76  				{`{{ -98765.4321 | lang.FormatNumberCustom 2 }}`, `-98,765.43`},
    77  			},
    78  		)
    79  
    80  		return ns
    81  	}
    82  
    83  	internal.AddTemplateFuncsNamespace(f)
    84  }