l10n

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 4 Imported by: 8

README

Purpose

Provide support in localizing an Alexa skill.

  • clear and easy structure of translations (one file per locale encouraged)
  • simple "key" lookup that allows placeholders (using fmt.Sprintf)
  • register locales (translations), define fallback locales
  • separating logic from translations (logic/flow is in the code, e.g. which Intent uses which Slots)

What does l10n NOT provide or aim to support:

  • it does not aim to be feature complete (yet)

Usage

Default keys

see l10n.go

Default "postfix" for keys: _Samples for samples of an intent or slot. _Values for a type

Example:

see skill_test.go

if you use multiple locales, it's easiest to define your own keys:

// keys of the project
const(
	ByeBye       string = "byebye"
	StopTitle    string = "stop_title"
	Stop         string = "stop"
	GenericTitle string = "Alexa"

	// Intents
	DemoIntent                string = "DemoIntent"
	DemoIntentSamples         string = "DemoIntent_Samples"
	DemoIntentTitle           string = "DemoIntent_Title"
	DemoIntentText            string = "DemoIntent_Text"
	DemoIntentSSML            string = "DemoIntent_SSML"
)

Documentation

Overview

Package l10n provides locale helpers (LocaleRegistry) and predefined standard keys for Alexa skills

Index

Constants

View Source
const (
	KeySkillName                string = "SKILL_Name"
	KeySkillDescription         string = "SKILL_Description"
	KeySkillSummary             string = "SKILL_Summary"
	KeySkillExamplePhrases      string = "SKILL_ExamplePhrases"
	KeySkillKeywords            string = "SKILL_Keywords"
	KeySkillSmallIconURI        string = "SKILL_SmallIconURI"
	KeySkillLargeIconURI        string = "SKILL_LargeIconURI"
	KeySkillTestingInstructions string = "SKILL_TestingInstructions"
	KeySkillInvocation          string = "SKILL_Invocation"
	KeySkillPrivacyPolicyURL    string = "SKILL_PrivacyPolicyURL"
	KeySkillTermsOfUseURL       string = "SKILL_TermsOfUse"
	KeyPostfixSamples           string = "_Samples"
	KeyPostfixValues            string = "_Values"
	KeyPostfixTitle             string = "_Title"
	KeyPostfixText              string = "_Text"
	KeyPostfixSSML              string = "_SSML"
	// fallback, standard error.
	KeyErrorTitle string = "Error_Title"
	KeyErrorText  string = "Error_Text"
	KeyErrorSSML  string = "Error_SSML"
	// unknown error.
	KeyErrorUnknownTitle string = "Error_Unknown_Title"
	KeyErrorUnknownText  string = "Error_Unknown_Text"
	KeyErrorUnknownSSML  string = "Error_Unknown_SSML"
	// not found error (slot, intent, ...), see request.go.
	KeyErrorNotFoundTitle string = "Error_NotFound_Title"
	KeyErrorNotFoundText  string = "Error_NotFound_Text"
	KeyErrorNotFoundSSML  string = "Error_NotFound_SSML"
	// missing locale error.
	KeyErrorLocaleNotFoundTitle string = "Error_LocaleNotFound_Title"
	KeyErrorLocaleNotFoundText  string = "Error_LocaleNotFound_Text"
	KeyErrorLocaleNotFoundSSML  string = "Error_LocaleNotFound_SSML"
	// generic translation error.
	KeyErrorTranslationTitle string = "Error_Translation_Title"
	KeyErrorTranslationText  string = "Error_Translation_Text"
	KeyErrorTranslationSSML  string = "Error_Translation_SSML"
	// missing translation error.
	KeyErrorNoTranslationTitle string = "Error_NoTranslation_Title"
	KeyErrorNoTranslationText  string = "Error_NoTranslation_Text"
	KeyErrorNoTranslationSSML  string = "Error_NoTranslation_SSML"
	// missing placeholder in translation error.
	KeyErrorMissingPlaceholderTitle string = "Error_MissingPlaceholder_Title"
	KeyErrorMissingPlaceholderText  string = "Error_MissingPlaceholder_Text"
	KeyErrorMissingPlaceholderSSML  string = "Error_MissingPlaceholder_SSML"
	// default intents.
	KeyLaunchTitle string = "Launch_Title"
	KeyLaunchText  string = "Launch_Text"
	KeyLaunchSSML  string = "Launch_SSML"
	KeyHelpTitle   string = "Help_Title"
	KeyHelpText    string = "Help_Text"
	KeyHelpSSML    string = "Help_SSML"
	KeyStopTitle   string = "Stop_Title"
	KeyStopText    string = "Stop_Text"
	KeyStopSSML    string = "Stop_SSML"
	KeyCancelTitle string = "Cancel_Title"
	KeyCancelText  string = "Cancel_Text"
	KeyCancelSSML  string = "Cancel_SSML"
)

Default keys.

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the standard registry used.

Functions

func GetLocales

func GetLocales() map[string]LocaleInstance

GetLocales returns the locales registered in the DefaultRegistry.

func Register

func Register(locale LocaleInstance, opts ...RegisterFunc) error

Register registers a new Locale in the DefaultRegistry.

func SetDefault

func SetDefault(locale string) error

SetDefault sets the default locale in the DefaultRegistry.

Types

type Config

