Load a language file in plugin

I’m developing a plugin that allows you to translate SVG files via language variables. I am using a route:
site/plugins/magicsvg/magicsvg.php:

kirby()->routes([
    [
        'pattern' => '(:all)/magicsvg/(:all)',
        'method' => 'GET',
        'action' => function ($lang, $svg) {
            $file = kirby()->roots()->languages() . DS . $lang . '.yml';
            $contents = file_get_contents($file);
            $l = Yaml::read($contents);

            header('Content-type: image/svg+xml');
            include __DIR__ . '/svg/' . $svg . '.php';
        }
    ]
]);

After that, I can use $l in any myimage.svg.php file. However, is there a better way to load the language variables? Can I somehow load them from the Kirby API, so I can support PHP language files alongside YAML?

No, Kirby itself does that with a condition:

   $path = $this->roots()->languages() . DS . $site->language()->code();

      // load .php file if it exists
      if(f::exists($path . '.php')) include_once($path . '.php');

      // load .yml file and set as language variables if it exists
      if(f::exists($path . '.yml')) l::set(data::read($path . '.yml', 'yaml'));
    }
1 Like