Skip to content

Commit

Permalink
Working craft 5 version
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Dec 21, 2023
1 parent 5d08f31 commit 2487371
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 118 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "statikbe/craft-translate",
"description": "Translate static string from the Craft control panel",
"type": "craft-plugin",
"version": "2.1.3",
"version": "5.0.0.beta-01",
"keywords": [
"craft",
"cms",
Expand All @@ -22,7 +22,7 @@
}
],
"require": {
"craftcms/cms": "^4.0.0-beta.1"
"craftcms/cms": "^5.0.0-alpha.1"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/TranslateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public function actionDownloadCsvFile(): Response
public function actionSave(): \yii\web\Response
{
$this->requireAcceptsJson();

$response = [
'success' => true,
'errors' => []
Expand Down Expand Up @@ -177,9 +176,10 @@ public function actionSave(): \yii\web\Response
}
}
$translations = Craft::$app->request->getRequiredBodyParam('translation');
$translations = reset($translations);

// Save to translation file
Translate::getInstance()->translate->set($site->language, $translations, $translatePath);
Translate::getInstance()->translate->set($site->language, $translations['translation'], $translatePath);

// Redirect back to page
return $this->asJson($response);
Expand Down
145 changes: 34 additions & 111 deletions src/elements/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Craft;
use craft\base\Element;
use craft\elements\db\ElementQueryInterface;
use craft\elements\User;
use craft\helpers\FileHelper;
use craft\web\ErrorHandler;
use statikbe\translate\elements\db\TranslateQuery;
Expand Down Expand Up @@ -116,13 +117,6 @@ public static function find(): ElementQueryInterface
*/
protected static function defineTableAttributes(): array
{
// $primary = Craft::$app->getSites()->getPrimarySite();
// $locale = Craft::$app->getI18n()->getLocaleById($primary->language);
// $attributes['original'] = ['label' => Craft::t('translate', 'Source: {region} ({language})', [
// 'language' => $primary->language,
// 'region' => $locale->displayName
// ])];
// $currentSite = Craft::$app->getRequest()->getQueryParam('site');
$attributes['field'] = ['label' => Craft::t('app', 'Translation')];
return $attributes;
}
Expand Down Expand Up @@ -161,7 +155,6 @@ protected static function defineSearchableAttributes(): array
'translation',
'source',
'file',
//'status',
'locale'
];
}
Expand All @@ -173,99 +166,6 @@ protected static function defineSources(string $context = null): array
{
$sources = [];

//toonde extra kolom met template status (bolletjes groen/oranje)
// $sources[] = ['heading' => Craft::t('translate', 'Template Status')];
//
// $key = 'status:' . self::ALL;
// $sources[] = [
// 'status' => null,
// 'key' => $key,
// 'label' => Craft::t('translate', 'All'),
// 'criteria' => [
// 'source' => [
// Craft::$app->path->getSiteTemplatesPath()
// ],
// ],
// ];
//
// $key = 'status:' . self::PENDING;
// $sources[] = [
// 'status' => self::PENDING,
// 'key' => $key,
// 'label' => Craft::t('translate', 'Pending'),
// 'criteria' => [
// 'source' => [
// Craft::$app->path->getSiteTemplatesPath()
// ],
// 'translateStatus' => self::PENDING
// ],
// ];
//
// $key = 'status:' . self::TRANSLATED;
// $sources[] = [
// 'status' => self::TRANSLATED,
// 'key' => $key,
// 'label' => Craft::t('translate', 'Translated'),
// 'criteria' => [
// 'source' => [
// Craft::$app->path->getSiteTemplatesPath()
// ],
// 'translateStatus' => self::TRANSLATED
// ],
// ];

// Get template sources (toont files in sidebar)
// $templateSources = array();
// $options = [
// 'recursive' => false,
// 'only' => ['*.html', '*.twig', '*.js', '*.json', '*.atom', '*.rss'],
// 'except' => ['vendor/', 'node_modules/', 'jsPlugins/']
// ];
//
// $templates = FileHelper::findFiles(Craft::$app->path->getSiteTemplatesPath(), $options);
//// $templates = FileHelper::findFiles(Craft::$app->path->getSiteTemplatesPath(), $allTemplates);
//
// foreach ($templates as $template) {
// // If matches, get template name
// $fileName = basename($template);
// // Fixes bug in ElementHelper::findSource in Linux OS
// $cleanTemplateKey = str_replace('/', '*', $template);
// // Add template source
// $templateSources['templatessources:' . $fileName] = [
// 'label' => $fileName,
// 'key' => 'templates:' . $cleanTemplateKey,
// 'criteria' => [
// 'source' => [
// $template
// ],
// ],
// ];
// }

// Maps (toont folders in sidebar)
// $options = [
// 'recursive' => false,
// 'except' => ['vendor/', 'node_modules/', 'jsPlugins/']
// ];
// $folders = FileHelper::findDirectories(Craft::$app->path->getSiteTemplatesPath(), $options);
//
// foreach ($folders as $template) {
// // If matches, get template name
// $fileName = basename($template);
// // Fixes bug in ElementHelper::findSource in Linux OS
// $cleanTemplateKey = str_replace('/', '*', $template);
// // Add template source
// $templateSources['templatessources:' . $fileName] = [
// 'label' => $fileName . '/',
// 'key' => 'templates:' . $cleanTemplateKey,
// 'criteria' => [
// 'source' => [
// $template
// ],
// ],
// ];
// }

$sources[] = ['heading' => Craft::t('translate', 'Default')];

$sources[] = [
Expand All @@ -290,7 +190,7 @@ protected static function defineSources(string $context = null): array

//was vroeger $modulesources (om plugin mapje te maken met eronder de plugins, nu enkel balkje per plugin)
// $sources['plugins:' . $path] = [
$modulesSources['plugins:' . $path] = [
$modulesSources['plugins:' . $path] = [
'label' => $module->id,
'key' => 'plugins:' . $module->id,
'criteria' => [
Expand Down Expand Up @@ -318,31 +218,56 @@ protected static function defineSources(string $context = null): array
return $sources;
}

public function canSave(User $user): bool
{
return true;
}

public function getInlineAttributeInputHtml(string $attribute): string
{
return $this->field;
}


/**
* @inheritdoc
*/
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes): string
public static function indexHtml(
ElementQueryInterface $elementQuery,
array $disabledElementIds = null,
array $viewState,
string $sourceKey = null,
string $context = null,
bool $includeContainer,
bool $showCheckboxes,
bool $sortable
): string
{
// just 1 locale enabled
if (empty($elementQuery->siteId)) {
$primarySite = Craft::$app->getSites()->getPrimarySite();
$elementQuery->siteId = $primarySite->id;
}

if ($elementQuery->translateStatus) {
$elementQuery->status = $elementQuery->translateStatus;
}

$elementQuery->status = null;
$elements = TranslatePlugin::getInstance()->translate->get($elementQuery);

$variables = [
'viewMode' => $viewState['mode'],
'context' => $context,
'disabledElementIds' => $disabledElementIds,
'inputNameSpace' => 'translation',
'nestedInputNamespace' => 'translation',
'disabledElementIds' => [],
'attributes' => Craft::$app->getElementSources()->getTableAttributes(static::class, $sourceKey),
'elements' => $elements,
'sourceKey' => $sourceKey,
'includeContainer' => $includeContainer,
'includeContainer' => false,
'showCheckboxes' => false,
'selectable' => false,
'sortable' => false,
'showHeaderColumn' => true,
'inlineEditing' => true,
];

// Better UI
Expand All @@ -351,9 +276,7 @@ public static function indexHtml(ElementQueryInterface $elementQuery, array $dis
Craft::$app->view->registerJs("$('.filter-btn').addClass('hidden');");
Craft::$app->view->registerJs("$('.btn.statusmenubtn').addClass('hidden');");

$template = '_elements/' . $viewState['mode'] . 'view/' . ($includeContainer ? 'container' : 'elements');

return Craft::$app->view->renderTemplate($template, $variables);
return Craft::$app->view->renderTemplate("_elements/tableview/container", $variables);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/services/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ private function _processFile(string $path, string $file, ElementQueryInterface
$translation = Craft::t($category, $original, [], $site->language);

$view = Craft::$app->getView();
$elementId = ElementHelper::generateSlug($original);
$slug = ElementHelper::generateSlug($original);

$field = $view->renderTemplate('_includes/forms/text', [
'id' => $elementId,
'id' => $slug,
'name' => 'translation[' . $original . ']',
'value' => $translation,
'placeholder' => $translation,
]);

// Let's create our translate element with all the info
$element = new TranslateElement([
'id' => ElementHelper::generateSlug($original),
'id' => $slug,
'original' => $original,
'translation' => $translation,
'source' => $path,
Expand Down
1 change: 1 addition & 0 deletions src/templates/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
{% set selectedSite = craft.app.request.getQueryParam('site', null) ? craft.app.sites.getSiteByHandle(craft.app.request.getQueryParam('site')) : craft.app.sites.primarySite %}
<input type="hidden" name="siteId" value="{{ selectedSite.id }}">
<input type="hidden" name="action" value="translate/translate/save">
<input type="hidden" name="sourceKey" value="{{ craft.app.request.getParam('source') }}">

{# banner block #}
<div class="readable tablepanel" data-layout-element="3a81c043-79a2-48d1-ab88-a0d512b7be9f">
Expand Down

0 comments on commit 2487371

Please sign in to comment.