Code Monkey home page Code Monkey logo

messages's Introduction

Messages

This repository handles message translations using simple JSON files for Go (Golang) projects. The translations can be specific to a language or a combination of language and region.

Usage

Message extraction

Users can use the msgextractor tool to extract messages from your go source files. The messages will be added/removed to/from your translation files.

Note: Only messages using direct strings will be extracted. Messages containing variables will not be extracted. The msgextractor will remove manually added translations if they use variables. To avoid this, use direct strings in your messages.

msgextractor --translations path_to_translation_files --src path_to_go_source_files

// Use --default-language to set the default language as source for the translations. Missing translations for other languages will use this as the source.
msgextractor --translations path_to_translation_files --src path_to_go_source_files --default-language en

How to use it in code

// Create a new translator.
tr, err := messages.FromDir("dir")
if err != nil {
    // Handle error
    log.Fatalf("Failed to create translator: %v", err)
}

// Setting the language context per request.
// Parse the language from a user request. This can be from a header or user settings, for example.
ctx := context.Background()
ctx, err := messages.WithLanguage(ctx, "en")
if err != nil {
    // Handle error
    log.Fatalf("Failed to set language: %v", err)
}

// Translate the message.
// If the user requested en-US but you only have en translations available, the translator will use the en translations.
msg := tr.Translate(ctx, "welcome.message", map[string]any{"user": "wvell"})
fmt.Println(msg) // prints: Welcome wvell!

Translation files

Translation files are named based on the language or language-region codes:

  • en.json for English
  • en_US.json for American English
  • es.json for Spanish

Each translation file follows this format:

{
    "welcome.message": "Welcome :user!"
}

You can use a capitalized replacement to to capitalize the replacement value:

{
    "welcome.message": "Welcome :User!"
}

This will change the replacement value john to John.

Transformers

Transformers allow you to replace placeholder values, which is particularly useful for validation messages.

Using transformers allows you to reuse translations. The following example illutrates the required validation. Without transformers you would have to create a translation for each field(required.first_name, required.street).

Example

es.json

{
  "required" : ":Field es requerido"
  "@transform" : {
    "field" : {
      "first_name": "nombre",
      "street": "calle"
    }
  }
}
tr.Translate(ctx, "required", map[string]any{"field": "first_name"}) // Nombre es requerido
tr.Translate(ctx, "required", map[string]any{"field": "street"}) // Calle es requerido

As you can see this also takes the title case for the translation message into account.

messages's People

Contributors

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