Skip to content

Commit b41b3a9

Browse files
authored
Merge pull request #39 from MichaelHoste/master
Add Ruby Exporter
2 parents b96b848 + d72e2a9 commit b41b3a9

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@ To use the languages data generated from this tool you can use the `bin/export-p
4747

4848
- `php`: build a php file that can be included
4949
```bash
50-
export-plural-rules php > yourfile.php
50+
export-plural-rules --output=yourfile.php php
5151
```
5252
Then you can use that generated file in your php scripts:
5353
```php
5454
$languages = include 'yourfile.php';
5555
```
5656

57+
- `ruby`: build a ruby file that can be included
58+
```bash
59+
export-plural-rules --output=yourfile.rb ruby
60+
```
61+
Then you can use that generated file in your ruby scripts:
62+
```ruby
63+
require './yourfile.rb'
64+
PLURAL_RULES['en']
65+
```
66+
5767
- `xml`: generate an XML document ([here you can find the xsd XML schema](https://php-gettext.github.io/Languages/GettextLanguages.xsd))
5868
```bash
5969
export-plural-rules xml

src/Exporter/Ruby.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Gettext\Languages\Exporter;
4+
5+
class Ruby extends Exporter
6+
{
7+
/**
8+
* {@inheritdoc}
9+
*
10+
* @see \Gettext\Languages\Exporter\Exporter::getDescription()
11+
*/
12+
public static function getDescription()
13+
{
14+
return 'Build a Ruby hash';
15+
}
16+
17+
/**
18+
* {@inheritdoc}
19+
*
20+
* @see \Gettext\Languages\Exporter\Exporter::toStringDo()
21+
*/
22+
protected static function toStringDo($languages)
23+
{
24+
$lines = array();
25+
$lines[] = 'PLURAL_RULES = {';
26+
foreach ($languages as $lc) {
27+
$lines[] = ' \'' . $lc->id . '\' => {';
28+
$lines[] = ' \'name\' => \'' . addslashes($lc->name) . '\',';
29+
if (isset($lc->supersededBy)) {
30+
$lines[] = ' \'supersededBy\' => \'' . $lc->supersededBy . '\',';
31+
}
32+
if (isset($lc->script)) {
33+
$lines[] = ' \'script\' => \'' . addslashes($lc->script) . '\',';
34+
}
35+
if (isset($lc->territory)) {
36+
$lines[] = ' \'territory\' => \'' . addslashes($lc->territory) . '\',';
37+
}
38+
if (isset($lc->baseLanguage)) {
39+
$lines[] = ' \'baseLanguage\' => \'' . addslashes($lc->baseLanguage) . '\',';
40+
}
41+
$lines[] = ' \'formula\' => \'' . $lc->formula . '\',';
42+
$lines[] = ' \'plurals\' => ' . count($lc->categories) . ',';
43+
$catNames = array();
44+
foreach ($lc->categories as $c) {
45+
$catNames[] = "'{$c->id}'";
46+
}
47+
$lines[] = ' \'cases\' => [' . implode(', ', $catNames) . '],';
48+
$lines[] = ' \'examples\' => {';
49+
foreach ($lc->categories as $c) {
50+
$lines[] = ' \'' . $c->id . '\' => \'' . $c->examples . '\',';
51+
}
52+
$lines[] = ' },';
53+
$lines[] = ' },';
54+
}
55+
$lines[] = '}';
56+
$lines[] = '';
57+
58+
return implode("\n", $lines);
59+
}
60+
}

0 commit comments

Comments
 (0)