coffreo/js-translation-extractor

Extract translation from javascript source code

0.3.0 2022-02-10 15:21 UTC

This package is auto-updated.

Last update: 2024-04-10 20:37:01 UTC


README

By Coffreo

Build Status codecov

Javascript translations extractor for willdurand/js-translation-bundle

This package is a small project existing to store in one place wonderfuls regexps to properly export translations strings & metas from javascript source files.
It can be used standalone, but it's main goal is to be used in :

Installation

Features

  • extracts following strings from javascript files like a pro
trans('MESSAGE', {param: 'foo'}, 'DOMAIN')
//    ^ 1st parameter is translation message
//                               ^ 3rd parameter is translation domain

transChoice('MESSAGE_WITH_PLURALS', 3, {param: 'foo'}, 'DOMAIN')
//          ^ 1st parameter is translation message      
//                                                     ^ 4th parameter is translation domain

// others parameters aren't extracted because they are useless. 

Note that this is the syntax used by js-translation-bundle

  • allow string delimiters to be " or '
  • extracts multi-lines commands

Usage

use Coffreo\JsTranslationExtractor\Extractor\JsTranslationExtractor;
use Coffreo\JsTranslationExtractor\Model\TranslationCollection;

$extractor = new JsTranslationExtractor();
$translationCollection = new TranslationCollection();

$extractor->extract(<<<FILECONTENT
import Translator from 'bazinga-translator';

export default () => Translator.trans('This is awesome', {}, 'foo');
export {
    bad: () => Translator.trans('This is ugly');
};
FILECONTENT
    , $translationCollection);

$first = $translationCollection->first();
$first->getMessage();  // This is awesome
$first->getLine();     // 3
$first->getContext();  // ['domain' => 'foo']

$second = $translationCollection->get(1);
$second->getMessage();  // This is ugly
$second->getLine();     // 5
$second->getContext();  // []

Found a bug

Please fill an issue with informations to reproduce bug.

If your translated strings are not extracted properly, please provide a sample failing string.

Development

  • Clone repository
  • Execute
make [install]   # to init composer after install
make test        # to run test

TODO

  • Find a way to add and extract desc/meaning values

License

This project is licensed under the MIT License - see the LICENSE file for details