Skip to content

Commit

Permalink
added multilanguage support
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed May 24, 2016
1 parent 02516de commit 659273d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,27 @@ echo $db->getAttribute('language'); // en
//You can access to PDO attributes, using constants:
echo $db->getAttribute(PDO::ATTR_DRIVER_NAME); //sqlite
```

### Localizable fields

If you need to save values in multiple languages, just have to create a field for each language using the language as suffix. For example, to save the title in "en" and "gl", just create the fields `title_en` and `title_gl`.

Then, you have to configure the current language using the `SimpleCrud::ATTR_LOCALE` attribute:

```php
//Set the current language as "en"
$db->setAttribute(SimpleCrud::ATTR_LOCALE, 'en');

//Select a post
$post = $db->post[23];

//Get the title in the current language
echo $post->title; //Returns the value of title_en

//You can access to any languages using the full name:
echo $post->title_en;
echo $post->title_gl;

//And assign a diferent value to the current language
$post->title = 'New title in english';
```
26 changes: 26 additions & 0 deletions src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ public function __get($name)
return $this->relations[$name] ?: new NullValue();
}

//It's a localizable field
$language = $this->getDatabase()->getAttribute(SimpleCrud::ATTR_LOCALE);

if (!is_null($language)) {
$localeName = "{$name}_{$language}";

if (array_key_exists($localeName, $this->values)) {
return $this->values[$localeName];
}
}

//Load the relation
$scheme = $this->getTable()->getScheme();

Expand Down Expand Up @@ -107,6 +118,21 @@ public function __set($name, $value)
return $this->values[$name] = $value;
}

//It's a localizable field
$language = $this->getDatabase()->getAttribute(SimpleCrud::ATTR_LOCALE);

if (!is_null($language)) {
$localeName = "{$name}_{$language}";

if (array_key_exists($localeName, $this->values)) {
if ($this->values[$localeName] !== $value) {
$this->changed = true;
}

return $this->values[$localeName] = $value;
}
}

//It's a relation
$table = $this->getTable();
$scheme = $table->getScheme();
Expand Down
2 changes: 2 additions & 0 deletions src/SimpleCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class SimpleCrud
{
const ATTR_LOCALE = 'simplecrud.language';

protected $connection;
protected $scheme;
protected $tables = [];
Expand Down
45 changes: 45 additions & 0 deletions tests/LocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use SimpleCrud\SimpleCrud;
use SimpleCrud\Table;

class LocaleTest extends PHPUnit_Framework_TestCase
{
private $db;

public function setUp()
{
$this->db = new SimpleCrud(new PDO('sqlite::memory:'));

$this->db->executeTransaction(function ($db) {
$db->execute(
<<<EOT
CREATE TABLE "post" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`title_gl` TEXT,
`title_es` TEXT
);
EOT
);
});
}

public function testRow()
{
$db = $this->db;
$db->setAttribute(SimpleCrud::ATTR_LOCALE, 'gl');

$post = $db->post->create();

$post->title = 'Galego';

$this->assertSame($post->title, $post->title_gl);

$db->setAttribute(SimpleCrud::ATTR_LOCALE, 'es');
$this->assertNotSame($post->title, $post->title_gl);
$this->assertSame($post->title, $post->title_es);

$post->title_es = 'Español';
$this->assertNotSame($post->title_gl, $post->title_es);
}
}

0 comments on commit 659273d

Please sign in to comment.