poeditor

package module
v0.0.0-...-2acbe78 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2018 License: MIT Imports: 12 Imported by: 0

README

poeditor GoDoc

This is a delicious go API wrapper for POEditor. The project strives to convert the POEditor REST API to an idiomatic go API.

Getting started

Install the library with go get or dep

go get github.com/blacksails/poeditor
dep ensure -add github.com/blacksails/poeditor

Provide an API token and go nuts

poe := poeditor.New("YOUR API TOKEN")

// Get projects
ps, _ := poe.ListProjects()

// Export all languages in project folders
wd, _ := os.Getwd()
for _, p := range ps {
    pDir := filepath.Join(wd, "translations", strconv.Itoa(p.ID))
    os.MkdirAll(pDir)
    ls, _ := p.ListLanguages()
    for _, l := range ls {
        f, _ := os.Create(pDir, fmt.Sprintf("%s.po", l.Code))
        l.Export(poeditor.FileFormatPO, []string{}, []string{}, f)
    }
}

Wrapper completeness

All of the API endpoints have been implemented. A few of them lack proper testing. Personally I dont use all of them, so I am only using a few in production. If you find something that doesn't work please file an issue and I will try to make a fix asap. Pull requests are also very welcome.

Documentation

Index

Constants

View Source
const (
	// UploadTerms is a valid value of UploadOptions.Updating
	UploadTerms = "terms"
	// UploadTermsTranslations is a valid value of UploadOptions.Updating
	UploadTermsTranslations = "terms_translations"
	// UploadTranslations is a valid value of UploadOptions.Updating
	UploadTranslations = "translations"
)
View Source
const (
	// FileFormatPO specifies a .po file
	FileFormatPO = "po"
	// FileFormatPOT specifies a .pot file
	FileFormatPOT = "pot"
	// FileFormatMO specifies a .mo file
	FileFormatMO = "mo"
	// FileFormatXLS specifies an .xls file
	FileFormatXLS = "xls"
	// FileFormatCSV specifies a .csv file
	FileFormatCSV = "csv"
	// FileFormatRESW specifies an .resw file
	FileFormatRESW = "resw"
	// FileFormatRESX specifies an .resx file
	FileFormatRESX = "resx"
	// FileFormatAndroidStrings specifies strings should be in android format
	FileFormatAndroidStrings = "android_strings"
	// FileFormatAppleStrings specifies strings should be in apple format
	FileFormatAppleStrings = "apple_strings"
	// FileFormatXLIFF specifies an .xliff file
	FileFormatXLIFF = "xliff"
	// FileFormatProperties specifies a .propterties file
	FileFormatProperties = "properties"
	// FileFormatKeyValueJSON specifies a .json file in key value format
	FileFormatKeyValueJSON = "key_value_json"
	// FileFormatJSON specifies a .json file
	FileFormatJSON = "json"
	// FileFormatXMB specifies an .xmb file
	FileFormatXMB = "xmb"
	// FileFormatXTB specifies an .xtb file
	FileFormatXTB = "xtb"
)
View Source
const (
	// FilterTranslated filters terms in translated state
	FilterTranslated = "translated"
	// FilterUntranslated filters terms in untranslated state
	FilterUntranslated = "untranslated"
	// FilterFuzzy filters terms in fuzzy state
	FilterFuzzy = "fuzzy"
	// FilterNotFuzzy filters terms in not fuzzy state
	FilterNotFuzzy = "not_fuzzy"
	// FilterAutomatic filters terms in automatic state
	FilterAutomatic = "automatic"
	// FilterNotAutomatic filters terms in not automatic state
	FilterNotAutomatic = "not_automatic"
	// FilterProofread filters terms in proofread state
	FilterProofread = "proofread"
	// FilterNotProofread filters terms in not proofread state
	FilterNotProofread = "not_proofread"
)

Variables

View Source
var (
	// ErrorUploadUpdating is returned from Project.Upload when the value of
	// Updating is invalid
	ErrorUploadUpdating = errors.New("Updating must be one of terms, terms_translations or translations")
	// ErrorUploadLanguage is return when language code is missing
	ErrorUploadLanguage = errors.New("Language code is required when uploading translations")
	// ErrorUpdateFields is returned when passing invalid fields to Project.Update
	ErrorUpdateFields = errors.New("Tried to update invalid field. Valid fields are name, description, reference_language")
	// ErrTranslationInvalid is return when a Singular or Plural type is expected,
	// but something else is found
	ErrTranslationInvalid = errors.New("invalid translation type please use Singular or Plural")
)

