Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example ? #27

Closed
Gys opened this issue Aug 4, 2015 · 9 comments · Fixed by #92
Closed

example ? #27

Gys opened this issue Aug 4, 2015 · 9 comments · Fixed by #92
Assignees
Milestone

Comments

@Gys
Copy link

Gys commented Aug 4, 2015

Hello

I am looking for a way to build translation into a web-based project. I came across this and it really looks impressive. However, I have no idea where to start; I am puzzled how the whole framework is to be integrated in a project. It would be great if there was a minimum sample project somewhere, showing how this is done ?

@nicksnyder
Copy link
Owner

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.

@Gys
Copy link
Author

Gys commented Aug 22, 2015

Hi Nick, I seem to have missed your reply earlier. The package is a good start indeed.

@Gys Gys closed this as completed Aug 22, 2015
@Gys
Copy link
Author

Gys commented Aug 22, 2015

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 ?

@nicksnyder
Copy link
Owner

@Gys some answers to your questions:

  1. If there is no translation for translationID, then the translationID itself is returned (added documentation in 5845346)
  2. 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?
  3. 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.

@Gys
Copy link
Author

Gys commented Aug 23, 2015

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 ?

@gobijan
Copy link

gobijan commented Jan 2, 2016

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),
    })

}

@gabrielsimoes
Copy link

Where the example reads i18n.IdentityTfunc, it should be i18n.IdentityTfunc()

@nicksnyder nicksnyder self-assigned this Apr 10, 2018
@nicksnyder
Copy link
Owner

v2 proposal includes an example web application. See #92

@nicksnyder nicksnyder mentioned this issue Apr 10, 2018
Merged
4 tasks
@nicksnyder
Copy link
Owner

I just tagged 2.0.0.beta.1. Please start using it and report any issues that you have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants