t

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

🚨 Time to Say Goodbye to t (Go Translate)

I regret to inform you that this project is being deprecated. This decision was not taken lightly, but after careful consideration, I have determined that it is in the best interests of the community and users to move on to more up-to-date solutions.

The main reason for this decision is that there are many excellent alternatives available that offer more robust and comprehensive i18n and l10n solutions.

I hope that you will continue to support me and my future endeavors, and I encourage you to explore the following alternatives:


PkgGoDev Build) Go Report Card

t helps you translate Go apps into multiple languages.

Features

  • Configurable
  • Strings with variables
  • Translation folders that is relative to module
  • Translation files in yaml format

Requirements

Getting Started

go get github.com/erdaltsksn/t
touch main.go

Directory Structure:

1 .
2 ├── translations
3 │   ├── en.yaml
4 │   └── tr.yaml
5 └── main.go

main.go:

package main

import (
	"fmt"

	"github.com/erdaltsksn/t"
	"golang.org/x/text/language"
)

func main() {
	t.Configure(t.Config{
		Language:         language.Turkish,
		FallbackLanguage: language.English,
		TranslationFolder: struct {
			Path     string
			Relative bool
		}{
			Path:     "/translations",
			Relative: true,
		},
	})

	fmt.Println(t.Translate("msgHello"))
	fmt.Println(t.Translate("msgMorning", "Adam"))
}

en.yaml and tr.yaml:

# en.yaml
msgHello: "Hello World"
msgMorning: "Good morning, %v"
# tr.yaml
msgHello: "Merhaba Dünya"
msgMorning: "Günaydın, %v"

Output:

Merhaba Dünya
Günaydın, Adam

Installation

go get github.com/erdaltsksn/t

Updating / Upgrading

go get -u github.com/erdaltsksn/t

Usage

t.Translate("keyText")
t.Translate("keyText", "Variable")
// Print in different language
message.NewPrinter(language.Chinese).Sprintf("keyText", "Variable-1", "var-2")

Check out examples directory for more.

Contributing

If you want to contribute to this project and make it better, your help is very welcome.

For more information, see Contributing Guide.

Security Policy

If you discover a security vulnerability within this project, please follow our Security Policy.

Code of Conduct

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.

Disclaimer

In no event shall we be liable to you or any third parties for any special, punitive, incidental, indirect or consequential damages of any kind, or any damages whatsoever, including, without limitation, those resulting from loss of use, data or profits, and on any theory of liability, arising out of or in connection with the use of this software.

Documentation

Overview

Package t provides a way to translate text in Go applications by using language files in YAML format. The package includes a function called `Translate` which takes a string of text and optional parameters, and returns a translated string based on the language specified in the package's configuration. The package also includes an HTTP middleware called AcceptLanguageMiddleware which sets the "Accept-Language" header in the incoming request.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptLanguageMiddleware

func AcceptLanguageMiddleware(next http.Handler) http.Handler

AcceptLanguageMiddleware sets `acceptLanguage` that stores language header.

func Configure

func Configure(c Config)

Configure configures the `t` package.

func Translate

func Translate(text string, params ...interface{}) string

Translate is used to make the translation using mapping specified in the `Config.TranslationFolder`. This uses `Config.Language` header if exists.

Types

type Config

type Config struct {
	Language          language.Tag
	FallbackLanguage  language.Tag
	TranslationFolder struct {
		Path     string
		Relative bool
	}
}

Config stores all configurations for `t` package.

Directories

Path Synopsis
examples
simple
Program simple is an example application for `t` go library.
Program simple is an example application for `t` go library.

Jump to

Keyboard shortcuts

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