Skip to content

Small library that brings AndroidStudio-like string resources system in Go.

License

Notifications You must be signed in to change notification settings

Vinetwigs/stres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card GitHub code size in bytes GitHub go.mod Go version GitHub last commit stars - stres forks - stres License issues - vilmos

stres - Android Studio string resources in Go

A simple and easy-to-use library to import and export string resources into your Go applications just like you would do in Android Studio. Useful to separate business logic from UI texts or to handle string translations.

Table of contents

References

Back to top

Prerequisites

  • Make sure you have at least installed Go v1.17.

Back to top

Installing

Install module

go get github.com/Vinetwigs/stres

Back to top

Import in your project

import("github.com/Vinetwigs/stres")

Back to top

Documentation

Types

Plural

type Plural struct {
	XMLName xml.Name      `xml:"plurals" json:"plurals" yaml:"plurals" toml:"plurals" watson:"plurals" msgpack:"plurals"`
	Name    string        `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name" msgpack:"name"`
	Items   []*PluralItem `xml:"item" json:"items" yaml:"items,flow" toml:"items,multiline" watson:"items" msgpack:"items,as_array"`
}

Back to top

PluralItem

type PluralItem struct {
	XMLName  xml.Name `xml:"item" json:"item" yaml:"item" toml:"item" watson:"item" msgpack:"item"`
	Quantity string   `xml:"quantity,attr" json:"quantity" yaml:"quantity" toml:"quantity" watson:"quantity" msgpack:"quantity"`
	Value    string   `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value" msgpack:"value"`
}

Back to top

Item

type Item struct {
	XMLName xml.Name `xml:"item" json:"item" yaml:"item" toml:"item" watson:"item" msgpack:"item"`
	Value   string   `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value" msgpack:"value"`
}

Back to top

StringArray

type StringArray struct {
	XMLName xml.Name `xml:"string-array" json:"string-array" yaml:"string-array" toml:"string-array" watson:"string-array" msgpack:"string-array"`
	Name    string   `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name" msgpack:"name"`
	Items   []*Item  `xml:"item" json:"items" yaml:"items,flow" toml:"items,multiline" watson:"items" msgpack:"items,as_array" `
}

Back to top

String

type String struct {
	XMLName xml.Name `xml:"string" json:"string" yaml:"string" toml:"string" watson:"string" msgpack:"string"`
	Name    string   `xml:"name,attr" json:"name" yaml:"name" toml:"name" watson:"name" msgpack:"name"`
	Value   string   `xml:",innerxml" json:"value" yaml:"value" toml:"value" watson:"value" msgpack:"value"`
}

Back to top

Nesting

type Nesting struct {
	XMLName      xml.Name       `xml:"resources" json:"resources" yaml:"resources" toml:"resources" watson:"resources" msgpack:"resources"`
	Strings      []*String      `xml:"string" json:"string" yaml:"string,flow" toml:"string,multiline" watson:"string" msgpack:"string,as_array"`
	StringsArray []*StringArray `xml:"string-array" json:"string-array" yaml:"string-array,flow" toml:"string-array,multiline" watson:"string-array" msgpack:"string-array,as_array"`
	Plurals      []*Plural      `xml:"plurals" json:"plurals" yaml:"plurals,flow" toml:"plurals,multiline" watson:"plurals" msgpack:"plurals,as_array"`
}

Back to top

CreateResourceFile

Creates strings resource file in "strings" directory, throws an error otherwise. Takes a FileType parameter to specify strings file format.

file, err := stres.CreateXMLFile()

Parameter Type Description
t types.FileType enum value to specify file format

Back to top

DeleteResourceFile

Deletes resource file if exists, throws an error otherwise. Uses setted resource file extension.

err := stres.DeleteXMLFile()

Back to top

LoadValues

Loads values from strings file into internal dictionaries. Needs to be invoked only one time (but before getting strings values).Takes a FileType parameter to specify strings file format.

err := stres.LoadValues()

Back to top

Parameter Type Description
t types.FileType enum value to specify file format

SetResourceType

Used to specify string file extension. If t is a wrong FileType, sets resource type to XML by default.

stres.SetResourceType(stres.WATSON)

Parameter Type Description
t types.FileType enum value to specify file format

Back to top

NewString

Adds a new string resource to resource file. Throws an error if the chosen name is already inserted or it is an empty string. Used for programmatic insertion (manual insertion recommended).

String, err := stres.NewString("name", "value")

Parameter Type Description
name string unique name to identify the string
value string string to associate to the given name

Returns String instance and error.

Back to top

NewStringArray

Adds a new string-array resource to resource file. Throws an error if the chosen name is already inserted or it is an empty string. Used for programmatic insertion (manual insertion recommended).

strArr, err := stres.NewStringArray("name", []string{"value1","value2",...})

Parameter Type Description
name string unique name to identify the string
values []string array of strings to associate to the given name

Returns StringArray instance and error.

Back to top

NewQuantityString

Adds a new quantity string resource to resource file. Throws an error if the chosen name is already inserted or it is an empty string. The function uses only the first 5 values in the array. The first value is assigned to "zero" quantity. The second value is assigned to "one" quantity. The third value is assigned to "two" quantity. The fourth value is assigned to "few" quantity. The fifth value is assigned to "more" quantity. Used for programmatic insertion (manual insertion recommended).

qntStr, err := stres.NewQuantityString("name", []string{"zero","one", "two", ...})

Parameter Type Description
name string unique name to identify the string
values []string array of strings for quantities to associate to the given name

Returns Plural instance and error.

Back to top

SetFewThreshold

Sets the threshold for "few" values in quantity strings.When getting quantity strings values, the function checks if the given count is less OR EQUAL to this value.(default value: 20)

stres.SetFewThreshold(25)

Parameter Type Description
value int new value for 'few' threshold

Returns Plural instance and error.

Back to top

GetString

Returns the string resource's value with the given name. If not exists, returns empty string.

str := GetString("name")

Parameter Type Description
name string unique name given to the corresponding string

Returns a string.

Back to top

GetArrayString

Returns the string-array resource's values with the given name. If not exists, returns nil.

strArr := GetStringArray("name")

Parameter Type Description
name string unique name given to the corresponding string-array

Returns an array of strings.

Back to top

GetQuantityString

Returns the quantity string resource's corresponding string value based on the value of the given count parameter. If the plural is not found, returns an empty string.

strArr := GetQuantityString("name", 10)

Parameter Type Description
name string unique name to identify the string
count int quantity to fetch the corresponding string

Returns a string.

Back to top

Contributors