Coder Social home page Coder Social logo

gopolyglot's Introduction

GoPolyglot

GoPolyglot is a Go (Golang) library designed to facilitate message translations and dynamic translations within Go applications. It provides an efficient way to manage multilingual support, particularly useful in web applications.

Features

  • Translation of response messages based on Response Code Identifiers (RC).
  • Support for dynamic translations tied to specific endpoints, allowing for real-time translation adjustments.
  • Flexible and customizable configuration for mapping database columns and crafting SQL queries tailored to your schema.
  • Framework-agnostic middleware capable of integrating with popular Go web frameworks such as Gin and Mux, making it versatile for various web applications.

Installation

To use GoPolyglot in your Go project, you need to install it using go get:

go install github.com/xans-me/GoPolyglot

Usage

Below is an example demonstrating how to integrate GoPolyglot with a Gin router. The setup for Mux follows a similar pattern, thanks to the agnostic nature of the provided middleware.

package main

import (
	"database/sql"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/xans-me/GoPolyglot"
	"github.com/xans-me/GoPolyglot/middleware"
	"net/http"
)

func main() {
	router := gin.Default()
	db, err := sql.Open("mysql", "your-database-connection-string")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	config := GoPolyglot.Config{
		// Configuration for your translation schema
	}

	translator := GoPolyglot.NewTranslator(db, config)
	router.Use(middleware.GinWrapper(middleware.AgnosticTranslationMiddleware(translator)))

	router.GET("/example", func(c *gin.Context) {
		// Your handler code
	})

	router.Run(":8080")
}

For Mux Users

If you are using Mux, the middleware can be directly applied without wrapping:

router := mux.NewRouter()
router.Use(middleware.AgnosticTranslationMiddleware(translator))

Configuration

Define your translation schema and queries in the Config struct:

config := GoPolyglot.Config{
ColumnConfig: GoPolyglot.ColumnConfig{
TableName:         "your_translation_table",
RCColumn:          "response_code",
TitleColumn:       "title",
DescriptionColumn: "description",
},
// Additional configuration options
}

translator := GoPolyglot.NewTranslator(db, config)

Middleware

The library includes middleware compatible with both Gin and Mux for translating response data based on the request's parameters and headers.

License

This library is released under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please create an issue or a pull request.

Author

Teuku Mulia Ichsan

Acknowledgments

Special thanks to the Go community.

gopolyglot's People

Watchers

Teuku Mulia Ichsan 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.