i18n

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 4 Imported by: 0

README

Ssi Core / I18n

This package has been developed to localize the response messages in the microservice applications.

Works with .toml.

Create New I18n

type:

type I18n interface {
	LoadLanguages(localeDir string, languages ...string)
	Translate(key string, languages ...string) string
    TranslateWithParams(key string, params interface{}, languages ...string) string
    GetLanguagesInContext(c *fiber.Ctx) (string, string)
    I18nMiddleware(c *fiber.Ctx) error
}

func New(fb string) *I18n
package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
)

func main() {
    i := i18n.New("tr")
}

Load Languages

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
)

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    // ./locales/tr.toml
    // ./locales/en.toml
}

Translate

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
    "fmt"
)

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    fb := i.Translate("hello_world") // translate with fallback language
    fmt.Println(fb) // Merhaba, Dünya!
    
    tr := i.Translate("hello_world", "tr") // translate with tr language
    fmt.Println(tr) // Merhaba, Dünya!

    en := i.Translate("hello_world", "en") // translate with en language
    fmt.Println(en) // Hello, World!
}

Translate With Params

package main

import (
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
    "fmt"
)

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    fb := i.TranslateWithParams("hello_world", map[string]interface{}{"name": "Sami"}) // translate with fallback language
    fmt.Println(fb) // Merhaba, Sami!
    
    tr := i.TranslateWithParams("hello_world", map[string]interface{}{"name": "Sami"}, "tr") // translate with tr language
    fmt.Println(tr) // Merhaba, Sami!

    en := i.TranslateWithParams("hello_world", map[string]interface{}{"name": "Sami"}, "en") // translate with en language
    fmt.Println(en) // Hello, Sami!
}

Get Languages In Context

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/ssibrahimbas/ssi-core/pkg/http"
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
    "fmt"
)

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    h := http.New(i)
	h.App.Use(h.i18n.I18nMiddleware)
    h.App.Use(i.I18nMiddleware)
    h.App.Get("/", func(c *fiber.Ctx) error {
        fb, _ := h.i18n.GetLanguagesInContext(c) // get languages in context with fallback language
        fmt.Println(fb) // tr
        return c.SendString("Hello, World!")
    })
    h.Listen(":3000")
}

I18n Middleware

use i18n in fiber

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/ssibrahimbas/ssi-core/pkg/http"
    "github.com/ssibrahimbas/ssi-core/pkg/i18n"
    "log"
)

func main() {
    i := i18n.New("tr")
    i.LoadLanguages("./locales", "tr", "en")
    h := http.New(i)
	h.App.Use(h.i18n.I18nMiddleware)
    h.App.Get("/", func(c *fiber.Ctx) error {
        l, a := h.i18n.GetLanguagesInContext(c)
        return h.i18n.Translate("hello_world", l, a)
    })
    log.Fatal(h.Listen(":3000"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type I18n

type I18n struct {
	// contains filtered or unexported fields
}

func New

func New(fb string) *I18n

func (*I18n) GetLanguagesInContext

func (i *I18n) GetLanguagesInContext(c *fiber.Ctx) (string, string)

func (*I18n) I18nMiddleware

func (i *I18n) I18nMiddleware(c *fiber.Ctx) error

func (*I18n) LoadLanguages

func (i *I18n) LoadLanguages(ld string, languages ...string)

func (*I18n) Translate

func (i *I18n) Translate(key string, languages ...string) string

func (*I18n) TranslateWithParams

func (i *I18n) TranslateWithParams(key string, params interface{}, languages ...string) string

Jump to

Keyboard shortcuts

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