| 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/gohugoio/hugo/deps" |
| 20 |
"github.com/gohugoio/hugo/langs" |
| 21 |
"github.com/gohugoio/hugo/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().(*langs.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.FormatAccounting, |
| 36 |
nil, |
| 37 |
[][2]string{ |
| 38 |
{`{{ 512.5032 | lang.FormatAccounting 2 "NOK" }}`, `NOK512.50`}, |
| 39 |
}, |
| 40 |
) |
| 41 |
|
| 42 |
ns.AddMethodMapping(ctx.FormatCurrency, |
| 43 |
nil, |
| 44 |
[][2]string{ |
| 45 |
{`{{ 512.5032 | lang.FormatCurrency 2 "USD" }}`, `$512.50`}, |
| 46 |
}, |
| 47 |
) |
| 48 |
|
| 49 |
ns.AddMethodMapping(ctx.FormatNumber, |
| 50 |
nil, |
| 51 |
[][2]string{ |
| 52 |
{`{{ 512.5032 | lang.FormatNumber 2 }}`, `512.50`}, |
| 53 |
}, |
| 54 |
) |
| 55 |
|
| 56 |
ns.AddMethodMapping(ctx.FormatNumberCustom, |
| 57 |
nil, |
| 58 |
[][2]string{ |
| 59 |
{`{{ lang.FormatNumberCustom 2 12345.6789 }}`, `12,345.68`}, |
| 60 |
{`{{ lang.FormatNumberCustom 2 12345.6789 "- , ." }}`, `12.345,68`}, |
| 61 |
{`{{ lang.FormatNumberCustom 6 -12345.6789 "- ." }}`, `-12345.678900`}, |
| 62 |
{`{{ lang.FormatNumberCustom 0 -12345.6789 "- . ," }}`, `-12,346`}, |
| 63 |
{`{{ lang.FormatNumberCustom 0 -12345.6789 "-|.| " "|" }}`, `-12 346`}, |
| 64 |
{`{{ -98765.4321 | lang.FormatNumberCustom 2 }}`, `-98,765.43`}, |
| 65 |
}, |
| 66 |
) |
| 67 |
|
| 68 |
ns.AddMethodMapping(ctx.FormatPercent, |
| 69 |
nil, |
| 70 |
[][2]string{ |
| 71 |
{`{{ 512.5032 | lang.FormatPercent 2 }}`, `512.50%`}, |
| 72 |
}, |
| 73 |
) |
| 74 |
|
| 75 |
ns.AddMethodMapping(ctx.Merge, |
| 76 |
nil, |
| 77 |
[][2]string{}, |
| 78 |
) |
| 79 |
|
| 80 |
ns.AddMethodMapping(ctx.Translate, |
| 81 |
[]string{"i18n", "T"}, |
| 82 |
[][2]string{}, |
| 83 |
) |
| 84 |
|
| 85 |
return ns |
| 86 |
} |
| 87 |
|
| 88 |
internal.AddTemplateFuncsNamespace(f) |
| 89 |
} |