Packaging a Custom Language for Magento 2

Packaging a Custom Language for Magento 2

Magento 2 translations live in language packages. Building your own lets you translate the whole storefront, including your custom modules, and deploy it like any other component. This example uses Simplified Chinese.

1. Collect every phrase

php bin/magento i18n:collect-phrases --output=zh_Hans_CN.csv --magento .

This writes a CSV of all translatable strings to the Magento root. Translate the second column.

2. Create the package folders

mkdir app/i18n
mkdir app/i18n/brush/
mkdir app/i18n/brush/zh_Hans_CN/

3. Add composer.json

{
 "name": "test/nl_nl",
   "description": "Chinese Language",
   "version": "100.0.1",
   "license": [
     "OSL-3.0",
     "AFL-3.0"
   ],
   "require": {
     "magento/framework": "100.0.*"
   },
   "type": "magento2-language",
   "autoload": {
     "files": [
       "registration.php"
     ]
   }
   ,"extra": {"map": "*", "brush/zh_Hans_CN"}
 }

4. Add language.xml

<?xml version="1.0"?>
<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
  zh_Hans_CN
  <vendor>brush</vendor>
  <package>zh_hans_cn</package>
</language>

5. Add registration.php

<?php
 /**
  * Copyright © 2015 Magento. All rights reserved.
  * See COPYING.txt for license details.
  */  
 \Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
     'brush_zh_hans_cn',
     __DIR__
 );

Move your translated CSV into the package folder, named after the locale.

6. Deploy

php bin/magento setup:static-content:deploy
php bin/magento setup:static-content:deploy nl_NL
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush

Finally, set the store view locale under Stores > Configuration > General > Locale Options.

Stuck on something similar? Our Magento development team is happy to take a look.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.