Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jansentjeu committed Dec 23, 2023
0 parents commit 494c9ea
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vendor/
node_modules/
Controller/Test/
Controller/Adminhtml/Test/
Build/
build
.idea/
composer.lock
*.map
.DS_Store
53 changes: 53 additions & 0 deletions Plugin/Model/Template/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types = 1);

namespace Triveon\TransCustomVars\Plugin\Model\Template;

use Magento\Email\Model\Template\Filter as Subject;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class Filter
*/
class Filter extends Subject
{
private const CUSTOM_VAR_PREFIX = 'customVar_';

/**
* Function to set values of custom vars used in translations
* @param Subject $subject
* @param callable $proceed
* @param string[] $construction
* @return string
* @throws NoSuchEntityException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundTransDirective(Subject $subject, callable $proceed, array $construction)
{
$directive = $this->explodeModifiers($construction[2], 'escape')[0];
$params = $this->getTransParameters($directive)[1];

foreach ($params as $param) {
if (!is_string($param) || !str_starts_with($param, self::CUSTOM_VAR_PREFIX)) {
continue;
}

$customVarCode = substr($param, strlen(self::CUSTOM_VAR_PREFIX));
$storeId = $this->_storeManager->getStore()->getId();
$customVariable = $this->_variableFactory->create()->setStoreId($storeId)->loadByCode($customVarCode);

if (!$customVariable->getId()) {
continue;
}

$construction[2] = str_replace(
$param,
sprintf('"%s"', $customVariable->getValue()),
$construction[2]
);
}

return $proceed($construction);
}
}
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Translate Custom Variables Magento 2

This is the official Translate Custom Variables Magento 2 extension. With this module it is possible to use custom variables in translatable strings in CMS elements.

## How does it work?

1. For example, you have a custom variable with code free_shipping_cost, and you want to place it in a translatable string in a CMS Block or CMS Page
2. To do this, the prefix "customVar_" must be placed before the custom variable code in the translation string
3. As: {{trans "Free Shipping from %free_shipping" free_shipping=customVar_free_shipping_cost}}

The %free_shipping will be replaced by the value from the custom variable free_shipping_cost.

## Installation

### Installation using composer (recommended)
To install the extension login to your environment using SSH. Then navigate to the Magento 2 root directory and run the following commands in the same order as described:

Enable maintenance mode:
~~~~shell
php bin/magento maintenance:enable
~~~~

1. Install the extension:
~~~~shell
composer require tig/postnl-magento2
~~~~

2. Enable the Translate Custom Variables Magento 2 plugin
~~~~shell
php bin/magento module:enable Triveon_TransCustomVars
~~~~

3. Update the Magento 2 environment:
~~~~shell
php bin/magento setup:upgrade
~~~~

When your Magento environment is running in production mode, you also need to run the following comands:

4. Compile DI:
~~~~shell
php bin/magento setup:di:compile
~~~~

5. Deploy static content:
~~~~shell
php bin/magento setup:static-content:deploy
~~~~

6. Disable maintenance mode:
~~~~shell
php bin/magento maintenance:disable
~~~~

### Installation manually
1. Download the extension directly from [github](https://github.com/triveon/translate-custom-variables) by clicking on *Code* and then *Download ZIP*.
2. Create the directory *app/code/Triveon/TransCustomVars* (Case-sensitive)
3. Extract the zip and upload the code into *app/code/Triveon/TransCustomVars*
4. Enable the Translate Custom Vars Magento 2 plugin
~~~~shell
php bin/magento module:enable Triveon_TransCustomVars
~~~~

5. Update the Magento 2 environment:
~~~~shell
php bin/magento setup:upgrade
~~~~

## Update
To update the Translate Custom Variables Extension run the following commands:
~~~~shell
composer update triveon/translate-custom-variables
php bin/magento setup:upgrade
~~~~
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "triveon/translate-custom-variables",
"description": "A module to use custom variables in translatable CMS strings",
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0"
],
"require": {
"php": ">=7.3"
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Triveon\\TransCustomVars\\": ""
}
}
}

8 changes: 8 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Email\Model\Template\Filter">
<plugin name="Triveon_TransCustomVars_Plugin_Model_Template_Filter"
type="Triveon\TransCustomVars\Plugin\Model\Template\Filter"/>
</type>
</config>
10 changes: 10 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Triveon_TransCustomVars">
<sequence>
<module name="Magento_Email"/>
</sequence>
</module>
</config>

11 changes: 11 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types = 1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Triveon_TransCustomVars',
__DIR__
);

0 comments on commit 494c9ea

Please sign in to comment.