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

Help Using Funcs #206

Closed
jamesgoodhouse opened this issue Dec 20, 2019 · 4 comments
Closed

Help Using Funcs #206

jamesgoodhouse opened this issue Dec 20, 2019 · 4 comments

Comments

@jamesgoodhouse
Copy link

jamesgoodhouse commented Dec 20, 2019

I have setup a LocalizeConfig to include a Funcs definition. However, it's unclear how those are to be used. When I attempt to use a function I've defined in a template, I get a panic saying it is not defined.

Is there additional documentation on how one uses these?

@nicksnyder
Copy link
Owner

There isn't any additional documentation right now. The Funcs you provide are forwarded to .Funcs on text/template here. It is definitely possible there is a bug; I just realized there are no unit tests to cover this. If you are seeing a panic can you post a stack trace?

@patgrasso
Copy link
Contributor

@nicksnyder I've also encountered a panic when running goi18n extract with Funcs provided. I haven't delved too deep into why it happens, but it appears to stem from this line in extractStringLiteral(). An instance of *ast.Ident's Obj field is used without a nil pointer check.

Here's a simple case that triggers the problem.

// funcs.go
package bug

import "text/template"

var Funcs = template.FuncMap{
	"noop": func(x interface{}) interface{} {
		return x
	},
}
// render.go
package bug

import "github.com/nicksnyder/go-i18n/v2/i18n"

func Render(m *i18n.Message) {
	l := i18n.NewLocalizer(nil, "en")
	l.Localize(&i18n.LocalizeConfig{
		DefaultMessage: m,
		Funcs: Funcs,        // <- note, Funcs is define in another file
	})
}

However, it works just fine if I define Funcs in the same file as where it's used.

// render.go
package bug

import (
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"text/template"
)

var Funcs = template.FuncMap{
	"noop": func(x interface{}) interface{} {
		return x
	},
}

func Render(m *i18n.Message) {
	l := i18n.NewLocalizer(nil, "en")
	l.Localize(&i18n.LocalizeConfig{
		DefaultMessage: m,
		Funcs: Funcs,
	})
}

It seems like a reasonable solution might be to just return from extractStringLiteral if the identifier's Obj is nil.

Would be happy to open a PR with a fix & a corresponding test case if you agree!

@patgrasso
Copy link
Contributor

patgrasso commented Feb 28, 2020

Also, I'm not 100% sure if this problem is the same as the one jamesgoodhouse is seeing. It seems like it might be, but if you want me to open a separate issue for this let me know.

@nicksnyder
Copy link
Owner

@patgrasso Looks like you found a bug, thanks! Yes please open a PR if you can to fix this (and include tests).

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

No branches or pull requests

3 participants