i18n

package module
v0.0.0-...-69b471e Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: MIT Imports: 5 Imported by: 0

README

i18n

Minimalistic internationalizing helper.

Installation

  • Install package

    go get git.sr.ht/~kyoto-framework/i18n
    
  • Parse translation files from specified directory

    i18n.Parse("./i18n")
    
  • (Optional) Attach template functions

      // If you're using kyoto framewrok
      func FuncMap() template.FuncMap {
          return render.ComposeFuncMap(
              i18n.FuncMap(),    // i18n functions
              ...
          )
      }
    
      // If you're using i18n separately
      func AttachI18N(fmap template.FuncMap) {
          for k, v := range i18n.FuncMap() {
              fmap[k] = v
          }
      }
    

Usage

First, you'll have to create translation files.
This module uses structured .yaml to define such files. We call this files "pages".

Your pages should be structured as follows:

<language-code-1>:
    <group>:
        <key>: <value>

<language-code-2>:
    <group>:
        <key>: <value>

Please note, file names also matter. It will be used as a translation path, as well as a group and key.

To use specified translations, you have i18n.TranslateStatic(lang, page, group, key) Go function and {{ translateStatic lang page group key }} template function.

Example:

lang := "en"
content := i18n.TranslateStatic(lang, "index", "home", "content")

This module also provides a way to extract translations from dynamic containers (Go maps and structs). To do this, you can use i18n.TranslateDynamic(lang, container, field) Go function and {{ translateDynamic lang container field }} template function, where container is a Go map or struct. Field name will be chosen based on the language provided. In case of default language ("en" by default), value will be fetched according to the field name. In case of non-default language, value will be fetched according to the field name + language code (f.e. ContentRU, Content_ru or content_ru).

Example:

data := map[string]string{"Content": "Hello, world!", "ContentES": "Hola, mundo!"}
contenten := i18n.TranslateDynamic("en", data, "Content") // "Hello, world!"
contentes := i18n.TranslateDynamic("es", data, "Content") // "Hola, mundo!"

Motivation

  • Almost every solution we found have really overcomplicated usage for our simple needs (take a needed string from a file, accoring to provided language)
  • Most of solutions are not providing any template functions
  • There are no functions to dynamically take translated value from a struct or map

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = "en"

Default language configuration

Functions

func Clean

func Clean()

Clean is a function for global storage cleanup

func FuncMap

func FuncMap() template.FuncMap

func Parse

func Parse(dirpath string)

Parse is a function for parsing translates according to provided directory with structured files into global storage

func TranslateDynamic

func TranslateDynamic(lang string, container any, field string) string

TranslateDynamic extracts proper field from container according to provided language

func TranslateStatic

func TranslateStatic(lang, page, group, key string) string

TranslateStatic takes static translation from provided path

Types

type File

type File map[string]Language

type Language

type Language map[string]Translates

type Translates

type Translates map[string]string

Jump to

Keyboard shortcuts

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