Skip to content

Commit

Permalink
Update for Flarum beta 16
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkwinkelmann committed Mar 21, 2021
1 parent 64afea6 commit 7cd1bde
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 119 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
}
],
"require": {
"flarum/core": ">=0.1.0-beta.15 <0.1.0-beta.16",
"symfony/yaml": "^4.0"
"flarum/core": ">=0.1.0-beta.16 <0.1.0-beta.17",
"symfony/yaml": "^5.0"
},
"replace": {
"flagrow/linguist": "*"
Expand Down
11 changes: 7 additions & 4 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace FoF\Linguist;

use Flarum\Extend;
use FoF\Linguist\Extenders\ClearCacheStatus;
use FoF\Linguist\Extenders\LoadStrings;
use Flarum\Foundation\Event\ClearingCache;
use FoF\Linguist\Api\Controllers;

return [
Expand All @@ -20,6 +19,10 @@
->delete('/fof/linguist/strings/{id:[0-9]+}', 'fof.linguist.api.strings.delete', Controllers\StringDeleteController::class)
->get('/fof/linguist/export', 'fof.linguist.api.export', Controllers\ExportController::class)
->post('/fof/linguist/import', 'fof.linguist.api.import', Controllers\ImportController::class),
new ClearCacheStatus(),
new LoadStrings(),

(new Extend\ServiceProvider())
->register(Providers\LoadStrings::class),

(new Extend\Event())
->listen(ClearingCache::class, Listeners\ClearCacheStatus::class),
];
2 changes: 1 addition & 1 deletion js/dist/admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

36 changes: 21 additions & 15 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
},
"devDependencies": {
"flarum": "0.1.0-beta.16"
}
}
4 changes: 2 additions & 2 deletions js/src/admin/components/StringLocale.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from 'flarum/app';
import ItemList from 'flarum/utils/ItemList';
import Button from 'flarum/components/Button';
import ItemList from 'flarum/common/utils/ItemList';
import Button from 'flarum/common/components/Button';
import highlightMithril from '../utils/highlightMithril';

/* global m */
Expand Down
2 changes: 1 addition & 1 deletion js/src/admin/models/StringKey.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Model from 'flarum/Model';
import Model from 'flarum/common/Model';

