Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Localization ​

Introduction ​

Headlines and nouns may use translation keys from your app's lang files. Storyfeed translates them when retrieving the feed, using the application's current locale. Set it for each request as described in Laravel's locale configuration; Storyfeed does not choose the reader's locale.

Translating Headlines ​

Define the template for each supported locale, starting with the default:

lang/en/feed.php
php
return [
    // Without this line, English readers see "feed.order_placed".
    'order_placed' => ':actor placed :object at :target',
];
lang/fr/feed.php
php
return [
    'order_placed' => ':actor a passé :object chez :target',
];

Pass the translation key to FeedHeadline::trans():

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;
use Storyfeed\FeedHeadline;

Story::for(Order::class)
    ->verb('place')
    ->headline(FeedHeadline::trans('feed.order_placed'));

With the locale set to French:

Erica Sinclair

Translated templates keep linked tokens and optional segments. You may reorder tokens in each translation. Undefined keys are displayed as written.

anonymousHeadline() and missingHeadline() take a FeedHeadline too.

Translating Group Headlines ​

Add a template for the repeat group to each locale's feed.php file:

lang/en/feed.php
php
return [
    'order_placements' => ':actor made :count order placements at :target',
];
lang/fr/feed.php
php
return [
    'order_placements' => ':actor a passé commande :count fois chez :target',
];

Pass the key to the group's headline:

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;
use Storyfeed\FeedHeadline;
use Storyfeed\Grouping\GroupBuilder;

Story::for(Order::class)
    ->verb('place')
    ->grouped(
        fn (GroupBuilder $group) => $group
            ->repeat(FeedHeadline::trans('feed.order_placements')),
    );
php
use App\Models\Order;
use Storyfeed\Facades\Story;
use Storyfeed\FeedHeadline;
use Storyfeed\Grouping\Group;

Story::for(Order::class)
    ->verb('place')
    ->grouped(
        Group::repeat()->headline(FeedHeadline::trans('feed.order_placements')),
    );

With the locale set to French:

Erica Sinclair
Erica Sinclair a passé commande 3 fois chez Scoops Ahoy

The translation is resolved when the feed is read, including when definitions are cached. Role tokens remain linked.

Translated group tokens are not checked at boot. Each locale must use only tokens supported by the group's axis.

Group HeadlineHow It Is Rendered
A string or FeedHeadline::trans()a template whose role tokens and count are replaced
A closurefinished text; its result is not processed as a token template

Translating Nouns ​

Pass a translation key to FeedNoun::trans() for a group's noun:

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;
use Storyfeed\FeedNoun;

Story::for(Order::class)->fallback()->noun(FeedNoun::trans('nouns.order'));

Separate singular and plural forms with a pipe:

lang/en/nouns.php
php
return [
    'order' => 'order|orders',
];
lang/fr/nouns.php
php
return [
    'order' => 'commande|commandes',
];

See Aggregation for where nouns appear.

Released under the MIT License.