KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

How to check the selected language #2845

Closed GHSix opened this topic on on Oct 7, 2022 - 4 comments

GHSix commented on Oct 7, 2022

Some times I may want to do something different in the code for a particular language.

Ie:

Currency::create('val_frete')
                ->label(Lang::t('Freight'))
                ->BRL()
                ->symbol()
                ->decimals(2),

Here I'm hard setting the currency format to BRL but, if EN is selected, I would need to change it to USD.

Is there a getLang() or something?

KoolReport commented on Oct 8, 2022

You use this to get name:

Lang::name()

you will get "EN" or "PTBR".

GHSix commented on Oct 19, 2022

Thank you.

Since I'm doing just 2 langs, I did it like:

Currency::create('val')
                ->label(Lang::t('Endorsed'))
                ->symbol()
                ->decimals(2)
                ->{(strpos(Lang::name(), 'PTBR') !== false)? 'BRL' : 'USD'}()

But I really would like an easy way to set currency based on the used language.

First I guess would be cool if currency autodetected the used language. But if it's too troublesome, some quickier way to set it would work well too, for example:

Currency::create('val')
                ->use(Lang::name())
KoolReport commented on Oct 20, 2022

I suggest that you make.your own field called FlexCurrency or something like that. This field derived from Currency. You put the code of setting property based on lang inside onCreated() method. With this way, you can utilize the "DRY". Let say later you add more language, changing above code every file is very tired.

GHSix commented on Oct 20, 2022

Yes, you are right about that. Thank you.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Dashboard