language

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2020 License: MIT Imports: 2 Imported by: 18

README

Build Status Coverage Status GoDoc Go Report Card

go-localization

Description

go-localization is a Go package that provides tool for showing message based on language code. It has the following rules:

  • configuration message written in JSON with specific format.
  • if the message is found but the language code is not, will return by the default language code that was set up previously.
  • if the message is not found, will return the message.

Requirements

Go programming language

JSON Format

{
  "en": {
    "Good morning": "Good morning"
  },
  "id": {
    "Good morning": "Selamat pagi"
  },
  "jp": {
    "Good morning": "おはようございます"
  }
}

Getting Started

The go-localization package mainly includes a set of methods for managing the data. You use method language.New() to init the data, and you can call some method to bind config

cfg := language.New()
// path to look for the language file in
cfg.BindPath("./language.json")
// call multiple times to add many search paths
cfg.BindPath("/home/appname/language.json")
cfg.BindMainLocale("en")

If you want to user the message convert function, start with the init function

lang, err := cfg.Init()
if err != nil {
	panic(err)
}

// print some message in different languages
fmt.Println(lang.Lookup("en", "Good morning"))
fmt.Println(lang.Lookup("id", "Good morning"))
fmt.Println(lang.Lookup("jp", "Good morning"))
// if language not found, will return default language
fmt.Println(lang.Lookup("fr", "Good morning"))
// print unlisted message
fmt.Println(lang.Lookup("en", "Good night"))
Installation

Run the following command to install the package:

go get github.com/moemoe89/go-localization
Example

You can find a example for the implementation at example repository.

Documentation

Overview

Package go-localization will converted some string message to another language that defined before.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config has two property: path for save the config file json path and mainLocale for default language code.

func New

func New() *Config

Create a new Config with default value.

func (*Config) BindMainLocale

func (c *Config) BindMainLocale(mainLocale string)

Store the default language code value.

func (*Config) BindPath

func (c *Config) BindPath(path string)

Store the config file json path value.

func (Config) Init

func (c Config) Init() (*Config, error)

Store the message to map variable.

func (Config) Lookup

func (c Config) Lookup(locale, msg string) string

Lookup the destination message based on language code & message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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