Comments (9)
Here is an example using two language files de-de.all.json and the default en-us.all.json:
package main
import (
"os"
"text/template"
"github.com/nicksnyder/go-i18n/i18n"
)
var funcMap = map[string]interface{}{
"T": i18n.IdentityTfunc,
}
var tmpl = template.Must(template.New("").Funcs(funcMap).Parse(`
{{T "program_greeting"}}
{{T "person_greeting" .}}
{{T "your_unread_email_count" 0}}
{{T "your_unread_email_count" 1}}
{{T "your_unread_email_count" 2}}
{{T "person_unread_email_count" 0 .}}
{{T "person_unread_email_count" 1 .}}
{{T "person_unread_email_count" 2 .}}
`))
func main() {
i18n.MustLoadTranslationFile("en-us.all.json")
i18n.MustLoadTranslationFile("de-de.all.json")
T, _ := i18n.Tfunc("en-US")
tmpl.Funcs(map[string]interface{}{
"T": T,
})
tmpl.Execute(os.Stdout, map[string]interface{}{
"Person": "Bob",
"Timeframe": T("d_days", 1),
})
T, _ = i18n.Tfunc("de-DE")
tmpl.Funcs(map[string]interface{}{
"T": T,
})
tmpl.Execute(os.Stdout, struct {
Person string
Timeframe string
}{
Person: "Bob",
Timeframe: T("d_days", 1),
})
}
from go-i18n.
Where the example reads i18n.IdentityTfunc
, it should be i18n.IdentityTfunc()
from go-i18n.
I agree a sample project would be useful.
Did you read the godoc for the goi18n command and i18n package? There are code examples there which should help.
Basically just use the i18n package like you would use any other go package. It assumes that you have strings stored in a json file somewhere.
The goi18n command helps you manage your json translation files.
from go-i18n.
Hi Nick, I seem to have missed your reply earlier. The package is a good start indeed.
from go-i18n.
I still think the package needs more explaining for slow learning people like me...
For example:
TranslateFunc returns the translation of the string identified by translationID.
So what happens if the translationID was not in the file ? I would prefer a list of those would be dumped somewhere so I can build a not yet existing translation of an existing package without going through all the files looking for strings that need translation.
If translationID is a plural form, then the first variadic argument must be an integer type (int, int8, int16, int32, int64) or a float formatted as a string (e.g. "123.45"). The second variadic argument may be a map[string]interface{} or struct that contains template data.
In what way is the translationID recognized as being a plural form ?
from go-i18n.
@Gys some answers to your questions:
- If there is no translation for translationID, then the translationID itself is returned (added documentation in 5845346)
- For the sake of argument, let's assume the main language for your app is English. When you add a new string to your application, you will create an id for it like "mainPageTitle". If you don't remember to add an English translation in your translation file (e.g. en-US.json), then it will be quite obvious when you run the app because you will see an ugly message id and not an English string. If you do add an English translation, then the goi18n command line tool can help you scan each other locale you support for strings which are in the en-US.json file but haven't been translated into your other locales. Make sense?
- If the second argument to the tfunc has type int, int8, int16, int32, int64, or string, then it is a plural form. Looking at the examples should help.
from go-i18n.
If the second argument to the tfunc has type int, int8, int16, int32, int64, or string, then it is a plural form. Looking at the examples should help.
I saw it but was not sure if it was just coincidence. Especially as the doc seemed to indicate the plural form is (also) in the translationID itself. Does that mean the plural should always be used inside the template as '.Count' and not something else ?
from go-i18n.
v2 proposal includes an example web application. See #92
from go-i18n.
I just tagged 2.0.0.beta.1. Please start using it and report any issues that you have.
from go-i18n.
Related Issues (20)
- How to write a library module designed to be consumed by clients requiring i18n support?
- go get -u "github.com/nicksnyder/go-i18n/v2/i18n" # cd /root/go/GOCODE/src/github.com/nicksnyder/go-i18n; git pull --ff-only HOT 2
- Please explain how the user's current language is selected at runtime
- Is there are way to properly handle translation based on gender HOT 1
- Error handling is faulty HOT 3
- Setting the delimiters globally? HOT 2
- How to generate translations for the toml files HOT 3
- Upgrade golang.org/x/text v0.3.8 HOT 1
- `goi18n extract` doesn't seem to work for source file that use generics HOT 3
- Cound you export this function or when translation has error get the original value HOT 7
- Hook to commonly catch errors HOT 4
- Just a review
- Easy wrapper around this library HOT 1
- List fitting problem HOT 3
- merging new translation into existing translations omits the hashes
- [Question] Is there any way for reverse lookup in bundle? HOT 1
- Upgrade golang.org/x/text HOT 1
- Breaks on key description HOT 1
- Expose fields (or add access methods) of MessageNotFoundErr HOT 4
- Support template.Options for setting up missing key behavior HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-i18n.