Coder Social home page Coder Social logo

example ? about go-i18n HOT 9 CLOSED

nicksnyder avatar nicksnyder commented on May 18, 2024 4
example ?

from go-i18n.

Comments (9)

gobijan avatar gobijan commented on May 18, 2024 5

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.

gabrielsimoes avatar gabrielsimoes commented on May 18, 2024 1

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

from go-i18n.

nicksnyder avatar nicksnyder commented on May 18, 2024

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.

Gys avatar Gys commented on May 18, 2024

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

from go-i18n.

Gys avatar Gys commented on May 18, 2024

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.

nicksnyder avatar nicksnyder commented on May 18, 2024

@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.

from go-i18n.

Gys avatar Gys commented on May 18, 2024

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.

nicksnyder avatar nicksnyder commented on May 18, 2024

v2 proposal includes an example web application. See #92

from go-i18n.

nicksnyder avatar nicksnyder commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.