export default class StringKey extends Model {
key = Model.attribute('key');
Expand Down
2 changes: 1 addition & 1 deletion js/src/admin/models/TextString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Model from 'flarum/Model';
import Model from 'flarum/common/Model';

export default class TextString extends Model {
key = Model.attribute('key');
Expand Down
4 changes: 2 additions & 2 deletions js/src/admin/pages/CoveragePage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from 'flarum/app';
import Button from 'flarum/components/Button';
import icon from 'flarum/helpers/icon';
import Button from 'flarum/common/components/Button';
import icon from 'flarum/common/helpers/icon';
import localesAsArray from '../utils/localesAsArray';
import namespaceLabel from '../utils/namespaceLabel';
import frontendLabel from '../utils/frontendLabel';
Expand Down
6 changes: 3 additions & 3 deletions js/src/admin/pages/ExportPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import app from 'flarum/app';
import Select from 'flarum/components/Select';
import Switch from 'flarum/components/Switch';
import LinkButton from 'flarum/components/LinkButton';
import Select from 'flarum/common/components/Select';
import Switch from 'flarum/common/components/Switch';
import LinkButton from 'flarum/common/components/LinkButton';
import localesAsArray from '../utils/localesAsArray';
import namespaceLabel from '../utils/namespaceLabel';

Expand Down
8 changes: 4 additions & 4 deletions js/src/admin/pages/ImportPage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import app from 'flarum/app';
import Button from 'flarum/components/Button';
import Select from 'flarum/components/Select';
import Switch from 'flarum/components/Switch';
import extractText from 'flarum/utils/extractText';
import Button from 'flarum/common/components/Button';
import Select from 'flarum/common/components/Select';
import Switch from 'flarum/common/components/Switch';
import extractText from 'flarum/common/utils/extractText';
import localesAsArray from '../utils/localesAsArray';

/* global m */
Expand Down
4 changes: 2 additions & 2 deletions js/src/admin/pages/LinguistPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from 'flarum/app';
import ExtensionPage from 'flarum/components/ExtensionPage';
import LoadingIndicator from 'flarum/components/LoadingIndicator';
import ExtensionPage from 'flarum/admin/components/ExtensionPage';
import LoadingIndicator from 'flarum/common/components/LoadingIndicator';
import StringsPage from './StringsPage';
import CoveragePage from './CoveragePage';
import ExportPage from './ExportPage';
Expand Down
12 changes: 6 additions & 6 deletions js/src/admin/pages/StringsPage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import app from 'flarum/app';
import Button from 'flarum/components/Button';
import Dropdown from 'flarum/components/Dropdown';
import Select from 'flarum/components/Select';
import Alert from 'flarum/components/Alert';
import LoadingModal from 'flarum/components/LoadingModal';
import extractText from 'flarum/utils/extractText';
import Button from 'flarum/common/components/Button';
import Dropdown from 'flarum/common/components/Dropdown';
import Select from 'flarum/common/components/Select';
import Alert from 'flarum/common/components/Alert';
import LoadingModal from 'flarum/admin/components/LoadingModal';
import extractText from 'flarum/common/utils/extractText';
import localesAsArray from '../utils/localesAsArray';
import StringKey from '../components/StringKey';
import namespaceLabel from '../utils/namespaceLabel';
Expand Down
3 changes: 3 additions & 0 deletions resources/less/admin/page.less
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
}

.ExtensionPage-settings {
// So the dropdowns don't go out of stream on pages with no results
min-height: 400px;

// Allow horizontal scrolling for the table in coverage tab
overflow-x: auto;

Expand Down
14 changes: 9 additions & 5 deletions src/Api/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

namespace FoF\Linguist\Api\Controllers;

use Flarum\Locale\Translator;
use FoF\Linguist\TextString;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Yaml\Yaml;

class ExportController implements RequestHandlerInterface
{
protected $translator;

public function __construct(Translator $translator)
{
$this->translator = $translator;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$request->getAttribute('actor')->assertAdmin();
Expand Down Expand Up @@ -49,10 +56,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

if (Arr::get($request->getQueryParams(), 'includeOriginals') && $locale !== null) {
/**
* @var MessageCatalogueInterface $catalogue
*/
$catalogue = app('translator')->getCatalogue($locale);
$catalogue = $this->translator->getCatalogue($locale);

$originals = $catalogue->all('messages');

Expand Down
14 changes: 8 additions & 6 deletions src/Api/Controllers/StringKeyIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ class StringKeyIndexController extends AbstractListController
{
public $serializer = StringKeySerializer::class;

protected $repository;

public function __construct(DefaultStringsRepository $repository)
{
$this->repository = $repository;
}

protected function data(ServerRequestInterface $request, Document $document)
{
$request->getAttribute('actor')->assertAdmin();

/**
* @var $repository DefaultStringsRepository
*/
$repository = app(DefaultStringsRepository::class);

return $repository->allTranslations();
return $this->repository->allTranslations();
}
}
28 changes: 0 additions & 28 deletions src/Extenders/ClearCacheStatus.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Listeners/ClearCacheStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace FoF\Linguist\Listeners;

use Flarum\Foundation\Event\ClearingCache;
use Flarum\Settings\SettingsRepositoryInterface;

class ClearCacheStatus
{
protected $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function handle(ClearingCache $event)
{
// When the cache has been cleared, either from Linguist tab or anywhere else, we remove our flag
$this->settings->delete('fof.linguist.should-clear-cache');
}
}
14 changes: 6 additions & 8 deletions src/Extenders/LoadStrings.php → src/Providers/LoadStrings.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
<?php

namespace FoF\Linguist\Extenders;
namespace FoF\Linguist\Providers;

use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Locale\LocaleManager;
use FoF\Linguist\Translator\StringLoader;
use Illuminate\Contracts\Container\Container;

class LoadStrings implements ExtenderInterface
class LoadStrings extends AbstractServiceProvider
{
public function extend(Container $container, Extension $extension = null)
public function register()
{
// Other extensions register their translations via `resolving()`, so we need to use `afterResolving()`
// to put our custom loader at the end to be able to override all translations
$container->afterResolving(
$this->container->afterResolving(
LocaleManager::class,
function (LocaleManager $locales) {
$translator = $locales->getTranslator();

$translator->addLoader('fof_linguist', app(StringLoader::class));
$translator->addLoader('fof_linguist', $this->container->make(StringLoader::class));

// Add the custom loader to every language available in Flarum
foreach ($locales->getLocales() as $locale => $name) {
Expand Down
13 changes: 5 additions & 8 deletions src/Repositories/DefaultStringsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@ class DefaultStringsRepository
{
protected $settings;
protected $events;
protected $manager;

public function __construct(SettingsRepositoryInterface $settings, Dispatcher $events)
public function __construct(SettingsRepositoryInterface $settings, Dispatcher $events, LocaleManager $manager)
{
$this->settings = $settings;
$this->events = $events;
$this->manager = $manager;
}

public function allTranslations()
{
/**
* @var $manager LocaleManager
*/
$manager = app(LocaleManager::class);

$translator = $manager->getTranslator();
$translator = $this->manager->getTranslator();

// Use our own cache factory that will force a fresh catalog load and not write it to file.
// This allows us to get fresh original strings without the Linguist-defined ones without
Expand All @@ -41,7 +38,7 @@ public function allTranslations()
// We disable our own translation loader so custom strings don't replace original ones
TranslationLock::stopLoadingTranslations();

foreach (array_keys($manager->getLocales()) as $locale) {
foreach (array_keys($this->manager->getLocales()) as $locale) {
foreach (Arr::get($translator->getCatalogue($locale)->all(), 'messages', []) as $key => $string) {
if (!Arr::has($translations, $key)) {
$translations[$key] = [
Expand Down
Loading

0 comments on commit 7cd1bde

Please sign in to comment.