egg icon indicating copy to clipboard operation
egg copied to clipboard

Adding new function to Nunjucks

Open sm2017 opened this issue 4 years ago • 7 comments

In documentation we have

<li>{{ __('Email') }}: {{ user.email }}</li>
<li>
  {{ __('Welcome back, %s!', user.name) }}
</li>
<li>
  {{ __('Hello {0}! My name is {1}.', ['foo', 'bar']) }}
</li>

I want to know how you add __ to the Nunjucks, I need to add a t function for my localizations Can you please show me the source code?

sm2017 avatar Nov 20 '21 07:11 sm2017

@sm2017 hello, you can set a t alias function to __ like gettext does.

app.context.t = app.context.__;

fengmk2 avatar Nov 20 '21 07:11 fengmk2

@fengmk2 I read the source codes, but cannot find where you defined app.context.__ in the Nunjucks, can you please show me the code?

sm2017 avatar Nov 20 '21 07:11 sm2017

@sm2017 egg will set __ function to ctx.locals then merge all locals on view plugin before pass to nunjucks engine.

fengmk2 avatar Nov 20 '21 07:11 fengmk2

@fengmk2 thanks a lot

sm2017 avatar Nov 20 '21 07:11 sm2017

@sm2017 hello, you can set a t alias function to __ like gettext does.

app.context.t = app.context.__;

The t function is the i18next.t function, So I don't want to override it

Is it safe to use the following code?

ctx.locals.t = t

sm2017 avatar Nov 20 '21 07:11 sm2017

@fengmk2 @atian25 Can you please reply ? is it safe to override ctx.locals.t = t?

sm2017 avatar Dec 05 '21 11:12 sm2017

@fengmk2 I need your help!

sm2017 avatar Dec 15 '21 06:12 sm2017