i18n

package module
v0.0.0-...-50a850c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 23 Imported by: 0

README

i18n

golang i18n, golang实现的多语言解析使用

安装

  • go get
go get github.com/go-fox/contrib/i18n

使用

# 创建文件夹
mkdir -p language

# 编写中文语言文件
cat >>demo/language/zh_cn.toml<<EOF
{
  "user": {
    "test": "test",
    "username":"username",
    "user_not_found": "user not found",
    "parameter_error":"parameter {{.Parameter}} error"
  }
}
EOF

# 编写中文语言文件
cat >>demo/language/en_us.json<<EOF
[user]
test="测试"
username="用户名"
user_not_found="用户未找到"
parameter_error="参数:{{.Parameter}}错误"
EOF

编写go代码文件demoi18n/main.go

package main

import (
	"fmt"

	"github.com/go-fox/contrilb/i18n"
)

func main() {
	n, err := i18n.New(
		i18n.WithSources(i18n.NewFileSource(
			"./language",
		)),
	)
	if err != nil {
		panic(err)
	}
	// 使用原始方法输出zh_cn的test
	fmt.Printf("user.test zh_cn is: %s\n", n.T("zh_cn", "user.test"))
	// 使用原始方法输出en_us的test
	fmt.Printf("user.test en_us is: %s\n", n.T("en_us", "user.test"))
	// 使用原始方法输出en_us的parameter_error,带参数模板
	fmt.Printf("user.parameter_error en_us is: %s\n", n.T("en_us", "user.parameter_error", map[string]any{
		"Parameter": "cat",
	}))
	// 使用原始方法输出en_us的parameter_error,带参数模板
	fmt.Printf("user.parameter_error zh_cn is: %s\n", n.T("zh_cn", "user.parameter_error", map[string]any{
		"Parameter": "cat",
	}))
	// 构建作用域的translate
	scope := n.Locale("zh_cn").Scope("user")
	fmt.Printf("test zh_cn is: %s\n", scope.T("test"))
	fmt.Printf("parameter_error zh_cn is: %s\n", scope.T("parameter_error", map[string]any{
		"Parameter": "cat",
	}))
}

Documentation

Overview

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package i18n MIT License

# Copyright (c) 2024 go-fox Author https://github.com/go-fox/fox

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataSet

type DataSet struct {
	Locale    string
	Value     []byte
	Format    string
	Timestamp time.Time
}

DataSet translate dataset

type Decoder

type Decoder func(*DataSet, map[string]interface{}) error

Decoder is config decoder.

type I18n

type I18n interface {
	Locale(locale string) Locale
	Scope(scope string) Locale
	T(locale, key string, args ...interface{}) string
}

I18n i18n interface

func New

func New(opts ...Option) (I18n, error)

New create an i18n

type Locale

type Locale interface {
	Locale(locale string) Locale
	Scope(scope string) Locale
	T(key string, args ...any) string
}

Locale locale translate interface

type Merge

type Merge func(dst, src interface{}) error

Merge is i18n merge func.

type Option

type Option func(i *i18n)

Option create i18n option

func WithDebug

func WithDebug(debug bool) Option

WithDebug with debug

func WithDecoder

func WithDecoder(decoder Decoder) Option

WithDecoder with decoder

func WithMerge

func WithMerge(merge Merge) Option

WithMerge with merge

func WithParse

func WithParse(parser Parser) Option

WithParse with template parser

func WithResolver

func WithResolver(resolver Resolver) Option

WithResolver with resolver

func WithSources

func WithSources(sources ...Source) Option

WithSources with sources

type Parser

type Parser func(key, template string, data ...any) (string, error)

Parser is template parse func

type Reader

type Reader interface {
	Merge(...*DataSet) error
	Value(string) (Value, bool)
	Source() ([]byte, error)
	Resolve() error
}

Reader interface

type Resolver

type Resolver func(map[string]interface{}) error

Resolver resolve placeholder in i18n.

type Source

type Source interface {
	// Load load dataset
	Load() ([]*DataSet, error)
}

Source i18n source interface

func NewSource

func NewSource(path any) Source

NewSource file source

type Value

type Value interface {
	String() string
}

Value is value store

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL