diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4c2c9b --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +vendor/ +node_modules/ +Controller/Test/ +Controller/Adminhtml/Test/ +Build/ +build +.idea/ +composer.lock +*.map +.DS_Store diff --git a/Plugin/Model/Template/Filter.php b/Plugin/Model/Template/Filter.php new file mode 100644 index 0000000..cc8eea8 --- /dev/null +++ b/Plugin/Model/Template/Filter.php @@ -0,0 +1,53 @@ +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); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..05d9764 --- /dev/null +++ b/README.md @@ -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 +~~~~ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..01d0858 --- /dev/null +++ b/composer.json @@ -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\\": "" + } + } +} + diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..6bb9e94 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..05f520b --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..97ad111 --- /dev/null +++ b/registration.php @@ -0,0 +1,11 @@ +