Skip to content

Commit d3af03e

Browse files
committed
first commit
1 parent d9cf8ed commit d3af03e

File tree

4 files changed

+227
-2
lines changed

4 files changed

+227
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
composer.lock
3+
tmp
4+
RoboFile.php

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
# robo-gettext
2-
robo task to extract gettext values from files
1+
# GettextRobo
2+
3+
[Robo](http://robo.li) task to extract gettext values from files using [gettext/gettext](https://github.com/oscarotero/Gettext) library
4+
5+
Created by Oscar Otero <http://oscarotero.com> <[email protected]> (MIT License)
6+
7+
## Install
8+
9+
Using composer:
10+
11+
```bash
12+
composer require gettext/robo
13+
```
14+
15+
## usage example
16+
17+
Create a RoboFile.php with the following code:
18+
19+
```php
20+
require 'vendor/autoload.php';
21+
22+
class RoboFile extends \Robo\Tasks
23+
{
24+
use Gettext\Robo\GettextScanner;
25+
26+
/**
27+
* Scan files to find new gettext values
28+
*/
29+
public function gettext()
30+
{
31+
$this->taskGettextScanner()
32+
->extract(__DIR__.'/templates/')
33+
->extract(__DIR__.'/js/', '/.*\.js/') //directory + regex
34+
->generate(__DIR__.'/Locale/gl/LC_MESSAGES/messages.mo')
35+
->generate(__DIR__.'/Locale/es/LC_MESSAGES/messages.mo')
36+
->generate(__DIR__.'/Locale/en/LC_MESSAGES/messages.mo')
37+
->run();
38+
}
39+
}
40+
```
41+
42+
Use robo to run the code:
43+
44+
```bash
45+
robo gettext
46+
```

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "gettext/robo",
3+
"type": "library",
4+
"description": "Robo tasks to manage gettext",
5+
"keywords": ["robo", "tasks", "js", "gettext", "i18n", "translation", "po", "mo"],
6+
"homepage": "https://github.com/oscarotero/GettextRobo",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Oscar Otero",
11+
"email": "[email protected]",
12+
"homepage": "http://oscarotero.com",
13+
"role": "Developer"
14+
}
15+
],
16+
"support": {
17+
"email": "[email protected]",
18+
"issues": "https://github.com/oscarotero/GettextRobo/issues"
19+
},
20+
"require": {
21+
"php": ">=5.3.0",
22+
"gettext/gettext": "3.*"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Gettext\\Robo\\": "src"
27+
}
28+
}
29+
}

src/GettextScanner.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
namespace Gettext\Robo;
3+
4+
use Gettext\Translations;
5+
use FilesystemIterator;
6+
use MultipleIterator;
7+
use RecursiveDirectoryIterator;
8+
use RecursiveIteratorIterator;
9+
use RecursiveRegexIterator;
10+
use RegexIterator;
11+
use Robo\Task\BaseTask;
12+
use Robo\Contract\TaskInterface;
13+
14+
/**
15+
* Trait to scan files to get gettext entries
16+
*/
17+
trait GettextScanner
18+
{
19+
/**
20+
* Init a gettext task
21+
*/
22+
protected function taskGettextScanner()
23+
{
24+
return new TaskGettextScanner();
25+
}
26+
}
27+
28+
class TaskGettextScanner extends BaseTask implements TaskInterface
29+
{
30+
protected $iterator;
31+
protected $targets = [];
32+
33+
public function __construct()
34+
{
35+
$this->iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
36+
}
37+
38+
/**
39+
* Add a new source folder
40+
*
41+
* @param string $path
42+
* @param null|string $regex
43+
*
44+
* @return $this
45+
*/
46+
public function extract($path, $regex = null)
47+
{
48+
$directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
49+
$iterator = new RecursiveIteratorIterator($directory);
50+
51+
if ($regex) {
52+
$iterator = new RegexIterator($iterator, $regex, RecursiveRegexIterator::GET_MATCH);
53+
}
54+
55+
$this->iterator->attachIterator($iterator);
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* Add a new target
62+
*
63+
* @param string $path
64+
*
65+
* @return $this
66+
*/
67+
public function generate($path)
68+
{
69+
$this->targets[] = $path;
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* Run the task
76+
*/
77+
public function run()
78+
{
79+
foreach ($this->targets as $target) {
80+
$translations = new Translations();
81+
82+
$this->scan($translations);
83+
84+
if (is_file($target)) {
85+
$fn = $this->getFunctionName('from', $target, 'File');
86+
87+
$translations->mergeWith(Translations::$fn($target), Translations::MERGE_HEADERS | Translations::MERGE_LANGUAGE | Translations::MERGE_PLURAL | Translations::MERGE_COMMENTS);
88+
}
89+
90+
$fn = $this->getFunctionName('to', $target, 'File');
91+
$dir = dirname($target);
92+
93+
if (!is_dir($dir)) {
94+
mkdir($dir, 0777, true);
95+
}
96+
97+
$translations->$fn($target);
98+
99+
$this->printTaskInfo("Gettext exported to {$target}");
100+
}
101+
}
102+
103+
/**
104+
* Execute the scan
105+
*
106+
* @param Translations $translations
107+
*/
108+
private function scan(Translations $translations)
109+
{
110+
foreach ($this->iterator as $each) {
111+
foreach ($each as $file) {
112+
if ($file === null || !$file->isFile()) {
113+
continue;
114+
}
115+
116+
$target = $file->getPathname();
117+
118+
if (($fn = $this->getFunctionName('addFrom', $target, 'File'))) {
119+
$translations->$fn($target);
120+
}
121+
}
122+
}
123+
}
124+
125+
/**
126+
* Get the format based in the extension
127+
*
128+
* @param string $file
129+
*
130+
* @return string|null
131+
*/
132+
private function getFunctionName($prefix, $file, $suffix)
133+
{
134+
switch (pathinfo($file, PATHINFO_EXTENSION)) {
135+
case 'php':
136+
return "{$prefix}PhpCode{$suffix}";
137+
138+
case 'js':
139+
return "{$prefix}JsCode{$suffix}";
140+
141+
case 'po':
142+
return "{$prefix}Po{$suffix}";
143+
144+
case 'mo':
145+
return "{$prefix}Mo{$suffix}";
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)