diff --git a/README.md b/README.md
index 335934d..9768d05 100644
--- a/README.md
+++ b/README.md
@@ -66,11 +66,11 @@ At this time your application have full gettext support. Now you need to set som
```php
/**
- * Default locale: this will be the default for your application all
- * localized strings. Is to be supposed that all strings are written
+ * Default locale: this will be the default for your application all
+ * localized strings. Is to be supposed that all strings are written
* on this language.
*/
- 'locale' => 'es_ES',
+ 'locale' => 'es_ES',
```
```php
@@ -82,7 +82,7 @@ At this time your application have full gettext support. Now you need to set som
'en_US',
'it_IT',
'es_AR',
- ),
+ ),
```
```php
@@ -92,7 +92,7 @@ At this time your application have full gettext support. Now you need to set som
'encoding' => 'UTF-8',
```
-Ok, now is configured. It's time to generate the directory structure and translation files for first time.
+Ok, now is configured. It's time to generate the directory structure and translation files for first time.
> Make sure you have write permissions on ```storage/``` before run this command
@@ -106,32 +106,59 @@ With this command the needed directories and files are created on **resources/la
##### A. Write strings :D
-By default *LaravelGettext* looks on app/Http/Controllers and resources/views recursively searching for translations. Translations are all texts printed with the **_()** function. Let's look a simple view example:
+By default *LaravelGettext* looks on app/Http/Controllers and resources/views recursively searching for translations. Translations are all texts printed with the **__()** function. Let's look a simple view example:
```php
// an example view file
echo 'Non translated string';
- echo _('Translated string');
- echo _('Another translated string');
+ echo __('Translated string');
+ echo __('Another translated string');
// with parameter
$str = 'parameter';
- echo sprintf(_('Translated string with %s'), $str);
+ $n = 2;
+ echo __('Translated string with %s', $str);
+ echo __('%dnd translated string with %s', [$n, $str]);
```
```php
// an example view in blade
- {{ _('Translated string') }}
+ {{ __('Translated string') }}
```
> PoEdit doesn't "understand" blade syntax. When using blade views you must run ```php artisan gettext:update``` in order to compile all blade views to plain php before update the translations in PoEdit
-##### B. Translate with PoEdit
+##### B. Plural strings
+
+The plural translations follow the same pattern above. Plural translations are all texts printed with the **_n()** function, and it follow the php ngettext. Let's look a simple view example:
+
+```php
+ // an example view file
+ $n = 2;
+ echo ($n > 1) ? 'Non translated plural string' : 'Non translated string';
+ echo _n('Translated string', 'Translated plural string', $n);
+ // with parameter
+ $str = 'parameter';
+ echo _n('Translated string %s', 'Translated plural string %s', 2, $str);
+```
+
+```php
+ // an example view in blade
+ {{ _n('Translated string', 'Translated plural string', $n) }}
+```
+
+> The PoEdit keywords are defined in configuration file with this default pattern:
+```php
+ ['_n:1,2', 'ngettext:1,2']
+```
+See Plural forms used by PoEdit to configure for your language.
+
+##### C. Translate with PoEdit
Open the PO file for the language that you want to translate with PoEdit. The PO files are located by default in **resources/lang/i18n/[locale]/LC_MESSAGES/[domain].po**. If you have multiple gettext domains, one file is generated by each domain.
-Once PoEdit is loaded press the Update button to load all localized strings. You can repeat this step anytime you add a new localized string.
+Once PoEdit is loaded press the Update button to load all localized strings. You can repeat this step anytime you add a new localized string.
Fill translation fields in PoEdit and save the file. The first time that you do this the MO files will be generated for each locale.
@@ -173,8 +200,8 @@ To change configuration on runtime you have these methods:
```php
/**
* Sets the Current encoding.
- * Example param value: 'UTF-8'
- *
+ * Example param value: 'UTF-8'
+ *
* @param mixed $encoding the encoding
* @return LaravelGettext
*/
@@ -184,8 +211,8 @@ To change configuration on runtime you have these methods:
```php
/**
* Gets the Current encoding.
- * Example returned value: 'UTF-8'
- *
+ * Example returned value: 'UTF-8'
+ *
* @return String
*/
LaravelGettext::getEncoding();
@@ -194,7 +221,7 @@ To change configuration on runtime you have these methods:
```php
/**
* Sets the current domain
- *
+ *
* @param String $domain
*/
LaravelGettext::setDomain($domain);
@@ -213,12 +240,12 @@ To change configuration on runtime you have these methods:
/**
* Returns the language selector object
*
- * @param Array $labels
- * @return LanguageSelector
+ * @param Array $labels
+ * @return LanguageSelector
*/
LaravelGettext::getSelector($labels = []);
```
-
+
### 5. Features and examples:
@@ -228,7 +255,7 @@ app/Http/routes.php
```php
Route::get('/lang/{locale?}', [
- 'as'=>'lang',
+ 'as'=>'lang',
'uses'=>'HomeController@changeLang'
]);
```
@@ -241,7 +268,7 @@ app/Http/Controllers/HomeController.php
* @return Redirect
*/
public function changeLang($locale=null){
-
+
LaravelGettext::setLocale($locale);
return Redirect::to(URL::previous());
@@ -274,15 +301,15 @@ It also supports custom labels:
'es_ES' => 'Spanish',
'de_DE' => 'Deutsch',
])->render();
-```
+```
#### D. Adding source directories and domains
-You can achieve this editing the **source-paths** configuration array. By default resources/views and app/Http/Controllers are set.
+You can achieve this editing the **source-paths** configuration array. By default resources/views and app/Http/Controllers are set.
```php
/**
- * Paths where PoEdit will search recursively for strings to translate.
+ * Paths where PoEdit will search recursively for strings to translate.
* All paths are relative to app/ (don't use trailing slash).
*
* Remember to call artisan gettext:update after change this.
@@ -343,7 +370,7 @@ Sometimes when you edit/add translations on PO files the changes does not appear
If you want to help with the development of this package, you can:
-- Warn about errors that you find, in issues section
+- Warn about errors that you find, in issues section
- Send me a pull request with your patch
- Fix my disastrous English in the documentation/comments ;-)
- Make a fork and create your own version of laravel-gettext
diff --git a/composer.json b/composer.json
index 0d57c6d..66431a7 100644
--- a/composer.json
+++ b/composer.json
@@ -9,7 +9,7 @@
"laravel-gettext",
"laravel", "translation"
],
- "license": "MIT",
+ "license": "MIT",
"authors": [
{
"name": "Nicolás Daniel Palumbo",
@@ -18,7 +18,7 @@
],
"support": {
"issues": "https://github.com/xinax/laravel-gettext/issues"
- },
+ },
"require": {
"php": ">=5.4.0",
"laravel/framework": "5.*"
@@ -31,7 +31,10 @@
"autoload": {
"psr-0": {
"Xinax\\LaravelGettext\\": "src/"
- }
+ },
+ "files": [
+ "src/Xinax/LaravelGettext/Support/helpers.php"
+ ]
},
"minimum-stability": "stable"
}
diff --git a/src/Xinax/LaravelGettext/Config/ConfigManager.php b/src/Xinax/LaravelGettext/Config/ConfigManager.php
index 064b3d8..52197c0 100644
--- a/src/Xinax/LaravelGettext/Config/ConfigManager.php
+++ b/src/Xinax/LaravelGettext/Config/ConfigManager.php
@@ -106,10 +106,14 @@ protected function generateFromArray(array $config)
$container->setRelativePath($config['relative-path']);
}
- if (array_key_exists("custom-locale", $config)) {
+ if (array_key_exists("custom-locale", $config)) {
$container->setCustomLocale($config['custom-locale']);
}
+ if (array_key_exists("keywords-list", $config)) {
+ $container->setKeywordsList($config['keywords-list']);
+ }
+
return $container;
}
}
diff --git a/src/Xinax/LaravelGettext/Config/Models/Config.php b/src/Xinax/LaravelGettext/Config/Models/Config.php
index 415f990..5912781 100644
--- a/src/Xinax/LaravelGettext/Config/Models/Config.php
+++ b/src/Xinax/LaravelGettext/Config/Models/Config.php
@@ -88,7 +88,7 @@ class Config
* @type Boolean
*/
protected $customLocale;
-
+
/**
* Default relative path
*
@@ -96,6 +96,13 @@ class Config
*/
protected $relativePath;
+ /**
+ * Poedit keywords list
+ *
+ * @type array
+ */
+ protected $keywordsList;
+
public function __construct()
{
$this->encoding = 'UTF-8';
@@ -105,11 +112,13 @@ public function __construct()
$this->relativePath = "../../../../../app";
}
- public function getRelativePath(){
+ public function getRelativePath()
+ {
return $this->relativePath;
}
- public function setRelativePath($path){
+ public function setRelativePath($path)
+ {
$this->relativePath = $path;
}
@@ -379,7 +388,7 @@ public function getCustomLocale()
{
return $this->customLocale;
}
-
+
/**
* Sets if will use C locale structure.
*
@@ -391,4 +400,28 @@ public function setCustomLocale($customLocale)
$this->customLocale = $customLocale;
return $this;
}
+
+ /**
+ * Gets the Poedit keywords list.
+ *
+ * @return mixed
+ */
+ public function getKeywordsList()
+ {
+ return !empty($this->keywordsList) ? $this->keywordsList : ['_'];
+ }
+
+ /**
+ * Sets the Poedit keywords list.
+ *
+ * @param mixed $keywordsList the keywords list
+ *
+ * @return self
+ */
+ public function setKeywordsList($keywordsList)
+ {
+ $this->keywordsList = $keywordsList;
+
+ return $this;
+ }
}
diff --git a/src/Xinax/LaravelGettext/FileSystem.php b/src/Xinax/LaravelGettext/FileSystem.php
index 343075a..4c6eeb5 100644
--- a/src/Xinax/LaravelGettext/FileSystem.php
+++ b/src/Xinax/LaravelGettext/FileSystem.php
@@ -162,6 +162,8 @@ public function createPOFile($path, $locale, $domain, $write = true)
$relativePath = $this->configuration->getRelativePath();
+ $keywords = implode(';', $this->configuration->getKeywordsList());
+
$template = 'msgid ""' . "\n";
$template .= 'msgstr ""' . "\n";
$template .= '"Project-Id-Version: ' . $project . '\n' . "\"\n";
@@ -174,7 +176,7 @@ public function createPOFile($path, $locale, $domain, $write = true)
$template .= '"Content-Type: text/plain; charset=' . $encoding . '\n' . "\"\n";
$template .= '"Content-Transfer-Encoding: 8bit' . '\n' . "\"\n";
$template .= '"X-Generator: Poedit 1.5.4' . '\n' . "\"\n";
- $template .= '"X-Poedit-KeywordsList: _' . '\n' . "\"\n";
+ $template .= '"X-Poedit-KeywordsList: ' . $keywords . '\n' . "\"\n";
$template .= '"X-Poedit-Basepath: ' . $relativePath . '\n' . "\"\n";
$template .= '"X-Poedit-SourceCharset: ' . $encoding . '\n' . "\"\n";
@@ -242,7 +244,7 @@ public function addLocale($localePath, $locale)
$this->createDirectory($localePath);
}
- if ( $this->configuration->getCustomLocale() ) {
+ if ($this->configuration->getCustomLocale()) {
$data[1] = 'C';
$gettextPath = implode($data, DIRECTORY_SEPARATOR);
@@ -293,10 +295,9 @@ public function updateLocale($localePath, $locale, $domain)
$domain . ".po",
];
- if ( $this->configuration->getCustomLocale() ) {
+ if ($this->configuration->getCustomLocale()) {
$customLocale = array('C');
-
- array_splice( $data, 1, 0, $customLocale );
+ array_splice($data, 1, 0, $customLocale);
}
$localePOPath = implode($data, DIRECTORY_SEPARATOR);
@@ -565,12 +566,12 @@ public static function clearDirectory($path)
foreach ($files as $fileinfo) {
// if the file isn't a .gitignore file we should remove it.
- if($fileinfo->getFilename() !== '.gitignore'){
+ if ($fileinfo->getFilename() !== '.gitignore') {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
}
-
+
// since the folder now contains a .gitignore we can't remove it
//rmdir($path);
return true;
diff --git a/src/Xinax/LaravelGettext/Support/helpers.php b/src/Xinax/LaravelGettext/Support/helpers.php
new file mode 100644
index 0000000..9df5888
--- /dev/null
+++ b/src/Xinax/LaravelGettext/Support/helpers.php
@@ -0,0 +1,42 @@
+ 1
+ * @param int $count the number of occurence to be used to pluralize the $singular
+ * @param array|mixed $argsthe tokens values used inside $singular or $plural
+ * @return string the message translated, pluralized and formated
+ */
+ function _n($singular, $plural, $count, $args = null)
+ {
+ $message = ngettext($singular, $plural, $count);
+ if (!empty($args) && !is_array($args)) {
+ $args = array_slice(func_get_args(), 3);
+ }
+ $message = vsprintf($message, $args);
+ return $message;
+ }
+}
diff --git a/src/config/config.php b/src/config/config.php
index ed6b0d3..561f9a6 100644
--- a/src/config/config.php
+++ b/src/config/config.php
@@ -102,4 +102,14 @@
* Use custom locale that is not supported by the system
*/
'custom-locale' => false,
+
+ /**
+ * The keywords list used by poedit to search the strings to be translated
+ *
+ * The "_", "__" and "gettext" are singular translation functions
+ * The "_n" and "ngettext" are plural translation functions
+ *
+ * "__" and "_n" are helpers functions @see \Xinax\LaravelGettext\Support\helpers.php
+ */
+ 'keywords-list' => ['_', '__', 'gettext', '_n:1,2', 'ngettext:1,2'],
];