Code Monkey home page Code Monkey logo

echoi18n's Introduction

Echo i18n Middleware

Echo i18n Middleware is a middleware package for the Echo web framework that provides internationalization (i18n) support.

itpey i18n echo middleware Go Reference itpey i18n echo middleware license

Features

  • Seamless integration with Echo web framework.
  • Support for loading message bundles in various formats such as YAML.
  • Flexible language negotiation using HTTP Accept-Language header or query parameter.
  • Panic-free message localization with error handling.

Installation

go get github.com/itpey/echoi18n

Usage

package main

import (
	"net/http"

	"github.com/itpey/echoi18n"
	"github.com/labstack/echo/v4"
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
)

func main() {
	e := echo.New()
	e.Use(
		echoi18n.NewMiddleware(&echoi18n.Config{
			RootPath:        "./localize",
			AcceptLanguages: []language.Tag{language.Chinese, language.English},
			DefaultLanguage: language.Chinese,
		}),
	)
	e.GET("/", func(c echo.Context) error {
		localize, err := echoi18n.Localize(c, "welcome")
		if err != nil {
			return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
		}
		return c.String(http.StatusOK, localize)
	})
	e.GET("/:name", func(c echo.Context) error {
		return c.String(http.StatusOK, echoi18n.MustLocalize(c, &i18n.LocalizeConfig{
			MessageID: "welcomeWithName",
			TemplateData: map[string]string{
				"name": c.QueryParam("name"),
			},
		}))
	})
	e.Logger.Fatal(e.Start(":1323"))
}

Feedback and Contributions

If you encounter any issues or have suggestions for improvement, please open an issue on GitHub.

We welcome contributions! Fork the repository, make your changes, and submit a pull request.

License

echoi18n is open-source software released under the MIT License. You can find a copy of the license in the LICENSE file.

Author

echoi18n was created by itpey

echoi18n's People

Contributors

itpey avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.