laravel translatable
6.9.2
該套件包含一個特徵HasTranslations
以使 Eloquent 模型可翻譯。翻譯存儲為 json。不需要額外的桌子來容納它們。
use Illuminate Database Eloquent Model ;
use Spatie Translatable HasTranslations ;
class NewsItem extends Model
{
use HasTranslations;
// ...
}
將特徵應用於模型後,您可以執行以下操作:
$ newsItem = new NewsItem ;
$ newsItem
-> setTranslation ( ' name ' , ' en ' , ' Name in English ' )
-> setTranslation ( ' name ' , ' nl ' , ' Naam in het Nederlands ' )
-> save ();
$ newsItem -> name ; // Returns 'Name in English' given that the current app locale is 'en'
$ newsItem -> getTranslation ( ' name ' , ' nl ' ); // returns 'Naam in het Nederlands'
app ()-> setLocale ( ' nl ' );
$ newsItem -> name ; // Returns 'Naam in het Nederlands'
// If you want to query records based on locales, you can use the `whereLocale` and `whereLocales` methods.
NewsItem:: whereLocale ( ' name ' , ' en ' )-> get (); // Returns all news items with a name in English
NewsItem:: whereLocales ( ' name ' , [ ' en ' , ' nl ' ])-> get (); // Returns all news items with a name in English or Dutch
// Returns all news items that has name in English with value `Name in English`
NewsItem:: query ()-> whereJsonContainsLocale ( ' name ' , ' en ' , ' Name in English ' )-> get ();
// Returns all news items that has name in English or Dutch with value `Name in English`
NewsItem:: query ()-> whereJsonContainsLocales ( ' name ' , [ ' en ' , ' nl ' ], ' Name in English ' )-> get ();
// The last argument is the "operand" which you can tweak to achieve something like this:
// Returns all news items that has name in English with value like `Name in...`
NewsItem:: query ()-> whereJsonContainsLocale ( ' name ' , ' en ' , ' Name in% ' , ' like ' )-> get ();
// Returns all news items that has name in English or Dutch with value like `Name in...`
NewsItem:: query ()-> whereJsonContainsLocales ( ' name ' , [ ' en ' , ' nl ' ], ' Name in% ' , ' like ' )-> get ();
我們投入了大量資源來創建一流的開源套件。您可以透過購買我們的一款付費產品來支持我們。
我們非常感謝您從家鄉寄給我們一張明信片,並註明您正在使用我們的哪種套餐。您可以在我們的聯絡頁面上找到我們的地址。我們在虛擬明信片牆上發布所有收到的明信片。
所有文件均可在我們的文件網站上找到。
composer test
詳細資訊請參閱貢獻。
如果您發現有關安全的錯誤,請發送郵件至 [email protected],而不是使用問題追蹤器。
您可以自由使用這個軟體包,但如果它進入您的生產環境,我們非常感謝您從您的家鄉給我們寄一張明信片,註明您正在使用我們的哪個軟體包。
我們的地址是:Spatie, Kruikstraat 22, 2018 安特衛普, 比利時。
我們在公司網站上發布所有收到的明信片。
我們從 Mohamed Said 那裡得到了將翻譯以 json 形式儲存在列中的想法。本自述文件中使用了他的多語言包的自述文件的部分內容。
麻省理工學院許可證 (MIT)。請參閱許可證文件以獲取更多資訊。