Creating new language attributes

I’ve been using Kirby’s Language feature and am loving it so far.

Is there a way to create additional language attributes? My goal is to have a “short name” (e.g. English > U.S.A) attribute for each language. (And I’m using Kirby 3.)

en.php

return [
  'code' => 'en',
  'default' => true,
  'direction' => 'ltr',
  'locale' => 'en_US',
  'name' => 'English',
  'shortname' => 'USA',
  'url' => '/',
];

And I’m trying to use it like this.

<?php foreach($kirby->languages() as $language): ?>
    <li<?php e($kirby->language() == $language, ' class="active"') ?>>
        <a href="<?php echo $language->url() ?>">
            <?php echo html($language->shortname()) ?>
        </a>
    </li>
<?php endforeach ?>

Currently, it’s returning Call to undefined method Kirby\Cms\Language::shortname(). Any tips would be appreciated!

No, custom props are not possible (yet). There’s already an issue on GitHub

1 Like

Ok, at least it’s not just me :slight_smile:

Thanks for resolving!

You could use a workaround by setting an entry in the translations array:

return [
  'code' => 'en',
  'default' => true,
  'direction' => 'ltr',
  'locale' => 'en_US',
  'name' => 'English',
  'translations' => [
    'shortname' => 'USA'
  ]
  'url' => '/',
];

Then in your template:

<?php
$fallback = $language->name();
echo i18n::translate('shortname', $fallback, $language->code());
?>
1 Like

That’s a perfect solution, thank you!

02%20PM