27

I am trying to add a translation for the text <!-- ko i18n: 'Store credit available' --><!-- /ko --> present in vendor/magento/module-customer-balance/view/frontend/web/template/payment/customer-balance.html, but if I try to change the text in my i18n/en_US.csv, still its not working.

I have flushed the cache and and used static content deploy.

Is there a different way to add translations for KO templates ?

Atish Goswami
  • 5,551
  • 7
  • 34
  • 63

2 Answers2

43

So I was finally able to figure out the problem.

Seems that the JS template translation are read from js-translation.json which is generated during setup:static-content:deploy execution. In order to populate data in this file a new language package has to be created for the project.

So instead of adding the CSV at the theme level like app/design/<area>/<vendor>/<theme-name>/i18n/xx_XX.csv we need to add it in the language package.

To create a new Language Package first from project document root we will need to create the following directories:

mkdir -p app/i18n/<project-name>/<xx_xx>

Important: USE lowercase DIRECTORY NAMES ONLY camcelcased folder names will not work

Then change directory to the newly created folders:

cd app/i18n/<project-name>/<xx_xx>

Now you can create a composer.json (optional) file with the following content:

{                                                     
    "name": "<project-name>/<xx_xx>",                             
    "description": "<sample description>", //Ex:English (United States) language
    "version": "<version-number>", //100.0.1                             
    "license": [                                      
        "OSL-3.0",                                    
        "AFL-3.0"                                     
    ],                                                
    "require": {                                      
        "magento/framework": "100.0.*"                
    },                                                
    "type": "magento2-language",                      
    "autoload": {                                     
        "files": [                                    
            "registration.php"                        
        ]                                             
    }                                                 
}                                                     

Next create we need a language.xml file with the following contents:

<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
    <code>xx_XX</code> <!-- example: <code>en_US</code> -->
    <vendor><project-name></vendor>
    <package><xx_xx></package> <!-- example: <package>en_us</package> -->
</language>

After than registration.php containing the following content is needed:

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
    '<project-name>_<xx_xx>',
    __DIR__
);

Now we can create a our translation CSV. If you already have one inside the theme folder something like app/design/<area>/<vendor>/<theme-name>/i18n/xx_XX.csv you can just move it to app/i18n/<project-name>/<xx_xx>/xx_XX.csv

Now from the project document root we need to run these commands:

find pub/static -name js-translation.json -exec rm -rf {} \;

We need to delete the js-translation.json which has been already created before running the setup:static-content:deploy

Now we run static content deploy:

php bin/magento setup:static-content:deploy <xx_XX>

Once thats done we clear the cache:

php bin/magento cache:clean
php bin/magento cache:flush

We can verify if the translation files for JS template has been generated by finding all the js-translation.json inside the pub/static folder.

find pub/static -name js-translation.json

This will provide the list of all the translation files generated for JS templates.

Reference:

  1. Magento DevDocs
  2. Related Github Issue
Atish Goswami
  • 5,551
  • 7
  • 34
  • 63
  • 1
    In my project js-translation.json files are created correctly from static content deploy even if .csv files are inserted in app/design/frontend/vendor/theme-name/i18n/xx_XX.csv. The only care is delete all js-translation.json before run static content deploy. – LucScu Aug 22 '16 at 09:47
  • Which version of magento are you on ? – Atish Goswami Aug 23 '16 at 13:24
  • Vender/Theme so from this what should be as per your above code?? – Kaushal Suthar Sep 27 '16 at 13:27
  • @KaushalSuthar Project name can be anything you want, just make sure that it is in all lower case – Atish Goswami Sep 27 '16 at 13:34
  • Yes just did it and working fine (y) thanks.... – Kaushal Suthar Sep 27 '16 at 13:48
  • 1
    Is this really the only way to do this? I mean, even the documentation (http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/translations/xlate.html) states that we should be able to put translation files into design/<vender>/<theme>/i18n/xx_XX.csv. – Giel Berkers Dec 09 '16 at 13:57
  • @GielBerkers this is only needed for JS translation, as the magento 2.0.X doesnot take the design///i18n/*.csv to generate the js-translation.json. – Atish Goswami Jan 06 '17 at 10:51
  • helpful, worked for M2 EE 2.1.1. couldn't Imagen that the solution would be by creating a translation package – user4531 Jan 18 '17 at 03:39
  • Nice, this actually works. Btw, the composer.json file is not needed. I could leave it out and it still worked. – Patrick van Bergen Jan 27 '17 at 14:54
  • In 2.15. in my custom theme design/vendor/theme/i18n/de_DE.csv translations didn't work at all. It just takes the translations from app/i18n/... You don't need to deploy static content btw. if not deployed they are created on the fly which is very handy for development. Just delete all js-translation.json once. – steros May 03 '17 at 13:59
  • 1
    Magento 2 has a lot of bugs related to this, for us the best way has been to manually create a script that creates the js-translation.json file and make web server configuration that delivers it on the right kinds of requests – cjohansson Jun 20 '17 at 06:14
  • @AtishGoswami, Could you please look into my question and provide some suggestion. https://magento.stackexchange.com/questions/307308/magento-2-knockout-js-translation-is-not-working?noredirect=1#comment442618_307308 – Ramya Mar 18 '20 at 13:40
  • This is an invaluable answer, can I gift rep? – Scott Anderson Apr 08 '21 at 14:13
1

As I described in the Github issue (https://github.com/magento/magento2/issues/7525#issuecomment-316690100) there is another (easier) solution to that problem.

You just need to make sure to deploy every language separate.

So instead of using:

php bin/magento setup:static-content:deploy en_US de_DE it_IT fr_FR es_ES

use

php bin/magento setup:static-content:deploy en_US
php bin/magento setup:static-content:deploy de_DE
php bin/magento setup:static-content:deploy it_IT
php bin/magento setup:static-content:deploy fr_FR
php bin/magento setup:static-content:deploy es_ES

This is probably caused by the fact that the static-content:deploy only compiles the files once and copies them afterwards to other languages.

FaHu
  • 238
  • 2
  • 9