Functions

This section is empty.

Types

type AvailableLanguage

type AvailableLanguage struct {
	Name string `json:"name"`
	Code string `json:"code"`
}

AvailableLanguage is a language supported by POEditor

type Contributor

type Contributor struct {
	Name        string
	Email       string
	Permissions []Permission
}

type CountResult

type CountResult struct {
	Parsed           int `json:"parsed"`
	Added            int `json:"added"`
	Deleted          int `json:"deleted"`
	WithAddedComment int `json:"with_added_comment"`
	Updated          int `json:"updated"`
}

CountResult is a part of UploadResult and returned directly from Project.Sync. It shows counts for uploaded/synced terms and translations

type Error

type Error struct {
	Status  string
	Code    string
	Message string
}

Error represents an error sent from the POEditor API

func (Error) Error

func (p Error) Error() string

type Language

type Language struct {
	Project *Project
	Code    string
}

Language represents a single language of a project

func (*Language) AddContributor

func (l *Language) AddContributor(name, email string) error

AddContributor adds a user as a language contributor

func (*Language) Delete

func (l *Language) Delete() error

Delete deletes the language

func (Language) Export

func (l Language) Export(fileFormat string, filters []string, tags []string, dest io.Writer) error

Export extracts the language in the given fileformat. For available file formats, see the FileFormat constants. Terms can be filtered using the Filter constants. Terms can also be filtered by tags.

func (*Language) ListContributors

func (l *Language) ListContributors() ([]Contributor, error)

ListContributors lists all contributors registered under the language

func (*Language) ListTerms

func (l *Language) ListTerms() ([]TermTranslated, error)

ListTerms returns all terms in the project along with the translations for the language

func (*Language) RemoveContributor

func (l *Language) RemoveContributor(email string) error

RemoveContributor removes a contributor from the language

func (*Language) Update

func (l *Language) Update(terms []TermTranslation) (CountResult, error)

Update inserts or overwrites translations for a language TODO: add fuzzy_trigger

type POEditor

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

POEditor is the main type used to interact with POEditor

func New

func New(apiToken string) *POEditor

New returns a new POEditor given a POEditor API Token

func (*POEditor) AddProject

func (poe *POEditor) AddProject(name, description string) (*Project, error)

AddProject creates a new project with the given name and description

func (*POEditor) AvailableLanguages

func (poe *POEditor) AvailableLanguages() ([]AvailableLanguage, error)

AvailableLanguages lists all languages supported by POEditor. This is handy when you want to look up a particular language code.

func (*POEditor) ListContributors

func (poe *POEditor) ListContributors() ([]Contributor, error)

ListContributors lists all contributors registered in POEditor

func (*POEditor) ListProjects

func (poe *POEditor) ListProjects() ([]*Project, error)

ListProjects lists all the projects that are accessable by the used APIKey

func (*POEditor) Project

func (poe *POEditor) Project(id int) *Project

Project returns a Project with the given id

func (*POEditor) ViewProject

func (poe *POEditor) ViewProject(id int) (*Project, error)

ViewProject returns project with given ID

type Permission

type Permission struct {
	ProjectID   string
	ProjectName string
	Type        string
}

type Plural

type Plural struct {
	One   string `json:"one"`
	Other string `json:"other"`
}

Plural is a plural translation

type Project

type Project struct {
	POEditor          *POEditor
	ID                int
	Name              string
	Description       string
	Public            int
	Open              int
	ReferenceLanguage string
	Terms             int
	Created           time.Time
}

Project represents a POEditor project

func (*Project) AddComments

func (p *Project) AddComments(comments []TermComment) (CountResult, error)

AddComments adds the given comments

func (*Project) AddContributor

func (p *Project) AddContributor(name, email string) error

AddContributor adds a user as a project admin

func (*Project) AddLanguage

func (p *Project) AddLanguage(code string) error

AddLanguage adds a new language to the project. See POEditor.AvailableLanguages for a list of supported language codes.

func (*Project) AddTerms

