Blog

By default Language strings in Laravel are stored in files within the 'resources/lang' directory. To store them in a database table we must make a few changes.

Database Structure:

CREATE TABLE `languages` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `languages` VALUES ('1', 'en-GB'), ('2', 'nl-NL');
DROP TABLE IF EXISTS `i18n`;
CREATE TABLE `i18n` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `source_language` int(11) unsigned DEFAULT NULL,
  `destination_language` int(11) unsigned DEFAULT NULL,
  `source_string` text,
  `destination_string` text,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `source_language` (`source_language`),
  KEY `destination_language` (`destination_language`),
  FULLTEXT KEY `source_string` (`source_string`,`destination_string`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Open the language file 'resources/lang/nl/messages.php':

<?php

return DB::table('i18n')
    ->join('languages AS l1', 'l1.id', '=', 'i18n.source_language')
    ->join('languages AS l2', 'l2.id', '=', 'i18n.destination_language')
    ->where('l1.name', '=', 'en-GB')
    ->where('l2.name', '=', 'nl-NL')
    ->orderBy('i18n.source_string')
    ->lists('i18n.destination_string', 'i18n.source_string')
;
Zoeken