type Config struct {
	DefaultLocale bool
	FallbackFor   string
}

Config contains the options for Locale registration.

type Locale

type Locale struct {
	Name         string // de-DE, en-US, ...
	TextSnippets Snippets
	// contains filtered or unexported fields
}

Locale is a representation of keys in a specific language.

func NewLocale

func NewLocale(locale string) *Locale

NewLocale creates a new, empty locale.

func (*Locale) Get

func (l *Locale) Get(key string, args ...interface{}) string

Get returns the first translation.

func (*Locale) GetAll

func (l *Locale) GetAll(key string, args ...interface{}) []string

GetAll returns all translations.

func (*Locale) GetAny

func (l *Locale) GetAny(key string, args ...interface{}) string

GetAny returns a random translation.

func (*Locale) GetErrors

func (l *Locale) GetErrors() []error

GetErrors returns key lookup errors that occurred.

func (*Locale) GetName

func (l *Locale) GetName() string

GetName returns the name of the locale.

func (*Locale) ResetErrors

func (l *Locale) ResetErrors()

ResetErrors resets existing errors.

func (*Locale) Set

func (l *Locale) Set(key string, values []string)

Set sets the translations for a key.

type LocaleError

type LocaleError interface {
	error
	GetLocale() string
	GetKey() string
	GetPlaceholder() string
}

LocaleError defines the interface for locale errors.

type LocaleInstance

type LocaleInstance interface {
	GetName() string
	Set(key string, values []string)
	Get(key string, args ...interface{}) string
	GetAny(key string, args ...interface{}) string
	GetAll(key string, args ...interface{}) []string
	GetErrors() []error
	ResetErrors()
}

LocaleInstance is the interface for a specific locale.

func GetDefault

func GetDefault() LocaleInstance

GetDefault returns the default locale in the DefaultRegistry.

func Resolve

func Resolve(name string) (LocaleInstance, error)

Resolve returns the matching locale from the DefaultRegistry.

type LocaleRegistry

type LocaleRegistry interface {
	Register(locale LocaleInstance, opts ...RegisterFunc) error
	Resolve(locale string) (LocaleInstance, error)
	GetDefault() LocaleInstance
	SetDefault(locale string) error
	GetLocales() map[string]LocaleInstance
}

LocaleRegistry is the interface for an l10n registry.

func NewRegistry

func NewRegistry() LocaleRegistry

NewRegistry returns an empty Registry.

type MissingPlaceholderError

type MissingPlaceholderError struct {
	Locale      string
	Key         string
	Placeholder string
}

MissingPlaceholderError defines a generic not found error.

func (MissingPlaceholderError) Error

func (e MissingPlaceholderError) Error() string

Error returns a string representing the error including the key (and placeholder) missing.

func (MissingPlaceholderError) GetKey

func (e MissingPlaceholderError) GetKey() string

GetKey returns the key of the error.

func (MissingPlaceholderError) GetLocale

func (e MissingPlaceholderError) GetLocale() string

GetLocale returns locale of the error.

func (MissingPlaceholderError) GetPlaceholder

func (e MissingPlaceholderError) GetPlaceholder() string

GetPlaceholder returns the placeholder concerned.

type NoTranslationError

type NoTranslationError struct {
	Locale      string
	Key         string
	Placeholder string
}

NoTranslationError defines a missing translation error.

func (NoTranslationError) Error

func (n NoTranslationError) Error() string

Error returns a string of the error.

func (NoTranslationError) GetKey

func (n NoTranslationError) GetKey() string

GetKey returns the associated key.

func (NoTranslationError) GetLocale

func (n NoTranslationError) GetLocale() string

GetLocale returns the locale of the error.

func (NoTranslationError) GetPlaceholder

func (n NoTranslationError) GetPlaceholder() string

GetPlaceholder returns the placeholder concerned.

type RegisterFunc

type RegisterFunc func(cfg *Config)

RegisterFunc defines the functions to be passed to Register.

func AsDefault

func AsDefault() RegisterFunc

AsDefault registers the given Locale as the default.

type Registry

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

Registry is the Locale registry.

func (*Registry) GetDefault

func (r *Registry) GetDefault() LocaleInstance

GetDefault returns the default locale.

func (*Registry) GetLocales

func (r *Registry) GetLocales() map[string]LocaleInstance

GetLocales returns all registered locales.

func (*Registry) Register

func (r *Registry) Register(l LocaleInstance, opts ...RegisterFunc) error

Register registers a new locale and fails if it already exists.

func (*Registry) Resolve

func (r *Registry) Resolve(locale string) (LocaleInstance, error)

Resolve returns the Locale matching the given name or an error.

func (*Registry) SetDefault

func (r *Registry) SetDefault(locale string) error

SetDefault sets the default locale which must be registered.

type Snippets

type Snippets map[string][]string

Snippets is the actual representation of key -> array of translations in a locale.

func (Snippets) GetAll

func (s Snippets) GetAll(key string, args ...interface{}) ([]string, error)

GetAll returns all translations of the snippet.

func (Snippets) GetAny

func (s Snippets) GetAny(key string, args ...interface{}) (string, error)

GetAny returns a random translation for the snippet.

func (Snippets) GetFirst

func (s Snippets) GetFirst(key string, args ...interface{}) (string, error)

GetFirst returns the first translation for the snippet.

Jump to

Keyboard shortcuts

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