diff --git a/.tests/tests/ImportTest.php b/.tests/tests/ImportTest.php index 0d51750..0febee2 100644 --- a/.tests/tests/ImportTest.php +++ b/.tests/tests/ImportTest.php @@ -3,7 +3,7 @@ require_once(__DIR__ . "/../lib/UloggerAPITestCase.php"); if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); } require_once(ROOT_DIR . "/helpers/config.php"); -require_once(ROOT_DIR . "/lang.php"); +require_once(ROOT_DIR . "/helpers/lang.php"); class ImportTest extends UloggerAPITestCase { @@ -609,7 +609,7 @@ class ImportTest extends UloggerAPITestCase { } public function testImportNoLongitude() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count"); @@ -656,7 +656,7 @@ class ImportTest extends UloggerAPITestCase { } public function testImportNoLatitude() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count"); @@ -703,7 +703,7 @@ class ImportTest extends UloggerAPITestCase { } public function testImportNoGPX() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count"); @@ -744,7 +744,7 @@ class ImportTest extends UloggerAPITestCase { } public function testImportCorrupt() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count"); diff --git a/.tests/tests/InternalAPITest.php b/.tests/tests/InternalAPITest.php index 29fc7de..94c0550 100644 --- a/.tests/tests/InternalAPITest.php +++ b/.tests/tests/InternalAPITest.php @@ -3,7 +3,7 @@ require_once(__DIR__ . "/../lib/UloggerAPITestCase.php"); if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); } require_once(ROOT_DIR . "/helpers/config.php"); -require_once(ROOT_DIR . "/lang.php"); +require_once(ROOT_DIR . "/helpers/lang.php"); class InternalAPITest extends UloggerAPITestCase { @@ -527,7 +527,6 @@ class InternalAPITest extends UloggerAPITestCase { /* handletrack.php */ public function testHandleTrackDeleteAdmin() { - global $lang; $this->assertTrue($this->authenticate(), "Authentication failed"); $userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); @@ -551,7 +550,6 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleTrackDeleteSelf() { - global $lang; $userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); $this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed"); @@ -575,7 +573,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleTrackDeleteOtherUser() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); $this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed"); @@ -597,7 +595,6 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleTrackUpdate() { - global $lang; $newName = "New name"; $this->assertTrue($this->authenticate(), "Authentication failed"); $userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); @@ -639,7 +636,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleTrackUpdateEmptyName() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); @@ -663,7 +660,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleTrackUpdateNonexistantTrack() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $newName = "New name"; $this->assertTrue($this->authenticate(), "Authentication failed"); $userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); @@ -687,8 +684,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleTrackMissingAction() { - global $lang; - + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $options = [ @@ -706,8 +702,7 @@ class InternalAPITest extends UloggerAPITestCase { /* handleuser.php */ public function testHandleUserMissingAction() { - global $lang; - + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $options = [ @@ -722,8 +717,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleUserNonAdmin() { - global $lang; - + $lang = (new uLang("en"))->getStrings(); $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); $this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed"); @@ -743,8 +737,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleUserSelf() { - global $lang; - + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count"); @@ -762,8 +755,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleUserEmptyLogin() { - global $lang; - + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count"); @@ -781,7 +773,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleUserNoAuth() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); @@ -825,8 +817,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleUserAddSameLogin() { - global $lang; - + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); @@ -864,7 +855,7 @@ class InternalAPITest extends UloggerAPITestCase { } public function testHandleUserUpdateEmptyPass() { - global $lang; + $lang = (new uLang("en"))->getStrings(); $this->assertTrue($this->authenticate(), "Authentication failed"); $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT)); $this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count"); diff --git a/.tests/tests/LangTest.php b/.tests/tests/LangTest.php new file mode 100644 index 0000000..4078306 --- /dev/null +++ b/.tests/tests/LangTest.php @@ -0,0 +1,32 @@ +assertNotEmpty($languages); + $this->assertArrayHasKey("en", $languages); + $this->assertArrayHasKey("pl", $languages); + $this->assertEquals("English", $languages["en"]); + $this->assertEquals("Polski", $languages["pl"]); + } + + public function testGetStrings() { + $lang = new uLang("en"); + $this->assertEquals("User", $lang->getStrings()["user"]); + $lang = new uLang("pl"); + $this->assertEquals("Użytkownik", $lang->getStrings()["user"]); + } + + public function testGetSetupStrings() { + $lang = new uLang("en"); + $this->assertEquals("Congratulations!", $lang->getSetupStrings()["congratulations"]); + $lang = new uLang("pl"); + $this->assertEquals("Gratulacje!", $lang->getSetupStrings()["congratulations"]); + } +} +?> diff --git a/helpers/lang.php b/helpers/lang.php new file mode 100644 index 0000000..d405587 --- /dev/null +++ b/helpers/lang.php @@ -0,0 +1,115 @@ +. + */ + + require_once(ROOT_DIR . "/helpers/config.php"); + + /** + * Localization + */ + class uLang { + + /** + * Available languages + * + * @var array + */ + private static $languages = [ + "en" => "English", + "pl" => "Polski", + "de" => "Deutsch", + "hu" => "Magyar", + "fr" => "Français", + "it" => "Italiano", + "es" => "Español", + "nl" => "Nederlands", + "zh" => "中文" + ]; + + /** + * Application strings + * Array of key => translation pairs + * + * @var array + */ + private $strings = []; + /** + * Setup script strings + * Array of key => translation pairs + * + * @var array + */ + private $setupStrings = []; + + /** + * Constructor + * + * @param string $language Language code (IANA) + */ + public function __construct($language = "en") { + $lang = []; + $langSetup = []; + // always load en base + require(ROOT_DIR . "/lang/en.php"); + + // override with translated strings if needed + // missing strings will be displayed in English + if ($language != "en" && array_key_exists($language, self::$languages)) { + require(ROOT_DIR . "/lang/$language.php"); + } + + // choose password messages based on config + $lang['passrules'] = isset($lang["passrules_" . uConfig::$pass_strength]) ? $lang["passrules_" . uConfig::$pass_strength] : ""; + $lang['passlenmin'] = sprintf($lang["passlenmin"], uConfig::$pass_lenmin); + $this->strings = $lang; + $this->setupStrings = $langSetup; + } + + /** + * Get supported languages array + * Language code => Native language name + * + * @return array + */ + public static function getLanguages() { + return self::$languages; + } + + /** + * Get translated strings array + * Key => translation string + * + * @return array + */ + public function getStrings() { + return $this->strings; + } + + /** + * Get translated strings array for setup script + * Key => translation string + * + * @return array + */ + public function getSetupStrings() { + return $this->setupStrings; + } + + } + + ?> \ No newline at end of file diff --git a/index.php b/index.php index 16e74df..dabfc2b 100755 --- a/index.php +++ b/index.php @@ -22,12 +22,16 @@ require_once(ROOT_DIR . "/helpers/position.php"); require_once(ROOT_DIR . "/helpers/track.php"); require_once(ROOT_DIR . "/helpers/utils.php"); - require_once(ROOT_DIR . "/lang.php"); + require_once(ROOT_DIR . "/helpers/lang.php"); $login = uUtils::postString('user'); $pass = uUtils::postPass('pass'); $action = uUtils::postString('action'); + $lang = (new uLang(uConfig::$lang))->getStrings(); + $langsArr = uLang::getLanguages(); + asort($langsArr); + $auth = new uAuth(); if ($action == "auth") { $auth->checkLogin($login, $pass); @@ -169,7 +173,6 @@