Skip to content
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

----

## v2.1.2 (2022-01-15)
## v2.1.3 (2022-01-16)

- Added more info to README.md file about using second argument in Lang::set() method.
- Improved performance of the program by caching included translations and reusing them.

----

## v2.1.2 (2022-01-16)

- Added flags to supported languages table in README.md file.
- Added ability to overwrite translations. Closes issue [#20](https://github.com/SerhiiCho/ago/issues/20).
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- [✏️ Description](#description)
- [🐘 Supported PHP versions](#supported-php-versions)
- [⚙️ Configurations](#configurations)
- [Set language](#set-language)
- [Overwrite translations](#overwrite-translations)
- [👏 Usage](#usage)
- [🚩 Supported languases](#supported-languages)
- [🤲 Options](#options)
Expand All @@ -22,7 +24,7 @@

Date/time converter into "n time ago" format that supports multiple languages. You can contribute any language that you wish easily by creating a pull request. I would gladly merge it in if you follow the simple steps.

This package is well tested and already used in many production apps and has shown itself very well. If you find any issues or bugs 🐞, please create an [issue](https://github.com/SerhiiCho/ago/issues/new), and I'll fix it as soon as I can.
This package is well tested, optimized and already used in many production apps. It has shown itself pretty well. If you find any issues or bugs 🐞, please create an [issue](https://github.com/SerhiiCho/ago/issues/new), and I'll fix it as soon as I can.

## Supported PHP versions

Expand All @@ -35,6 +37,8 @@ This package is well tested and already used in many production apps and has sho

## Configurations

### Set language

Default language is English. Optionally you can set the language in your application by calling `set()` method and passing a flag `ru` for Russian or `en` for English language. You can see supported languages in the next section.

```php
Expand All @@ -49,6 +53,21 @@ Serhii\Ago\Lang::set('ru');
| 🇷🇺 | Russian | ru |
| 🇺🇦 | Ukrainian | uk |

### Overwrite translations
There are cases when you want to replace certain words with specific ones. You can do it with “Overwrites”. All you need to do is just to pass `array<string, string>` of values that you want to overwrite.

For example, instead of `1 minute ago` you want to have the output `1 minute before`. To achieve that, create `['ago' => 'before']` array and pass it as the second argument to method `set()` in `Serhii\Ago\Lang` class.

```php
Lang::set('en', [
'ago' => 'before',
'day' => 'Day',
'days' => 'Days',
]);
```

> The list of all default key values you can find in [resources/lang](https://github.com/SerhiiCho/ago/tree/master/resources/lang) directory.

## Usage

For outputting post publishing date or something else you can just pass the date to method `trans()`. It will count the interval between now and given date and returns needed format. Internally given date will be parsed by `strtotime()` PHP's internal function.
Expand Down
12 changes: 11 additions & 1 deletion src/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Lang
*/
private static $rules;

/**
* @var array<string, array<string, string>>
*/
private static $included_files_cache = [];

/**
* Set the language by passing short representation of a language
* like 'ru' for russian or 'en' for english.
Expand Down Expand Up @@ -94,7 +99,12 @@ private static function getLanguagesSlugs(): array
*/
public static function includeTranslations(): void
{
$translations = require __DIR__ . '/../resources/lang/' . self::$lang . '.php';
$path = __DIR__ . '/../resources/lang/' . self::$lang . '.php';
$cached_translations = self::$included_files_cache[$path] ?? [];

$translations = \count($cached_translations) > 0 ? $cached_translations : require $path;

self::$included_files_cache[$path] = $translations;

if (\count(self::$overwrites) > 0) {
$translations = \array_merge($translations, self::$overwrites);
Expand Down