func (p *Project) AddTerms(terms []Term) (CountResult, error)

AddTerms adds the given terms to the project

func (*Project) Delete

func (p *Project) Delete() error

Delete does its thing

func (*Project) DeleteTerms

func (p *Project) DeleteTerms(terms []TermBase) (CountResult, error)

DeleteTerms deletes the given terms from the project

func (*Project) ListContributors

func (p *Project) ListContributors() ([]Contributor, error)

ListContributors lists all contributors registered under the project

func (*Project) ListLanguages

func (p *Project) ListLanguages() ([]Language, error)

ListLanguages lists all the available languages in the project

func (*Project) ListTags

func (p *Project) ListTags() ([]string, error)

ListTags returns a list of tags found on the project. This is not a standard API endpoint, but a useful helper never the less.

func (*Project) ListTerms

func (p *Project) ListTerms() ([]Term, error)

ListTerms returns all terms in the project

func (*Project) RemoveContributor

func (p *Project) RemoveContributor(email string) error

RemoveContributor removes a user as a project admin

func (*Project) Sync

func (p *Project) Sync(terms []Term) (CountResult, error)

Sync syncs project terms with the given list of terms.

CAUTION: this is a destructive operation. Any term not found in the input array will be deleted from the project.

func (*Project) Update

func (p *Project) Update(props map[string]string) (*Project, error)

Update updates the project according to the map[string]string.

...
p, err := p.Update(map[string]string{
	"name": "a project name",
	"description": "a project description"
	"reference_language": "a reference language code"
})

Omitted key value pairs are not updated. Only `name`, `description` and `reference_language` can be updated.

func (*Project) UpdateTerms

func (p *Project) UpdateTerms(terms []TermUpdate, fuzzyTrigger bool) (CountResult, error)

UpdateTerms lets you change the text, context, reference, plural and tags of terms. Setting fuzzyTrigger to true marks associated translations as fuzzy.

func (*Project) Upload

func (p *Project) Upload(reader io.Reader, options UploadOptions) (UploadResult, error)

Upload uploads terms

type Term

type Term struct {
	TermBase
	Plural    string       `json:"plural,omitempty"`
	Reference string       `json:"reference,omitempty"`
	Comment   string       `json:"comment,omitempty"`
	Tags      []string     `json:"tags,omitempty"`
	Created   poEditorTime `json:"created,omitempty"`
	Updated   poEditorTime `json:"updated,omitempty"`
}

Term is used when adding new terms, syncing or listing terms

type TermBase

type TermBase struct {
	Term    string `json:"term"`
	Context string `json:"context,omitempty"`
}

TermBase is used reference a particular term. This is only used publicly in term deletion, but it is utilzed heavliy in the code aswell.

type TermComment

type TermComment struct {
	TermBase
	Comment string `json:"comment"`
}

TermComment is used when adding a comment to a term

type TermTranslated

type TermTranslated struct {
	Term
	Translation Translation `json:"translation"`
}

TermTranslated is used when listing a project's terms along with translations for a language

type TermTranslation

type TermTranslation struct {
	TermBase
	Translation Translation `json:"translation,omitempty"`
}

TermTranslation is used when updating translations for a language

type TermUpdate

type TermUpdate struct {
	Term
	NewTerm    string `json:"new_term,omitempty"`
	NewContext string `json:"new_context,omitempty"`
}

TermUpdate is used when updating terms

type Translation

type Translation struct {
	Content   interface{}  `json:"content"`
	Fuzzy     int          `json:"fuzzy"`
	Proofread int          `json:"proofread"`
	Updated   poEditorTime `json:"updated"`
}

Translation is used to update translations in POEditor. The field Content must be either a string or a Plural type.

func (*Translation) UnmarshalJSON

func (t *Translation) UnmarshalJSON(bytes []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface

type UploadOptions

type UploadOptions struct {
	Updating       string
	Language       Language
	Overwrite      bool
	SyncTerms      bool
	Tags           []string
	ReadFromSource bool
	FuzzyTrigger   bool
}

UploadOptions specifies options for upload of a file

type UploadResult

type UploadResult struct {
	Terms        CountResult `json:"terms"`
	Translations CountResult `json:"translations"`
}

UploadResult is returned when uploading a file

Jump to

Keyboard shortcuts

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