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

Add HasFunc eqvlt to TFunc #56

Closed
mh-cbon opened this issue Nov 6, 2016 · 7 comments · Fixed by #92
Closed

Add HasFunc eqvlt to TFunc #56

mh-cbon opened this issue Nov 6, 2016 · 7 comments · Fixed by #92
Assignees
Milestone

Comments

@mh-cbon
Copy link
Contributor

mh-cbon commented Nov 6, 2016

Hi,

In one particular situation i want to translate some materials using a cascade of IDs.

Specifically for form error handling.

Reason for that is to be able to have a general translation, and if needed a set of more and more specific translations ID alternative for that specific (input + error) of that specific (form+input+error).

Right now i m doing it by trying to translate each possibility until it returns non empty value which is considered as the translation to consume.

I d prefer to use a HasFunc(ID) to remove that test on empty response.

what do you think ?

@nicksnyder
Copy link
Owner

So HasFunc would return a boolean true/false if a translation exists or not?

@mh-cbon
Copy link
Contributor Author

mh-cbon commented Nov 7, 2016

yes. I also have been thinking to a sort of CascadeTFunc(Ids []string, args ...interface{}) string which has the advantage to better optimize the resources to find and identify the translation, see https://github.com/nicksnyder/go-i18n/blob/master/i18n/bundle/bundle.go#L224.

@nicksnyder
Copy link
Owner

Couple questions:

Right now i m doing it by trying to translate each possibility until it returns non empty value which is considered as the translation to consume.
I d prefer to use a HasFunc(ID) to remove that test on empty response.

Tfunc should return the translation id, not an empty response if a translation doesn't exist. Can you share your code?

I also have been thinking to a sort of CascadeTFunc(Ids []string, args ...interface{}) string

If this function existed, would you still want/need HasFunc?

has the advantage to better optimize the resources to find and identify the translation

I don't quite see how there is a better way than checking the ids one by one in priority order.

@mh-cbon
Copy link
Contributor Author

mh-cbon commented Nov 10, 2016

1/ Right! mistake of mine.

func (f *FormTranslator) TError(formName string, fErr *FormValidationError) template.HTML {
  ids := []string{
    strings.ToLower(formName+"-"+fErr.Field+"-"+fErr.Tag),
    strings.ToLower(fErr.Ns+"-"+fErr.Tag),
    strings.ToLower(fErr.Field+"-"+fErr.Tag),
    strings.ToLower("form_error-"+fErr.Tag),
  }
  for _,id := range ids {
    m := f.T(id, fErr) //todo, escape fErr.Value.
    if m!="" && m!=id {
      return template.HTML(m)
    }
  }
  return template.HTML(f.missingTranslation(formName, fErr))
}

2/ If this function existed, would you still want/need HasFunc?

no

3/ there is this code that would be redundant.

4/ btw, language detection could be handled into TfuncAndLanguage, no ?

@nicksnyder nicksnyder added this to the v2 milestone Feb 14, 2018
@nicksnyder nicksnyder self-assigned this Apr 10, 2018
@nicksnyder
Copy link
Owner

I think this will be solved/obsolete with v2 proposed in #92

@nicksnyder nicksnyder mentioned this issue Apr 10, 2018
Merged
4 tasks
@mh-cbon
Copy link
Contributor Author

mh-cbon commented Apr 10, 2018

sure, thanks.

@mh-cbon mh-cbon closed this as completed Apr 10, 2018
@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.

2 participants