Skip to content

Commit

Permalink
Add support to get setup JSON data
Browse files Browse the repository at this point in the history
For module loader usage
  • Loading branch information
elkuku committed Jul 19, 2018
1 parent bd08131 commit 808f443
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 28 additions & 6 deletions src/G11n.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use ElKuKu\G11n\Language\Parser\Code;
use ElKuKu\G11n\Language\Parser\Language;
use ElKuKu\G11n\Language\Storage;
use ElKuKu\G11n\Support\ExtensionHelper;

require_once __DIR__ . '/Language/methods.php';

Expand Down Expand Up @@ -92,6 +91,13 @@ abstract class G11n
*/
protected static $pluralFunctionJsStr = '';

/**
* The pluralization function for Javascript as a raw string.
*
* @var string
*/
protected static $pluralFunctionJsStrRaw = '';

/**
* This is for, well... debugging =;)
*
Expand Down Expand Up @@ -462,11 +468,6 @@ public static function getLanguageParser(string $type): Language
*/
protected static function setPluralFunction(string $pcrePluralForm): void
{
if (!$pcrePluralForm || ';' === $pcrePluralForm)
{
return;
}

if (preg_match("/nplurals\s*=\s*(\d+)\s*\;\s*plural\s*=\s*(.*?)\;+/", $pcrePluralForm, $matches))
{
$nplurals = $matches[1];
Expand Down Expand Up @@ -499,6 +500,8 @@ protected static function setPluralFunction(string $pcrePluralForm): void
self::$pluralFunctionRaw = $expression;

self::$pluralFunctionJsStr = "phpjs.create_function('n', '" . $jsFuncBody . "')";

self::$pluralFunctionJsStrRaw = $jsFuncBody;
}

/**
Expand All @@ -525,6 +528,25 @@ public static function getJavaScript(): string
return implode("\n", $js);
}

/**
* Get the JavaScript declaration in JSON format.
*
* @return string
*
* @since 5.0.2
*/
public static function getJavaScriptJson(): string
{
$data = new \stdClass;

$data->debug = self::$debug;
$data->strings = self::$stringsJs;
$data->stringsPlural = self::$stringsJsPlural;
$data->pluralFunction = self::$pluralFunctionJsStrRaw;

return json_encode($data);
}

/**
* Processes the final translation. Decoding and converting \n to <br /> if necessary.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static function getTemplatePath(string $extension, string $scope) : strin
*
* @return string pcre type PluralForms
*/
protected static function translatePluralForms(string $gettextPluralForms) : string
protected function translatePluralForms(string $gettextPluralForms) : string
{
$expr = $gettextPluralForms . ';';
$exprLen = \strlen($expr);
Expand Down

0 comments on commit 808f443

Please sign in to comment.