How to get i18n Directory path of a module using Magento 2?

i18n folder in module useful for translation purpose in Magento 2 for multiple languages.

You can add a translated CSV file at the module or theme level under the i18n folder. The website has multiple languages and you want to translate the text to a specific locale of the store, you need to create a CSV file for respective language locale like en_US.csv, fr_FR.csv file.

i18n stand for Internationalization. i18n is a short form of the entire word. taken first character i and last character n from the Internationalization and 18 character is the middle of i and n.

You can retrieve the path of the i18n Directory of a module using the Magento\Framework\Module\Dir class.

Dir.php class contains the directory path for the separate folder,

const MODULE_ETC_DIR = 'etc';
const MODULE_I18N_DIR = 'i18n';
const MODULE_VIEW_DIR = 'view';
const MODULE_CONTROLLER_DIR = 'Controller';
const MODULE_SETUP_DIR = 'Setup';

Let’s look a simple example for getting i18n file path,

<?php
namespace Jesadiya\I18nPath\Model;

use Magento\Framework\Module\Dir;

class GetI18nPath
{
    /**
     * @var Dir
     */
    private $moduleReader;


    public function __construct(
        LoggerInterface $logger,
        Dir $moduleReader
    ) {
        $this->moduleReader = $moduleReader;
    }

    /**
     * Get i18n directory full path from the module
     */
    public function getI18nPath()
    {
        $dirPath = $this->moduleReader->getDir(
            'Jesadiya_Modulename', Dir::MODULE_I18N_DIR
        );

        return $dirPath;
    }
}

Call function,
echo $this->getI18nPath();

OutPut:
/var/www/html/magento234/app/code/Jesadiya/Modulename/i18n

You can also get the full path of csv file by appending CSV file name in the above module.