Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Merge pull request #62 from jonatascabral/master
Browse files Browse the repository at this point in the history
X-Poedit-KeywordsList config and helpers functions
  • Loading branch information
shaggyz committed Mar 10, 2016
2 parents 53b9062 + 52952ab commit 442a828
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 41 deletions.
79 changes: 53 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 <a href="http://php.net/manual/en/function.ngettext.php">php ngettext</a>. 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 <a href="http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms">Plural forms</a> 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.

<img src="https://raw.github.com/xinax/laravel-gettext/master/doc/poedit.png" />

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.

Expand Down Expand Up @@ -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
*/
Expand All @@ -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();
Expand All @@ -194,7 +221,7 @@ To change configuration on runtime you have these methods:
```php
/**
* Sets the current domain
*
*
* @param String $domain
*/
LaravelGettext::setDomain($domain);
Expand All @@ -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:

Expand All @@ -228,7 +255,7 @@ app/Http/routes.php

```php
Route::get('/lang/{locale?}', [
'as'=>'lang',
'as'=>'lang',
'uses'=>'HomeController@changeLang'
]);
```
Expand All @@ -241,7 +268,7 @@ app/Http/Controllers/HomeController.php
* @return Redirect
*/
public function changeLang($locale=null){

LaravelGettext::setLocale($locale);
return Redirect::to(URL::previous());

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"laravel-gettext",
"laravel", "translation"
],
"license": "MIT",
"license": "MIT",
"authors": [
{
"name": "Nicolás Daniel Palumbo",
Expand All @@ -18,7 +18,7 @@
],
"support": {
"issues": "https://github.com/xinax/laravel-gettext/issues"
},
},
"require": {
"php": ">=5.4.0",
"laravel/framework": "5.*"
Expand All @@ -31,7 +31,10 @@
"autoload": {
"psr-0": {
"Xinax\\LaravelGettext\\": "src/"
}
},
"files": [
"src/Xinax/LaravelGettext/Support/helpers.php"
]
},
"minimum-stability": "stable"
}
6 changes: 5 additions & 1 deletion src/Xinax/LaravelGettext/Config/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
41 changes: 37 additions & 4 deletions src/Xinax/LaravelGettext/Config/Models/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,21 @@ class Config
* @type Boolean
*/
protected $customLocale;

/**
* Default relative path
*
* @type string
*/
protected $relativePath;

/**
* Poedit keywords list
*
* @type array
*/
protected $keywordsList;

public function __construct()
{
$this->encoding = 'UTF-8';
Expand All @@ -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;
}

Expand Down Expand Up @@ -379,7 +388,7 @@ public function getCustomLocale()
{
return $this->customLocale;
}

/**
* Sets if will use C locale structure.
*
Expand All @@ -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;
}
}
15 changes: 8 additions & 7 deletions src/Xinax/LaravelGettext/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 442a828

Please sign in to comment.