This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

PolloZen
PolloZen

Hi! I have a YAML file with options to be used in a dropdown menu

workspaces.yaml

school: author.plugin::lang.workspace.school
home: author.plugin::lang.workspace.home
office: author.plugin::lang.workspace.office

In my lang file plugins/author/plugin/lang/en/lang.php I have:

return [
    'workspace' => [
        'school' => 'School',
        'home' => 'In my home',
        'office' => 'I prefer my office',
    ],
];

I use the ParseFile yaml function in order to get the content file

public function populate()
{
    $parser  = new Yaml;
    $path    = plugins_path() . '/author/plugin/additional/populate/workspaces.yaml';
    $options = $parser->ParseFile($path);
    return $options;
}

The next step is use the populate function in the onRun() function component in order to pass the options to the front end

public function onRun()
{
    $this->page['workspaces'] = $this->myOwnClass->populate();
}

Everything in the process works great, except the translation. The string author.plugin::lang.workspace.school pass as it is to the front end, without be translated.

What is going on? :S

mjauvin
mjauvin

Try this maybe:

use Yaml;

Yaml::parseFile(...);
mjauvin
mjauvin

Did you forget to open the php parser tag in your lang.php file? i.e.

<?php
PolloZen
PolloZen

mjauvin said:

Try this maybe:

use Yaml;

Yaml::parseFile(...);

Hi, thank you for your answer, unfurnetly the parseFile is a non static method, so I can't use it as you sugest.

About the php tag, it's open. The lang file is used across the plugin with other translations.

PolloZen
PolloZen

Well, it's solved.

For an unknown reason, the localization strings doesn't work in the yaml files that are located outside the controllers and models folders.

That's way the parse function gets the string author.plugin::lang.workspace.school without be translated.

I modified my populate function; I parse the yaml and use a foreach to translate the string

public function populate($who)
{
    $parser  = new Yaml;
    $path    = plugins_path() . '/author/plugin/additional/populate/workspaces.yaml';
    $options = $parser->ParseFile($path);
    foreach ($options as $key=>$value) {
        $options[$key] = Lang::get($value);
    }
    return $options;
}
mjauvin
mjauvin

That's odd, do you mind opening an issue for this on github?

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.