Git Product home page Git Product logo

msgcat's Introduction

msgcat

Message catalog to handle i18n of your messages and errors. msg-meow!

Description

msgcat allows to setup a yaml formatted, context driven message catalog, that is, is possible to setup different language files for the implementing project.

Since it uses context key "language" (configurable as needed) to get the current language, it's possible to get the language of a given request from start to finish, this is specially good for i18n on APIs :)

First thigs first

go get msgcat onto your project

go get github.com/LoopContext/msgcat

Create the resource files

The project layout should be like this by default (you can override the configuration of the catalog)

/ (project root)
|
 -resources
     |
      - messages
         |
          - en.yaml
         |
          - es.yaml

en.yaml

default:
  short: Unexpected error
  long: Unexpected message code [{{0}}] was received and was not found in this catalog
set:
  1:
    short: This is a message in english
  2:
    short: This is an error message
    long: Any variable can be injected here {{0}}

es.yaml

default:
  short: Error inesperado
  long: Error inesperado con el con el código [{{0}}] ha sido solicitado y no encontrado en este catálogo.
set:
  1:
    short: Este mensaje es en español

Once have the files have been created, it's time to code!

package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/loopcontext/msgcat"
)

func main() {
	ctx := context.WithValue(context.Background(), "language", "en")
	catalog, err := msgcat.NewMessageCatalog(msgcat.Config{
		// CtxLanguageKey: "lang",
		// ResourcePath: "path/to/resource",
	})
	if err != nil {
		fmt.Printf("%q",err)
	}
	printMessage(ctx, catalog.GetMessageWithCtx(ctx, 1))
	printMessage(ctx, catalog.GetMessageWithCtx(ctx, 2, 2*2))
	// The message can be get as a error
	printError(ctx, catalog.GetErrorWithCtx(ctx, 1))
	// The message can wrap another error
	printError(ctx, catalog.WrapErrorWithCtx(ctx, errors.New("This error is wrapped"), 1))

	ctx = context.WithValue(context.Background(), "language", "es")
	printMessage(ctx, catalog.GetMessageWithCtx(ctx, 1))
	printMessage(ctx, catalog.GetMessageWithCtx(ctx, 2)) // this will fail
	printError(ctx, catalog.GetErrorWithCtx(ctx, 1))
	printError(ctx, catalog.WrapErrorWithCtx(ctx, errors.New("This error is wrapped"), 1))
}

func printMessage(ctx context.Context, msg *msgcat.Message) {
	fmt.Printf("\nLanguage: %s\nShort: %s\nLong: %s\n", ctx.Value("language"), msg.ShortText, msg.LongText)
}

func printError(ctx context.Context, err error) {
	wrapped := errors.Unwrap(err)
	if wrapped != nil {
		err = wrapped
	}
	fmt.Printf("\nLanguage: %s\nError: %q\n", ctx.Value("language"), err)
}

This is the output:

Language: en
Short: This is a message in english
Long: 

Language: en
Short: This is an error message
Long: Any variable can be injected here 4

Language: en
Error: "This is a message in english"

Language: en
Error: "This error is wrapped"

Language: es
Short: Este mensaje es en español
Long: 

Language: es
Short: Error inesperado
Long: Error inesperado con el con el código [2] ha sido solicitado y no encontrado en este catálogo.

Language: es
Error: "Este mensaje es en español"

Language: es
Error: "This error is wrapped"

msgcat's People

Contributors

cmelgarejo 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.