From 8fb2ec52723ead81221549e9f6e7233cbc9e5f21 Mon Sep 17 00:00:00 2001 From: Nelson Martell Date: Mon, 19 Feb 2024 13:38:11 -0500 Subject: [PATCH] feat: make gettext features optional --- autoload.php | 21 +++++++++++---------- composer.json | 7 ++++++- config/bootstrap.php | 6 +++++- src/functions.php | 16 ++++++++++------ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/autoload.php b/autoload.php index cfd23d56..7c304b98 100644 --- a/autoload.php +++ b/autoload.php @@ -7,13 +7,13 @@ * - File to perform manual autoload. For non composer instalation, must be * required at app initialization. * - * Copyright © 2015-2021 Nelson Martell (http://nelson6e65.github.io) + * Copyright © 2015-2024 Nelson Martell (http://nelson6e65.github.io) * * Licensed under The MIT License (MIT) * For full copyright and license information, please see the LICENSE * Redistributions of files must retain the above copyright notice. * - * @copyright 2015-2021 Nelson Martell + * @copyright 2015-2024 Nelson Martell * @link http://nelson6e65.github.io/php_nml/ * @since 0.3.0 * @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) @@ -67,17 +67,18 @@ function autoloadNelsonMartellLibrary($class) } elseif (is_file($path . '.inc')) { $path .= '.inc'; } else { - $msg = 'Unable to auto-load "%s" class in Nelson Martell Library (NML): "%s" file was not found.' . - ' You can see the API documentation (http://nelson6e65.github.io/php_nml/api) in order to check ' . - ' availability of all classes/namespaces in NML. Note: If you are using "NelsonMartell" as main namespace' . - ' in a file that not belongs to NML, you should include it before to load "NML/autoload.php" or,' . - ' using SPL autoload features, register autoload function for that class(es) using "prepend" argument for' . - ' spl_autoload_register function set to TRUE.'; + $msg = + 'Unable to auto-load "%s" class in Nelson Martell Library (NML): "%s" file was not found.' . + ' You can see the API documentation (http://nelson6e65.github.io/php_nml/api) in order to check ' . + ' availability of all classes/namespaces in NML. Note: If you are using "NelsonMartell" as main namespace' . + ' in a file that not belongs to NML, you should include it before to load "NML/autoload.php" or,' . + ' using SPL autoload features, register autoload function for that class(es) using "prepend" argument for' . + ' spl_autoload_register function set to TRUE.'; - throw new Exception(sprintf(dgettext('nml', $msg), $class, $path)); + throw new Exception(sprintf(NelsonMartell\msg($msg), $class, $path)); } - require_once($path); + require_once $path; } spl_autoload_register('autoloadNelsonMartellLibrary'); diff --git a/composer.json b/composer.json index 42f99c4a..36c3b5e5 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,12 @@ "docs": "http://nelson6e65.github.io/php_nml/api" }, "require": { - "php": ">=7.2" + "php": ">=7.2", + "ext-mbstring": ">=7.2", + "ext-json": "*" + }, + "suggest": { + "ext-gettext": "Required to enable translations and some functions" }, "require-dev": { "squizlabs/php_codesniffer": "^3.0", diff --git a/config/bootstrap.php b/config/bootstrap.php index ff929991..6cdacf54 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -12,11 +12,15 @@ * For full copyright and license information, please see the LICENSE * Redistributions of files must retain the above copyright notice. * - * @copyright 2015-2021 Nelson Martell + * @copyright 2015-2024 Nelson Martell * @link http://nelson6e65.github.io/php_nml/ * @since v0.5.0 * @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) * */ +if (!extension_loaded('gettext')) { + return; +} + // Set the path of translations bindtextdomain(NML_GETTEXT_DOMAIN, __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Locale'); diff --git a/src/functions.php b/src/functions.php index dc0a9836..e7d5a7ae 100644 --- a/src/functions.php +++ b/src/functions.php @@ -6,13 +6,13 @@ * Content: * - Global functions definition for NML. * - * Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io) + * Copyright © 2016-2024 Nelson Martell (http://nelson6e65.github.io) * * Licensed under The MIT License (MIT) * For full copyright and license information, please see the LICENSE * Redistributions of files must retain the above copyright notice. * - * @copyright 2016-2021 Nelson Martell + * @copyright 2016-2024 Nelson Martell * @link http://nelson6e65.github.io/php_nml/ * @since 0.6.0 * @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) @@ -38,7 +38,7 @@ * */ function msg(string $message, ...$args): string { - $translated = \dgettext(NML_GETTEXT_DOMAIN, $message); + $translated = extension_loaded('gettext') ? \dgettext(NML_GETTEXT_DOMAIN, $message) : $message; $data = $args; @@ -49,7 +49,6 @@ function msg(string $message, ...$args): string return Text::format($translated, $data); } - /** * Busca un mensaje único, en singular y plural, traducido en el dominio 'nml'. * El mensaje puede contener cadenas de formato. @@ -64,10 +63,16 @@ function msg(string $message, ...$args): string * @return string * @since 0.6.0 * @see \dngettext() + * @internal * */ function nmsg(string $singular, string $plural, int $n, ...$args): string { - $translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n); + if (extension_loaded('gettext')) { + $translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n); + } else { + // Simple implementation without Gettext + $translated = $n === 1 ? $singular : $plural; + } $data = $args; @@ -78,7 +83,6 @@ function nmsg(string $singular, string $plural, int $n, ...$args): string return Text::format($translated, $data); } - /** * Obtiene el tipo del objeto especificado. * Es un alias para el constructor de la clase Type.