Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make gettext optional #173

Merged
merged 2 commits into from
Feb 19, 2024
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
21 changes: 11 additions & 10 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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');
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
5 changes: 2 additions & 3 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ruleset name="PHP: Nelson Martell Library">
<description>
The coding standard configuration for «PHP: Nelson Martell Library» package.
</description>
<description>The coding standard configuration for «PHP: Nelson Martell Library» package.</description>
<!-- Rules -->
<rule ref="PSR12" />
<rule ref="PEAR.Functions.ValidDefaultValue" />
Expand Down Expand Up @@ -44,6 +42,7 @@
</rule>
<rule ref="Generic.NamingConventions.ConstructorName" />
<rule ref="Generic.WhiteSpace.ScopeIndent">
<type>warning</type>
<properties>
<property name="indent" value="4" />
<property name="exact" value="true" />
Expand Down
16 changes: 10 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand All @@ -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.
Expand All @@ -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;

Expand All @@ -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.
Expand Down
Loading