Skip to content

Commit 8705bcc

Browse files
committed
Initial commit.
0 parents  commit 8705bcc

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
This project adheres to [Semantic Versioning](http://semver.org/).
4+
5+
## [0.1.0] - 2016-08-15
6+
### Added
7+
- Initial release to GitHub.
8+
9+
[0.1.0]: https://github.com/brightnucleus/localization/compare/v0.0.0...v0.1.0

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Alain Schlesser, Bright Nucleus
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Bright Nucleus Localization
2+
3+
[![Latest Stable Version](https://img.shields.io/packagist/v/brightnucleus/localization.svg)](https://packagist.org/packages/brightnucleus/localization)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/brightnucleus/localization.svg)](https://packagist.org/packages/brightnucleus/localization)
5+
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/brightnucleus/localization.svg)](https://packagist.org/packages/brightnucleus/localization)
6+
[![License](https://img.shields.io/packagist/l/brightnucleus/localization.svg)](https://packagist.org/packages/brightnucleus/localization)
7+
8+
WordPress localization for Bright Nucleus components.
9+
10+
## Table Of Contents
11+
12+
* [Installation](#installation)
13+
* [Basic Usage](#basic-usage)
14+
* [Filters](#filters)
15+
* [Contributing](#contributing)
16+
* [License](#license)
17+
18+
## Installation
19+
20+
The best way to use this package is through Composer:
21+
22+
```BASH
23+
composer require brightnucleus/localization
24+
```
25+
26+
## Basic Usage
27+
28+
This package provides a trait to be used within Bright Nucleus components to load the translation files (compiled `*.mo`-files) for a given locale.
29+
30+
To use the trait, import it into your class, and call the `loadLocalization( $domain, $path )` method.
31+
32+
__Example:__
33+
34+
```PHP
35+
<?php namespace Localization\Example;
36+
37+
use BrightNucleus\Localization\LocalizationTrait;
38+
39+
class TranslatedClass {
40+
41+
use LocalizationTrait;
42+
43+
public function register() {
44+
45+
$this->loadLocalization(
46+
'bn-localization-example',
47+
__DIR__ . '/../languages'
48+
);
49+
50+
// Your normal registration comes here, knowing that all `gettext`
51+
// strings have already been translated.
52+
}
53+
}
54+
```
55+
56+
## Filters
57+
58+
Each loading of localization files passes through two filters:
59+
60+
1. __`Localization::LOCALE_FILTER`__ - Filter the locale of a Bright Nucleus library.
61+
62+
Arguments:
63+
* `$locale` (string) - The plugin's current locale.
64+
* `$domain` (string) - Text domain. Unique identifier for retrieving translated strings.
65+
66+
Return value:
67+
* `$locale` (string) - Filtered locale.
68+
69+
2. __`Localization::MOFILE_FILTER`__ - Filter the name of the MO-file of a Bright Nucleus library.
70+
71+
Arguments:
72+
* `$path` (string) - Path to the MO-file.
73+
* `$locale` (string) - The plugin's current locale.
74+
* `$domain` (string) - Text domain. Unique identifier for retrieving translated strings.
75+
76+
Return value:
77+
* `$path` (string) - Fitlered path to the MO-file.
78+
79+
## Contributing
80+
81+
All feedback / bug reports / pull requests are welcome.
82+
83+
## License
84+
85+
Copyright (c) 2016 Alain Schlesser, Bright Nucleus
86+
87+
This code is licensed under the [MIT License](LICENSE).

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "brightnucleus/localization",
3+
"description": "WordPress localization for Bright Nucleus components.",
4+
"minimum-stability": "dev",
5+
"prefer-stable": true,
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Alain Schlesser",
10+
"email": "alain.schlesser@gmail.com"
11+
}
12+
],
13+
"require": {
14+
},
15+
"require-dev": {
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"BrightNucleus\\Localization\\": "src/"
20+
}
21+
}
22+
}

src/Localization.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Bright Nucleus Localization.
4+
*
5+
* WordPress localization for Bright Nucleus components.
6+
*
7+
* @package BrightNucleus\Localization
8+
* @author Alain Schlesser <alain.schlesser@gmail.com>
9+
* @license MIT
10+
* @link https://www.brightnucleus.com/
11+
* @copyright 2016 Alain Schlesser, Bright Nucleus
12+
*/
13+
14+
namespace BrightNucleus\Localization;
15+
16+
/**
17+
* Class Localization.
18+
*
19+
* @since 0.1.0
20+
*
21+
* @package BrightNucleus\Localization
22+
* @author Alain Schlesser <alain.schlesser@gmail.com>
23+
*/
24+
class Localization {
25+
26+
/*
27+
* Name of the filter used to filter the locale to be used.
28+
*/
29+
const LOCALE_FILTER = 'bn_library_locale';
30+
31+
/*
32+
* Name of the filter used to filter the MO-file to use.
33+
*/
34+
const MOFILE_FILTER = 'bn_library_mofile';
35+
}

src/LocalizationTrait.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Bright Nucleus Localization.
4+
*
5+
* WordPress localization for Bright Nucleus components.
6+
*
7+
* @package BrightNucleus\Localization
8+
* @author Alain Schlesser <alain.schlesser@gmail.com>
9+
* @license MIT
10+
* @link https://www.brightnucleus.com/
11+
* @copyright 2016 Alain Schlesser, Bright Nucleus
12+
*/
13+
14+
namespace BrightNucleus\Localization;
15+
16+
/**
17+
* Trait LocalizationTrait.
18+
*
19+
* @since 0.1.0
20+
*
21+
* @package BrightNucleus\Localization
22+
* @author Alain Schlesser <alain.schlesser@gmail.com>
23+
*/
24+
trait LocalizationTrait {
25+
26+
/**
27+
* Load localized strings.
28+
*
29+
* @since 0.1.0
30+
*
31+
* @param string $domain Textdomain to load.
32+
* @param string $path Path to languages files.
33+
*/
34+
protected function loadLocalization( $domain, $path ) {
35+
if ( is_textdomain_loaded( $domain ) ) {
36+
return;
37+
}
38+
39+
$locale = get_locale();
40+
41+
/**
42+
* Filter the locale of a Bright Nucleus library.
43+
*
44+
* @since v0.1.0
45+
*
46+
* @param string $locale The plugin's current locale.
47+
* @param string $domain Text domain. Unique identifier for retrieving
48+
* translated strings.
49+
* @return string $locale Filtered locale.
50+
*/
51+
$locale = apply_filters(
52+
Localization::LOCALE_FILTER,
53+
$locale,
54+
$domain
55+
);
56+
57+
/**
58+
* Filter the name of the MO-file of a Bright Nucleus library.
59+
*
60+
* @since v0.1.0
61+
*
62+
* @param string $path Path to the MO-file.
63+
* @param string $locale The plugin's current locale.
64+
* @param string $domain Text domain. Unique identifier for retrieving
65+
* translated strings.
66+
* @return string $path Filtered path to the MO-file.
67+
*/
68+
$mofile = apply_filters(
69+
Localization::MOFILE_FILTER,
70+
sprintf(
71+
'%1$s/%2$s-%3$s.mo',
72+
$path,
73+
$domain,
74+
$locale
75+
),
76+
$locale,
77+
$domain
78+
);
79+
80+
return load_textdomain(
81+
$domain,
82+
$mofile
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)