Skip to content

🌍 A translation system for embedded go application (such as webassembly)

License

Notifications You must be signed in to change notification settings

cjtoolkit/translate-gen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Translate Gen

A working prototype translation system that relies purely on constant variable, package namespace, maps and go generate.

A simple translation system that nicely suitable for practically embedded systems such as docker (IMO) and webassembly.

Installation

$ go get -u github.com/cjtoolkit/translate-gen

Example

Input

base.toml

[base]
language = "English"
createMap = true # Optional

[[base.value]]
const = "Hello"
value = "Hello World"

[[base.value]]
const = "Name"
value = "My name is %s"

french.toml

[[import]] # Optional
path = "net/http"
alias = "httpStuff" # Optional

[translation]
language = "French"
functionName = "French"

[[translation.value]]
const = "Hello"
value = "Bonjour le monde"

[[translation.value]]
const = "Name"
value = "Mon nom est %s"

[[translation.value]]
const = "httpStuff.StatusNotFound"
value = "Pas trouvé"

Generate

generate.go

//go:generate translate-gen example base.toml french.toml

package example

Than run go generate generate.go

Output

base.go

// Code generated by translate-gen. DO NOT EDIT.
// Source: locale/base.toml

package example

// English
const (
	Hello = `Hello World`
	Name  = `My name is %s`
)

// English Map
func BaseMap() map[interface{}]string {
	return map[interface{}]string{
		Hello: Hello,
		Name:  Name,
	}
}

french.go

// Code generated by translate-gen. DO NOT EDIT.
// Source: locale/french.toml

package example

import (
	httpStuff "net/http"
)

// French
func French() map[interface{}]string {
	return map[interface{}]string{
		Hello: `Bonjour le monde`,
		Name:  `Mon nom est %s`,
		httpStuff.StatusNotFound: `Pas trouvé`,
	}
}

It's basically generate go code from toml.

Note

It's will replace backtick ( ` ) with a single quote ( ' ) for values.

There is no field validation, I omitted that for flexibility. Just be extra careful.

About

🌍 A translation system for embedded go application (such as webassembly)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published