Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Linter"

on: [pull_request]
jobs:
lint:
name: Linter
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Run Linter
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer lint"
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"vimeo/psalm": "4.0.1"
"vimeo/psalm": "4.0.1",
"laravel/pint": "1.2.*"
},
"scripts": {
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint"
}
}
43 changes: 22 additions & 21 deletions src/Locale/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Locale
/**
* @var array
*/
static protected $language = [];
protected static $language = [];

/**
* Throw Exceptions?
*
* @var bool
*/
static public $exceptions = true;
public static $exceptions = true;

/**
* Default Locale
Expand All @@ -28,34 +28,33 @@ class Locale
/**
* Set New Locale from an array
*
* @param string $name
* @param array $translations
* @param string $name
* @param array $translations
*/
static public function setLanguageFromArray(string $name, array $translations): void //TODO add support for lazy load to memory
public static function setLanguageFromArray(string $name, array $translations): void //TODO add support for lazy load to memory
{
self::$language[$name] = $translations;
}

/**
* Set New Locale from JSON file
*
* @param string $name
* @param string $path
* @param string $name
* @param string $path
*/
static public function setLanguageFromJSON(string $name, string $path): void
public static function setLanguageFromJSON(string $name, string $path): void
{
if (!file_exists($path)) {
if (! file_exists($path)) {
throw new Exception('Translation file not found.');
}
$translations = json_decode(file_get_contents($path),true);

$translations = json_decode(file_get_contents($path), true);
self::$language[$name] = $translations;
}


public function __construct(string $default)
{
if (!\array_key_exists($default, self::$language)) {
if (! \array_key_exists($default, self::$language)) {
throw new Exception('Locale not found');
}

Expand All @@ -66,11 +65,12 @@ public function __construct(string $default)
* Change Default Locale
*
* @param $name
*
* @throws Exception
*/
public function setDefault(string $name): self
{
if (!\array_key_exists($name, self::$language)) {
if (! \array_key_exists($name, self::$language)) {
throw new Exception('Locale not found');
}

Expand All @@ -82,18 +82,19 @@ public function setDefault(string $name): self
/**
* Get Text by Locale
*
* @param string $key
* @param mixed $default
* @param string $key
* @param mixed $default
* @return mixed
*
* @throws Exception
*/
public function getText(string $key, $placeholders = [])
{
$default = '{{' . $key . '}}';
$default = '{{'.$key.'}}';

if (!\array_key_exists($key, self::$language[$this->default])) {
if (! \array_key_exists($key, self::$language[$this->default])) {
if (self::$exceptions) {
throw new Exception('Key named "' . $key . '" not found');
throw new Exception('Key named "'.$key.'" not found');
}

return $default;
Expand All @@ -102,9 +103,9 @@ public function getText(string $key, $placeholders = [])
$translation = self::$language[$this->default][$key];

foreach ($placeholders as $placeholderKey => $placeholderValue) {
$translation = str_replace('{{' . $placeholderKey . '}}', $placeholderValue, $translation);
$translation = str_replace('{{'.$placeholderKey.'}}', $placeholderValue, $translation);
}

return $translation;
}
}
}
22 changes: 12 additions & 10 deletions tests/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
/**
* Utopia PHP Framework
*
* @package Locale
* @subpackage Tests
*
* @link https://github.com/utopia-php/framework
*
* @author Eldad Fux <eldad@appwrite.io>
*
* @version 1.0 RC4
*
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/

namespace Utopia\Tests;

use Exception;
use Utopia\Locale\Locale;
use PHPUnit\Framework\TestCase;
use Utopia\Locale\Locale;

class LocaleTest extends TestCase
{
Expand All @@ -28,7 +29,7 @@ public function setUp(): void
{
Locale::$exceptions = false; // Disable exceptions

Locale::setLanguageFromArray('en-US', ['hello' => 'Hello','world' => 'World', 'helloPlaceholder' => 'Hello {{name}} {{surname}}!', 'numericPlaceholder' => 'We have {{usersAmount}} users registered.', 'multiplePlaceholders' => 'Lets repeat: {{word}}, {{word}}, {{word}}']); // Set English
Locale::setLanguageFromArray('en-US', ['hello' => 'Hello', 'world' => 'World', 'helloPlaceholder' => 'Hello {{name}} {{surname}}!', 'numericPlaceholder' => 'We have {{usersAmount}} users registered.', 'multiplePlaceholders' => 'Lets repeat: {{word}}, {{word}}, {{word}}']); // Set English
Locale::setLanguageFromArray('he-IL', ['hello' => 'שלום']); // Set Hebrew
Locale::setLanguageFromJSON('hi-IN', realpath(__DIR__.'/../hi-IN.json')); // Set Hindi
}
Expand Down Expand Up @@ -57,20 +58,20 @@ public function testTexts()
// Test placeholders
$locale->setDefault('en-US');

$this->assertEquals('Hello Matej Bačo!', $locale->getText("helloPlaceholder", [
$this->assertEquals('Hello Matej Bačo!', $locale->getText('helloPlaceholder', [
'name' => 'Matej',
'surname' => 'Bačo'
'surname' => 'Bačo',
]));
$this->assertEquals('Hello Matej {{surname}}!', $locale->getText("helloPlaceholder", [
$this->assertEquals('Hello Matej {{surname}}!', $locale->getText('helloPlaceholder', [
'name' => 'Matej',
]));
$this->assertEquals('Hello {{name}} {{surname}}!', $locale->getText("helloPlaceholder"));
$this->assertEquals('Hello {{name}} {{surname}}!', $locale->getText('helloPlaceholder'));

$this->assertEquals('We have 12 users registered.', $locale->getText("numericPlaceholder", [
$this->assertEquals('We have 12 users registered.', $locale->getText('numericPlaceholder', [
'usersAmount' => 6 + 6,
]));

$this->assertEquals('Lets repeat: Appwrite, Appwrite, Appwrite', $locale->getText("multiplePlaceholders", [
$this->assertEquals('Lets repeat: Appwrite, Appwrite, Appwrite', $locale->getText('multiplePlaceholders', [
'word' => 'Appwrite',
]));

Expand All @@ -82,6 +83,7 @@ public function testTexts()
$locale->getText('world');
} catch (\Throwable $exception) {
$this->assertInstanceOf(Exception::class, $exception);

return;
}

Expand Down