/** * Scans the theme templates for message references. * @return void */ public function scanForMessages() { $messages = []; foreach (Layout::all() as $layout) { $messages = array_merge($messages, $this->parseContent($layout->markup)); } foreach (Page::all() as $page) { $messages = array_merge($messages, $this->parseContent($page->markup)); } foreach (Partial::all() as $partial) { $messages = array_merge($messages, $this->parseContent($partial->markup)); } Message::importMessages($messages); }
public function testMakeMessageCode() { $this->assertEquals('hello.world', Message::makeMessageCode('hello world')); $this->assertEquals('hello.world', Message::makeMessageCode(' hello world ')); $this->assertEquals('hello.world', Message::makeMessageCode('hello-world')); $this->assertEquals('hello.world', Message::makeMessageCode('hello--world')); // casing $this->assertEquals('hello.world', Message::makeMessageCode('Hello World')); $this->assertEquals('hello.world', Message::makeMessageCode('Hello World!')); // underscores $this->assertEquals('helloworld', Message::makeMessageCode('hello_world')); $this->assertEquals('helloworld', Message::makeMessageCode('hello__world')); // length limit $veryLongString = str_repeat("10 charstr", 30); $this->assertTrue(strlen($veryLongString) > 250); $this->assertEquals(253, strlen(Message::makeMessageCode($veryLongString))); $this->assertStringEndsWith('...', Message::makeMessageCode($veryLongString)); // unicode characters // brrowered some test cases from Stringy, the library Laravel's // `slug()` function depends on // https://github.com/danielstjules/Stringy/blob/master/tests/CommonTest.php $this->assertEquals('fòô.bàř', Message::makeMessageCode('fòô bàř')); $this->assertEquals('ťéśţ', Message::makeMessageCode(' ŤÉŚŢ ')); $this->assertEquals('φ.ź.3', Message::makeMessageCode('φ = ź = 3')); $this->assertEquals('перевірка', Message::makeMessageCode('перевірка')); $this->assertEquals('лысая.гора', Message::makeMessageCode('лысая гора')); $this->assertEquals('щука', Message::makeMessageCode('щука')); $this->assertEquals('foo.漢字', Message::makeMessageCode('foo 漢字')); // Chinese $this->assertEquals('xin.chào.thế.giới', Message::makeMessageCode('xin chào thế giới')); $this->assertEquals('xin.chào.thế.giới', Message::makeMessageCode('XIN CHÀO THẾ GIỚI')); $this->assertEquals('đấm.phát.chết.luôn', Message::makeMessageCode('đấm phát chết luôn')); $this->assertEquals('foo', Message::makeMessageCode('foo ')); // no-break space (U+00A0) $this->assertEquals('foo', Message::makeMessageCode('foo ')); // spaces U+2000 to U+200A $this->assertEquals('foo', Message::makeMessageCode('foo ')); // narrow no-break space (U+202F) $this->assertEquals('foo', Message::makeMessageCode('foo ')); // medium mathematical space (U+205F) $this->assertEquals('foo', Message::makeMessageCode('foo ')); // ideographic space (U+3000) }
public function prepareTable() { $fromCode = post('locale_from', null); $toCode = post('locale_to', Locale::getDefault()->code); $this->hideTranslated = post('hide_translated', false); /* * Page vars */ $this->vars['hideTranslated'] = $this->hideTranslated; $this->vars['defaultLocale'] = Locale::getDefault(); $this->vars['locales'] = Locale::all(); $this->vars['selectedFrom'] = $selectedFrom = Locale::findByCode($fromCode); $this->vars['selectedTo'] = $selectedTo = Locale::findByCode($toCode); /* * Make table config, make default column read only */ $config = $this->makeConfig('config_table.yaml'); if (!$selectedFrom) { $config->columns['from']['readOnly'] = true; } if (!$selectedTo) { $config->columns['to']['readOnly'] = true; } /* * Make table widget */ $widget = $this->makeWidget('Backend\\Widgets\\Table', $config); $widget->bindToController(); /* * Populate data */ $dataSource = $widget->getDataSource(); $dataSource->bindEvent('data.getRecords', function ($offset, $count) use($selectedFrom, $selectedTo) { $messages = $count ? Message::orderBy('message_data', 'asc')->limit($count)->offset($offset)->get() : Message::orderBy('message_data', 'asc')->get(); $result = $this->processTableData($messages, $selectedFrom, $selectedTo); return $result; }); $dataSource->bindEvent('data.getCount', function () { return Message::count(); }); $dataSource->bindEvent('data.updateRecord', function ($key, $data) { $message = Message::find($key); $this->updateTableData($message, $data); CacheHelper::clear(); }); $dataSource->bindEvent('data.deleteRecord', function ($key) { if ($message = Message::find($key)) { $message->delete(); } }); $this->vars['table'] = $widget; }
/** * Set the page context for translation caching. */ public function setMessageContext($page) { if (!$page) { return; } $translator = Translator::instance(); Message::setContext($translator->getLocale(), $page->url); }
protected function updateGridData($changes) { if (!is_array($changes)) { return; } foreach ($changes as $change) { if (!($code = array_get($change, 'rowData.code'))) { continue; } if (!($columnType = array_get($change, 'keyName'))) { continue; } if ($columnType != 'to' && $columnType != 'from') { continue; } if (!($locale = post('locale_' . $columnType))) { continue; } if (!($item = Message::whereCode($code)->first())) { continue; } $newValue = array_get($change, 'newValue'); $item->toLocale($locale, $newValue); } }
<?php use RainLab\Translate\Models\Message; use RainLab\Translate\Classes\Translator; /* * Adds a custom route to check for the locale prefix. */ App::before(function ($request) { $translator = Translator::instance(); if (!$translator->isConfigured()) { return; } $locale = Request::segment(1); if ($translator->setLocale($locale)) { Route::group(['prefix' => $locale], function () use($locale) { Route::any('{slug}', 'Cms\\Classes\\Controller@run')->where('slug', '(.*)?'); }); Route::any($locale, 'Cms\\Classes\\Controller@run'); } }); /* * Save any used messages to the contextual cache. */ App::after(function ($request) { Message::saveToCache(); });
public function translateString($string, $params = []) { return Message::trans($string, $params); }
public function translatePlural($string, $count = 0, $params = []) { return Lang::choice(Message::trans($string, $params), $count, $params); }