Skip to content

Commit ba38853

Browse files
committed
Add ability to use multilang array as default value
1 parent 9130896 commit ba38853

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

docs/index.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,22 @@ Package registers global helper function `_p($file_key, $default, $placeholders)
5050
_p('auth.login', 'Login'); // "Login"
5151
```
5252

53-
It will create new localization file `auth.php` (if it doesn't exist) and write second parameter as language string under `login` key:
53+
It will create new localization file `auth.php` (if it doesn't exist) for fallback locale and write second parameter as language string under `login` key:
5454

5555
```php
5656
return [
5757
"login" => "Login"
5858
];
5959
```
6060

61+
Array can be used as default value:
62+
63+
```php
64+
_p('auth.login', ['en' => 'Login', 'fr' => 'Connexion']);
65+
```
66+
67+
It will create files `auth.php` for every locale from array keys and write array value as language string.
68+
6169
On second call with same file/key `_p('auth.login')`, localization string will be returned, file will remain untouched.
6270

6371
Placeholders are also supported:

src/LocalizationHelper.php

+37-41
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct($basePath)
2020

2121
$this->translator = app('translator');
2222
}
23-
23+
2424
/**
2525
* Translate the given message
2626
*
@@ -31,75 +31,78 @@ public function __construct($basePath)
3131
*/
3232
public function trans($key, $default = null, $placeholders = [])
3333
{
34-
if (gettype(__($key)) === 'array') {
34+
$translation = __($key, $placeholders);
35+
if (is_array($translation)) {
3536
return $key;
3637
}
38+
if (config('app.debug')) {
39+
if (!is_array($default)) {
40+
$default = [$this->translator->getFallback() => $default];
41+
}
42+
foreach ($default as $locale => $item) {
43+
$this->updateTranslation($key, $item, $locale);
44+
}
45+
$translation = __($key, $placeholders);
46+
}
3747

38-
$fallbackLocale = $this->translator->getFallback();
39-
40-
if ($this->canBeUpdated($default, $key, $fallbackLocale)) {
41-
42-
$path = explode('.', $key);
43-
44-
$filename = $path[0];
45-
46-
if (! isset($path[1])) return __($key, $placeholders);
47-
48-
$this->translator->addLines([$key => $default], $fallbackLocale);
48+
return $translation;
49+
}
4950

51+
private function updateTranslation($key, $default, $locale)
52+
{
53+
$path = explode('.', $key);
54+
if ($this->canBeUpdated($default, $key, $locale) && !empty($path[1])) {
55+
$this->translator->addLines([$key => $default], $locale);
5056
$this->writeToLangFile(
51-
$fallbackLocale,
52-
$this->translator->get($filename),
53-
$filename
57+
$locale,
58+
$this->translator->get($path[0]),
59+
$path[0]
5460
);
5561

5662
}
57-
return __($key, $placeholders);
5863
}
5964

60-
private function canBeUpdated($default, $key, $fallbackLocale)
65+
private function canBeUpdated($default, $key, $locale)
6166
{
67+
if (!$default || $this->translator->hasForLocale($key, $locale)) {
68+
return false;
69+
}
6270
$parsedKey = $this->translator->parseKey($key);
63-
6471
[$namespace, $path, $item] = $parsedKey;
65-
6672
$items = array_filter(explode('.', $item));
67-
6873
foreach ($items as $item) {
6974
$path .= '.' . $item;
70-
if ($this->translator->has($path) && gettype($this->translator->get($path)) === 'string') {
75+
if ($this->translator->hasForLocale($path, $locale)
76+
&& is_string($this->translator->get($path, [], $locale))) {
7177
return false;
7278
}
7379
}
7480

75-
return $default && config('app.debug')
76-
&& !$this->translator->hasForLocale($key, $fallbackLocale);
81+
return true;
7782
}
7883

7984
/**
8085
* Write to language file
81-
*
86+
*
8287
* @param $locale
8388
* @param $translations
8489
* @return bool
8590
*/
8691
private function writeToLangFile($locale, $translations, $filename)
8792
{
88-
$header = "<?php\n\nreturn ";
89-
90-
$language_file = $this->basePath . "/{$locale}/{$filename}.php";
91-
93+
$file = $this->basePath . "/{$locale}/{$filename}.php";
9294
try {
93-
if (($fp = fopen($language_file, 'w')) !== FALSE) {
94-
95+
if (($fp = fopen($file, 'w')) !== FALSE) {
96+
$header = "<?php\n\nreturn ";
9597
fputs($fp, $header . $this->var_export54($translations) . ";\n");
96-
9798
fclose($fp);
98-
99+
99100
return true;
100101
}
101102
} catch (\Exception $e) {
102-
return false;}}
103+
return false;
104+
}
105+
}
103106

104107
/**
105108
* var_export to php5.4 array syntax
@@ -112,26 +115,19 @@ private function writeToLangFile($locale, $translations, $filename)
112115
private function var_export54($var, $indent = "")
113116
{
114117
switch (gettype($var)) {
115-
116118
case "string":
117119
return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
118-
119120
case "array":
120121
$indexed = array_keys($var) === range(0, count($var) - 1);
121-
122122
$r = [];
123-
124123
foreach ($var as $key => $value) {
125124
$r[] = "$indent "
126125
. ($indexed ? "" : $this->var_export54($key) . " => ")
127126
. $this->var_export54($value, "$indent ");
128127
}
129-
130128
return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
131-
132129
case "boolean":
133130
return $var ? "TRUE" : "FALSE";
134-
135131
default:
136132
return var_export($var, TRUE);
137133
}

0 commit comments

Comments
 (0)