sgtn

package
v0.0.0-...-6f33ff2 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: EPL-2.0 Imports: 17 Imported by: 0

README

Singleton Library for Golang Client

Several client libraries are provided to support sending API requests to Singleton service.

Below are details on how to use the library for Golang client.

Prerequisites

  • Run the Singleton service by following the instructions in here.
  • Ensure the following are installed:

Get the client library

go get -u github.com/vmware/singleton@g11n-go-client

Here g11n-go-client is the branch name, it's better to use a tag name to get a stable release.

Sample code

package main

import (
	"fmt"
	"log"

	sgtn "github.com/vmware/singleton"
)

func main() {
	cfg, err := sgtn.LoadConfig("resource/conf/singletonconfig.json")
	if err != nil {
		log.Fatal(err)
	}
	sgtn.Initialize(cfg)
	trans := sgtn.GetTranslation()

	name, version, locale, component, key := "SgtnTest", "1.0.0", "zh-Hans", "sunglow", "application.title"

	// Get translation of a component
	compData, err := trans.GetComponentMessages(name, version, locale, component)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("\nComponent translation:\n%#v\n", compData)

	// Get translation of a string
	zhMsg, err := trans.GetStringMessage(name, version, locale, component, key)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("\nThe translation is '%s'.\n", zhMsg)
}

Existing features

  • Supported interfaces:
    • GetLocaleList: Get supported locale list
    • GetComponentList: Get supported component list
    • GetStringMessage: Get a message with optional arguments. Fallback to default locale is provided.
    • GetComponentMessages: Get messages of a component. Fallback to default locale is NOT provided.
  • Provide cache management as well as cache registration.
  • Support fallback to local bundles when failing to get from server
  • Support fallback to default locale when failing to get a string message of a nondefault locale.

Upcoming features

Request for contributions from the community

Documentation

Overview

stgn ...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize(cfg *Config)

Initialize initialize the client

func RegisterCache

func RegisterCache(c Cache)

RegisterCache Register cache implementation. There is a default implementation

func RegisterSource

func RegisterSource(name, version string, sources []ComponentMsgs)

RegisterSource is the way sending source strings to Singleton client programmatically

func SetHTTPHeaders

func SetHTTPHeaders(h map[string]string) error

SetHTTPHeaders Set customized HTTP headers

func SetLogger

func SetLogger(l Logger)

SetLogger Set a global logger. There is a default console logger

Types

type Cache

type Cache interface {
	// Get
	Get(key interface{}) (value interface{}, found bool)

	// Set
	Set(key interface{}, value interface{})
}

Cache the interface of Cache

type ComponentMsgs

type ComponentMsgs interface {
	// Get Get a message by key
	Get(key string) (value string, found bool)

	Set(key, value string)

	Component() string

	Locale() string

	Size() int

	Range(func(key, value string) bool)

	Clone() ComponentMsgs
}

ComponentMsgs The interface of a component's messages

type Config

type Config struct {
	ServerURL         string `json:"online_service_url"`
	LocalBundles      string `json:"offline_resources_base_url"`
	LocalSourceBundle string `json:"source_bundle"`
	DefaultLocale     string `json:"default_locale"`
	SourceLocale      string `json:"source_locale"`
}

Config Singleton configuration

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig Create a new Singleton configuration instance

type Logger

type Logger interface {
	Debug(message string)
	Info(message string)
	Warn(message string)
	Error(message string)
}

Logger The logger interface

type MapComponentMsgs

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

MapComponentMsgs is for a ComponentMsgs containing map

func NewMapComponentMsgs

func NewMapComponentMsgs(messages map[string]string, locale, component string) *MapComponentMsgs

NewMapComponentMsgs is to create a new ComponentMsgs easily

func (*MapComponentMsgs) Clone

func (d *MapComponentMsgs) Clone() ComponentMsgs

Clone the component

func (*MapComponentMsgs) Component

func (d *MapComponentMsgs) Component() string

Component returns the component name

func (*MapComponentMsgs) Get

func (d *MapComponentMsgs) Get(key string) (value string, found bool)

Get returns a message by key

func (*MapComponentMsgs) Locale

func (d *MapComponentMsgs) Locale() string

Locale returns the locale of component

func (*MapComponentMsgs) Range

func (d *MapComponentMsgs) Range(f func(key, value string) bool)

Range traverses the messages

func (*MapComponentMsgs) Set

func (d *MapComponentMsgs) Set(key, value string)

Set is to save a key-message pair

func (*MapComponentMsgs) Size

func (d *MapComponentMsgs) Size() int

Size returns the number of messages

type Translation

type Translation interface {
	// GetLocaleList Get locale list
	GetLocaleList(name, version string) ([]string, error)

	// GetComponentList Get component list
	GetComponentList(name, version string) ([]string, error)

	// GetStringMessage Get a message with optional arguments
	GetStringMessage(name, version, locale, component, key string, args ...string) (string, error)

	// GetComponentMessages Get component messages
	GetComponentMessages(name, version, locale, component string) (ComponentMsgs, error)

	// GetComponentsMessages Get messages of multiple components
	GetComponentsMessages(name, version string, locales, components []string) ([]ComponentMsgs, error)
}

Translation interface of translation

func GetTranslation

func GetTranslation() Translation

GetTranslation Get translation instance

Jump to

Keyboard shortcuts

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