privet

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEFAULT_DELIMITER byte = '/'
)

Variables

This section is empty.

Functions

func Load

func Load() *ekaerr.Error

func Source

func Source(args ...interface{}) *ekaerr.Error

func Tr

func Tr(localeName, key string, args Args) string

Tr is an alias for LC(localeName).Tr(key, args). See LC() function and Locale.Tr() method for more details.

Types

type Args

type Args map[string]interface{}

Args represents map of arguments that are used for interpolating translated phrase.

type Client

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

TODO: comment

func (*Client) Default

func (c *Client) Default() *Locale

Default returns a Locale object that is marked as default Locale. If no Locale marked as default, nil is returned.

Reminder: It's safe to call Locale.Tr() of nil Locale. You just will get an error string message. Not panic, not UB.

func (*Client) LC

func (c *Client) LC(name string) *Locale

LC returns the requested Locale by its name.

If the Locale with the specified name doesn't exists (or if name is empty):

  • Default Locale is returned if any locale marked as default;
  • nil is returned if no locale is marked as default.

You may change that behaviour using:

  1. Config.LCEmptyLocaleNameAsNil set to true (false by default) if you want to get nil Locale if name is empty (even if any Locale is marked as default).

  2. Config.LCNotFoundLocaleAsNil set to true (false by default) if you want to get nil Locale if Locale with requested name not found (even if any Locale is marked as default).

func (*Client) Load

func (c *Client) Load() *ekaerr.Error

TODO: comment

func (*Client) Source

func (c *Client) Source(args ...interface{}) *ekaerr.Error

Source allows you to add locale content or path to it. Multiple calls are allowed or you can pass all arguments into one call.

Source(<content1>)                                   // (1)
Source(<content2>)                                   // (2)

is the same as

Source(<content1>, <content2>)                       // (3)

You can even combine the arrays of content and single data.

Source(<content1>, []{<content2>, <content3>})       // (4)

As you noticed, the examples above written at the near Golang pseudocode. That's because there is different entities may be used as locale source depends on type of argument.

First thing first, all arguments are passed and combined into one single array. Passed arrays also too.

Then the formed array's items are analysed independently, the whole set of sources is generating, step by step, moving forward to being ready to loaded by Load() call.

Argument's types and their meanings: (Keep in mind, that ALL OTHER ARGUMENT TYPES which not listed below ARE PROHIBITED and will lead to fast return error).

As we know from the above, there is only some "base" types and arrays are allowed. Base types are:

  • string (treated as path to either locale's directory or locale's one file),
  • []byte (treated as the content of locale's file).

Adding arrays to the list above and we've also get:

  • []string (treated as the array of either locale's directories, files or even mixed),
  • [][]byte (treated as the array of locale's files content).

The passed paths are analyzed as soon as possible during this call (thus you will get an error if path is not exist, access denied or something else) but the content will not be loaded until Load() call. Also for passed []byte (or [][]byte).

Keep in mind, Load() call flushes all pending sources. Thus you will need to re-register sources you want to use as source again after Load() call, if you need to call Load() more than one times (dunno why you even need that).

Path to directory or file might be absolute or relative. If relative it will converted to absolutely starting from the current work directory.

You may provide path to directory that contains sub-directories. In that case all these sub-directories will be scanned too recursively, till locale files found.

func (*Client) Tr

func (c *Client) Tr(localeName, key string, args Args) string

Tr is an alias for Client.LC(localeName).Tr(key, args). See LC() function and Locale.Tr() method for more details.

type Locale

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

Locale is a storage of all translated phrases for one language.

Getting locale by Client.LC() or Client.Default() allows you to get (from cache) Locale object, using which you may transform your translated key to the desired language's phrase.

WARNING! You must not instantiate this class manually! It's useless but safely. So you won't get panicked or UB. Manually instantiated Locale objects are considered not initialized and provides to you the same behaviour as if it'd be nil.

func Default

func Default() *Locale

func LC

func LC(name string) *Locale

LC returns the requested Locale by its name.

If the Locale with the specified name doesn't exists (or if name is empty):

  • Default Locale is returned if any locale marked as default;
  • nil is returned if no locale is marked as default.

You may change that behaviour using:

  1. Config.LCEmptyLocaleNameAsNil set to true (false by default) if you want to get nil Locale if name is empty (even if any Locale is marked as default).

  2. Config.LCNotFoundLocaleAsNil set to true (false by default) if you want to get nil Locale if Locale with requested name not found (even if any Locale is marked as default).

func (*Locale) MarkAsDefault

func (l *Locale) MarkAsDefault()

MarkAsDefault marks the current Locale object as a default Locale. If any Locale was marked as default Locale already, the will be overwritten.

Nil safe. If this method is called on nil object, there is no-op.

func (*Locale) Name

func (l *Locale) Name() string

Name returns the current Locale's name.

Returned name is always in "xx_YY" format, where:

  • xx is a lower case chars of language name ("en", "ru", "jp"),
  • YY is a upper case chars of country name ("US", "GB", "RU").

Nil safe. If this method is called on nil object, the empty string is returned.

func (*Locale) Tr

func (l *Locale) Tr(key string, args Args) string

Tr tries to get translated language phrase by the specified translation key and then tries to interpolate this phrase using passed args, if any.

Nil safe. If this method is called on nil object, the special string is returned.

Special returned strings. All of special returned strings has the same format:

       "i18nErr: <error_class>. Key: <translation_key>".
               <translation_key> is your translation key,
               <error_class> might be:

- _SPTR_LOCALE_IS_NIL:                Current Locale object is nil,
- _SPTR_TRANSLATION_KEY_IS_EMPTY:     Translation key is empty,
- _SPTR_TRANSLATION_KEY_IS_INCORRECT: Translation key is invalid (incorrect separator),
- _SPTR_TRANSLATION_NOT_FOUND:        Translation not found.

type SourceItem

type SourceItem struct {
	Type       SourceItemType
	Path       string
	LocaleName string
	// contains filtered or unexported fields
}

SourceItem is a type that represents one thing that will be used as a source locale data will be load from.

It may represent a file or a RAW data (content). Since we're supporting only YAML and TOML formats for now, SourceItem could be a link to either YAML or TOML file or just holds a content of that.

If SourceItem represents a file, Path contains a absolute filepath to that, content is nil. If SourceItem represents a some locale content, content contains that, but Path contain an absolute path of the Go source file that calls Source() with the line number.

SourceItem doesn't mean that source it holds is valid.

type SourceItemType

type SourceItemType uint8

SourceItemType allows you to know which data SourceItem holds: A file? A RAW data? Which format? YAML? TOML? SourceItemType allows SourceItem to get an answers about itself.

const (
	/*
		There are a constants of SourceItemType.
		In the source code you may determine using these constants what SourceItem is.
	*/
	SOURCE_ITEM_TYPE_FILE_YAML       SourceItemType = 100
	SOURCE_ITEM_TYPE_FILE_TOML       SourceItemType = 101
	SOURCE_ITEM_TYPE_CONTENT_UNKNOWN SourceItemType = 150
	SOURCE_ITEM_TYPE_CONTENT_YAML    SourceItemType = 151
	SOURCE_ITEM_TYPE_CONTENT_TOML    SourceItemType = 151
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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