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

Support for message key prefix #21

Closed
wants to merge 2 commits into from
Closed

Conversation

fabiancz
Copy link

This feature simplify writing of translate keys in templates. Example of Nette template:

{block #content}
{translatePrefix 'messages.example.'}
    <h1>{_title}</h1>
    <h2>{_subtitle}</h2>
    <p>{_some_text}</p>
{/translatePrefix}
{/block}

messages.en_EN.neon file:

example:
    title: "Title of the page"
    subtitle: "Subtitle of the page"
    some_text: "Lorem ipsum sil dolor amet .."

You appreciate this, when you'r preparing large Nette templates for translation. Writing/copying same prefix of keys is really annoying.

$me->addMacro(
'translatePrefix',
'$presenter->translator->setPrefix(%node.args)',
'$presenter->translator->resetPrefix()'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should nott be here $template instead of $presenter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With $template it throws: "The variable 'translator' does not exist in template."

You'd have to add this variable somewhere in BasePresenter createTemplate():

$template->translator = $this->translator;

And I think this is unnecessary step.

@stekycz
Copy link

stekycz commented Dec 17, 2013

Please, see changes - there is typo in indention. It must be fixed, thanks.

@enumag
Copy link
Member

enumag commented Dec 17, 2013

I think it's wrong to have this in the Translator class. However I do think it could be useful in templates. The macro {translatePrefix} could set the prefix in a local variable inside the template and then the macro {_} could prepend it automatically.

@fprochazka
Copy link
Member

It certainly doesn't belong to translator and the indentation is horrible :)

Better would be to create a class PrefixedTranslator which would wrap existing translator and at the end of the macro it will then unwrap it.

Fyi there is an issue for this :) #3

@fabiancz
Copy link
Author

Sorry for indentation. It works bad with my IDE.

I think it should be in Translator class (or PrefixedTranslator for example) because then you can use it both in PHP and template.

@fprochazka
Copy link
Member

The problem with adding it directly to Translator class is a global state, which will be a problem, when you use the translator in presenters and forms.

@fprochazka
Copy link
Member

Thank you for your time, would you mind testing the new macro? I'm also open to better naming if anyone has a thought. Please let any further discussion happen at #3

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 this pull request may close these issues.

None yet

4 participants