Jump to content

Dynamic language string name in templates


motomac

Recommended Posts

Posted

I need to get short weekday names dynamically. But this code in templates doesn't work:

{{$day1 = 'day_' . date( 'w', $timestamp ) . '_short';}}

{lang="$day1"}

Is it possible to do this?

Posted
2 hours ago, motomac said:

I need to get short weekday names dynamically. But this code in templates doesn't work:


{{$day1 = 'day_' . date( 'w', $timestamp ) . '_short';}}

{lang="$day1"}

Is it possible to do this?

Edit: scratch what I said before. What you want is

{lang="{$day1}"}

 

Posted
8 hours ago, Adriano Faria said:

Probably you need something like:


{{$day1 = \IPS\Member::loggedIn()->language()->addToStack( 'day_' . date( 'w', $timestamp ) . '_short' );}}

addToStack or get.

Untested.

addToStack is actually the equivalent of lang="".

Posted
23 minutes ago, motomac said:

@Adriano Faria I get day_2_short string in the output, so I guess the problem is in the second part: how {lang=""} works.

lang makes a call to addToStack. So the question is, do you have a language string called day_2_short?

Posted
6 hours ago, Daniel F said:

No, it's a js language string, which means that it doesn't exist in the php scope ^_^

Hm... But I can see it in the ACP:

Снимок экрана 2016-04-11 в 21.23.11.png

What should I do now?

Posted
2 hours ago, motomac said:

Hm... But I can see it in the ACP:

Снимок экрана 2016-04-11 в 21.23.11.png

What should I do now?

It will be listed in the ACP because that would display both PHP and JS variables. You'll need to create duplicate strings in your own lang file.

Posted

Is there any way to avoid this duplication? I don't want to let the users of my plugin to translate these strings while they are already translated in the System.

Posted
On 4/10/2016 at 10:21 AM, motomac said:

I need to get short weekday names dynamically. But this code in templates doesn't work:


{{$day1 = 'day_' . date( 'w', $timestamp ) . '_short';}}

{lang="$day1"}

Is it possible to do this?

 

So you have a variable $timestamp which, presumably, holds a timestamp right? And you want to show the short form of the day (i.e. "Sun" for Sunday)?

Something like this should work

{expression="\IPS\DateTime::ts( $timestamp )->strFormat( '%a' )"}

 

This doesn't rely on language strings but rather uses the native PHP DateTime object (which is locale aware).

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...