Commit eb36a8df authored by Boris Mühmer's avatar Boris Mühmer
Browse files

added I18N package

parent 5a5d0e99
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
package countries

import "errors"

var (
	// ErrorFunctionNotImplemented is returned when a function is not implemented.
	ErrorFunctionNotImplemented = errors.New("function not implemented")
)

// Countries holds mappings.
type Countries struct {
	data map[string]string
}

// New creates a new instance.
func New() *Countries {
	return &Countries{
		data: make(map[string]string),
	}
}

// Add ...
func (c *Countries) Add(country, description string) (bool, error) {
	c.data[country] = description
	return true, nil
}

pkg/i18n/i18n_test.go

0 → 100644
+57 −0
Original line number Diff line number Diff line
package i18n

import (
	"strings"
	"testing"

	"repositories.muehmer.net/bsmrgo/wizards/pkg/i18n/countries"
	"repositories.muehmer.net/bsmrgo/wizards/pkg/i18n/languages"
	"repositories.muehmer.net/bsmrgo/wizards/pkg/i18n/mappings"
	"repositories.muehmer.net/bsmrgo/wizards/pkg/i18n/translations"
)

func TestI18N(t *testing.T) {

	countries := countries.New()
	countries.Add("US", "United States")
	countries.Add("GB", "Great Britian")
	countries.Add("DE", "Germany")
	countries.Add("AT", "Austria")
	countries.Add("CH", "Swiss")

	languages := languages.New()
	languages.Add("en", "English")
	languages.Add("de", "Germany")

	mappings := mappings.New()
	mappings.Add("en", "US", "English for the United States", nil)
	m0, _ := mappings.Get("en", "US")
	mappings.Add("en", "GB", "English for Great Britian", m0)
	mappings.Add("de", "DE", "German for Germany", m0)
	m1, _ := mappings.Get("de", "DE")
	mappings.Add("de", "AT", "German for Austria", m1)
	mappings.Add("de", "CH", "German for Switzerland", m1)

	translations := translations.New(mappings)
	translations.Add("en", "US", "hello", "hello")
	translations.Add("en", "US", "world", "world")
	translations.Add("en", "US", "unknown", "unknown")
	translations.Add("de", "DE", "hello", "Hallo")
	translations.Add("de", "AT", "hello", "Servus")
	translations.Add("de", "DE", "world", "Welt")

	for _, x := range []struct {
		l, c, key, val string
	}{
		{l: "de", c: "AT", key: "hello", val: "Servus"},
		{l: "de", c: "CH", key: "hello", val: "Hallo"},
		{l: "de", c: "AT", key: "world", val: "Welt"},
		{l: "de", c: "AT", key: "unknown", val: "unknown"},
	} {
		m, _ := mappings.Get(x.l, x.c)
		r, _ := translations.Get(m, x.key)
		if strings.Compare(r, x.val) != 0 {
			t.Errorf("\"%s\"(%s_%s) is \"%s\", expected \"%s\"", x.key, x.l, x.c, r, x.val)
		}
	}
}
+26 −0
Original line number Diff line number Diff line
package languages

import "errors"

var (
	// ErrorFunctionNotImplemented is returned when a function is not implemented.
	ErrorFunctionNotImplemented = errors.New("function not implemented")
)

// Languages ...
type Languages struct {
	data map[string]string
}

// New creates a new instance.
func New() *Languages {
	return &Languages{
		data: make(map[string]string),
	}
}

// Add ...
func (l *Languages) Add(name, description string) (bool, error) {
	l.data[name] = description
	return true, nil
}
+75 −0
Original line number Diff line number Diff line
package mappings

import "errors"

var (
	// ErrorFunctionNotImplemented is returned when a function is not implemented.
	ErrorFunctionNotImplemented = errors.New("function not implemented")
	// ErrorNoMapping is returned when no mapping was found.
	ErrorNoMapping = errors.New("no mapping found")
	// ErrorNoDefaultMapping is returned when no default mapping was set.
	ErrorNoDefaultMapping = errors.New("no default mapping")
)

// Mapping ...
type Mapping struct {
	Language, Country string
}

// Value ...
type Value struct {
	Description string
	Fallback    *Mapping
}

// Mappings ...
type Mappings struct {
	base *Mapping
	data map[Mapping]Value
}

// New ...
func New() *Mappings {
	return &Mappings{
		data: make(map[Mapping]Value),
	}
}

// Add ...
func (m *Mappings) Add(language, country, description string, fallback *Mapping) (bool, error) {
	mapping := Mapping{Language: language, Country: country}
	if fallback == nil {
		m.base = &mapping
	}
	value := Value{Description: description, Fallback: fallback}
	m.data[mapping] = value
	return true, nil
}

// Get ...
func (m *Mappings) Get(language, country string) (*Mapping, error) {
	mapping := Mapping{Language: language, Country: country}
	_, valid := m.data[mapping]
	if !valid {
		return nil, ErrorNoMapping
	}
	return &mapping, nil
}

// GetFallback ...
func (m *Mappings) GetFallback(language, country string) (*Mapping, error) {
	key := Mapping{Language: language, Country: country}
	value, valid := m.data[key]
	if !valid {
		return nil, ErrorNoMapping
	}
	return value.Fallback, nil
}

// GetDefault ...
func (m *Mappings) GetDefault() (*Mapping, error) {
	if m.base == nil {
		return nil, ErrorNoDefaultMapping
	}
	return m.base, nil
}
+74 −0
Original line number Diff line number Diff line
package translations

import (
	"errors"

	"repositories.muehmer.net/bsmrgo/wizards/pkg/i18n/mappings"
)

var (
	// ErrorFunctionNotImplemented is returned when a function is not implemented.
	ErrorFunctionNotImplemented = errors.New("function not implemented")
	// ErrorNoTranslation is returned when no translation was found.
	ErrorNoTranslation = errors.New("no translation")
)

type key struct {
	language, country, name string
}

// Translations ...
type Translations struct {
	mappings *mappings.Mappings
	data     map[key]string
}

// New ...
func New(mappings *mappings.Mappings) *Translations {
	return &Translations{
		mappings: mappings,
		data:     make(map[key]string),
	}
}

// Add ...
func (t *Translations) Add(language, country, name, value string) (bool, error) {
	k := key{language: language, country: country, name: name}
	t.data[k] = value
	return true, nil
}

// Get ...
func (t *Translations) Get(mapping *mappings.Mapping, name string) (string, error) {
	var value string
	var valid bool
	k := key{
		language: mapping.Language,
		country:  mapping.Country,
		name:     name,
	}
	value, valid = t.data[k]
	if !valid {
		m, _ := t.mappings.GetFallback(mapping.Language, mapping.Country)
		k := key{
			language: m.Language,
			country:  m.Country,
			name:     name,
		}
		value, valid = t.data[k]
		if !valid {
			m, _ := t.mappings.GetDefault()
			k := key{
				language: m.Language,
				country:  m.Country,
				name:     name,
			}
			value, valid = t.data[k]
			if !valid {
				return "", ErrorNoTranslation
			}
		}
	}

	return value, nil
}