Skip to content

EspoCRM – Open Source CRM Application

language manager improvements

Yuri Kuznetsov57bc86a17e9e
diff --git a/application/Espo/Core/Utils/DataUtil.php b/application/Espo/Core/Utils/DataUtil.php
index 7bfc1d2b47..e05d80bda2 100644
--- a/application/Espo/Core/Utils/DataUtil.php
+++ b/application/Espo/Core/Utils/DataUtil.php
@@ -31,6 +31,7 @@ namespace Espo\Core\Utils;
use InvalidArgumentException;
use LogicException;
+use RuntimeException;
use stdClass;
class DataUtil
@@ -213,4 +214,36 @@ class DataUtil
/** @var array<string|int, mixed>|stdClass */
return $overrideData;
}
+
+ /**
+ * @param string[] $path
+ * @since 9.2.0
+ * @internal
+ */
+ public static function setByPath(stdClass $data, array $path, mixed $value): void
+ {
+ if (count($path) === 1) {
+ $property = $path[0];
+
+ $data->$property = $value;
+
+ return;
+ }
+
+ if (count($path) < 1) {
+ return;
+ }
+
+ $property = array_shift($path);
+
+ if (!isset($data->$property)) {
+ $data->$property = (object) [];
+ }
+
+ if (!($data->$property instanceof stdClass)) {
+ throw new RuntimeException("Cannot set by path. Not an object.");
+ }
+
+ self::setByPath($data->$property, $path, $value);
+ }
}
diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php
index c58ab91370..4ca396b64b 100644
--- a/application/Espo/Core/Utils/Language.php
+++ b/application/Espo/Core/Utils/Language.php
@@ -35,6 +35,7 @@ use Espo\Core\Utils\Resource\Reader\Params as ResourceReaderParams;
use Espo\Entities\Preferences;
use RuntimeException;
+use stdClass;
class Language
{
@@ -51,13 +52,17 @@ class Language
private string $customPath = 'custom/Espo/Custom/Resources/i18n/{language}';
private string $resourcePath = 'i18n/{language}';
+ /**
+ * @param bool $noFallback As of v9.2.
+ */
public function __construct(
?string $language,
private FileManager $fileManager,
private ResourceReader $resourceReader,
private DataCache $dataCache,
protected bool $useCache = false,
- protected bool $noCustom = false
+ protected bool $noCustom = false,
+ private bool $noFallback = false,
) {
$this->currentLanguage = $language ?? $this->defaultLanguage;
@@ -216,6 +221,45 @@ class Language
return $this->get();
}
+ /**
+ * @since 9.2.0
+ */
+ public function getScopeCustom(string $scope): ?stdClass
+ {
+ $path = str_replace('{language}', $this->currentLanguage, $this->customPath) . "/$scope.json";
+
+ if (!$this->fileManager->isFile($path)) {
+ return null;
+ }
+
+ $fileContent = $this->fileManager->getContents($path);
+
+ return Json::decode($fileContent);
+ }
+
+ /**
+ * @since 9.2.0
+ */
+ public function saveScopeCustom(string $scope, stdClass $data): void
+ {
+ $path = str_replace('{language}', $this->currentLanguage, $this->customPath) . "/$scope.json";
+
+ foreach (get_object_vars($data) as $key => $item) {
+ if (
+ $item instanceof stdClass &&
+ count(get_object_vars($data)) === 0
+ ) {
+ unset($data->$key);
+ }
+ }
+
+ $changedData = Json::encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+
+ $this->fileManager->putContents($path, $changedData);
+
+ $this->init(true);
+ }
+
/**
* Save changes.
*/
@@ -382,7 +426,7 @@ class Language
$data = $this->resourceReader->readAsArray($path, $readerParams);
- if ($language != $this->defaultLanguage) {
+ if ($language !== $this->defaultLanguage && !$this->noFallback) {
/** @var array<string, array<string, mixed>> $data */
$data = Util::merge($this->getDefaultLanguageData($reload), $data);
}
diff --git a/application/Espo/Tools/LabelManager/LabelManager.php b/application/Espo/Tools/LabelManager/LabelManager.php
index 98606689a2..4a762f9f18 100644
--- a/application/Espo/Tools/LabelManager/LabelManager.php
+++ b/application/Espo/Tools/LabelManager/LabelManager.php
@@ -30,11 +30,13 @@
namespace Espo\Tools\LabelManager;
use Espo\Core\ORM\Type\FieldType;
+use Espo\Core\Utils\DataUtil;
use Espo\Core\Utils\Json;
use Espo\Core\Di;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Language;
+use RuntimeException;
use stdClass;
class LabelManager implements
@@ -86,13 +88,14 @@ class LabelManager implements
{
$languageObj = $this->injectableFactory->createWith(Language::class, [
'language' => $language,
+ 'noFallback' => true,
]);
$data = $languageObj->get($scope);
- if (empty($data)) {
+ /*if (empty($data)) {
return (object) [];
- }
+ }*/
if ($this->metadata->get(['scopes', $scope, 'entity'])) {
if (empty($data['fields'])) {
@@ -155,11 +158,11 @@ class LabelManager implements
continue;
}
- $optionsData[$option] = $option;
+ $optionsData[$option] = '';
if (
array_key_exists($option, $data['options'][$field]) &&
- !empty($data['options'][$field][$option])
+ isset($data['options'][$field][$option])
) {
$optionsData[$option] = $data['options'][$field][$option];
}
@@ -223,14 +226,12 @@ class LabelManager implements
{
$languageObj = $this->injectableFactory->createWith(Language::class, [
'language' => $language,
+ 'noFallback' => true,
]);
- $languageOriginalObj = $this->injectableFactory->createWith(Language::class, [
- 'language' => $language,
- 'noCustom' => true,
- ]);
+ $customData = $languageObj->getScopeCustom($scope) ?? (object) [];
- $returnDataHash = [];
+ $affectedPaths = [];
foreach ($labels as $key => $value) {
$arr = explode('[.]', $key);
@@ -239,59 +240,33 @@ class LabelManager implements
$setPath = [$scope, $category, $name];
- $setValue = null;
-
- if (count($arr) == 2) {
+ if (count($arr) === 2) {
if ($value !== '') {
- $languageObj->set($scope, $category, $name, $value);
-
- $setValue = $value;
+ DataUtil::setByPath($customData, [$category, $name], $value);
} else {
- $setValue = $languageOriginalObj->get(implode('.', [$scope, $category, $name]));
-
- if (is_null($setValue) && $scope !== 'Global') {
- $setValue = $languageOriginalObj->get(implode('.', ['Global', $category, $name]));
- }
-
- $languageObj->delete($scope, $category, $name);
+ DataUtil::unsetByKey($customData, [[$category, $name]], true);
+ /** @var stdClass $customData */
}
} else if (count($arr) === 3) {
$attribute = $arr[2];
- $data = $languageObj->get("$scope.$category.$name");
-
- $setPath[] = $attribute;
+ $setPath = [$scope, $category, $name, $attribute];
- if (is_array($data)) {
- if ($value !== '') {
- $data[$attribute] = $value;
- $setValue = $value;
- } else {
- $dataOriginal = $languageOriginalObj->get("$scope.$category.$name");
-
- if (is_array($dataOriginal) && isset($dataOriginal[$attribute])) {
- $data[$attribute] = $dataOriginal[$attribute];
- $setValue = $dataOriginal[$attribute];
- }
- }
-
- $languageObj->set($scope, $category, $name, $data);
+ if ($value !== '') {
+ DataUtil::setByPath($customData, [$category, $name, $attribute], $value);
+ } else {
+ DataUtil::unsetByKey($customData, [[$category, $name, $attribute]], true);
+ /** @var stdClass $customData */
}
}
- if (!is_null($setValue)) {
- $frontKey = implode('[.]', $setPath);
+ $frontKey = implode('[.]', $setPath);
- $returnDataHash[$frontKey] = $setValue;
- }
+ $affectedPaths[$frontKey] = implode('.', $setPath);
}
- $languageObj->save();
-
- if ($returnDataHash === []) {
- return (object) [];
- }
+ $languageObj->saveScopeCustom($scope, $customData);
- return json_decode(Json::encode($returnDataHash));
+ return $this->getScopeData($language, $scope);
}
}
diff --git a/client/src/views/admin/label-manager/edit.js b/client/src/views/admin/label-manager/edit.js
index 2089782870..4aabc27826 100644
--- a/client/src/views/admin/label-manager/edit.js
+++ b/client/src/views/admin/label-manager/edit.js
@@ -175,7 +175,8 @@ class LabelManagerEditView extends View {
language: this.language,
labels: data,
})
- .then(returnData => {
+ .then(/** Record.<string, Record<string, string>>*/returnData => {
+ this.scopeData = returnData;
this.scopeDataInitial = Espo.Utils.cloneDeep(this.scopeData);
this.dirtyLabelList = [];
this.setConfirmLeaveOut(false);
@@ -184,9 +185,17 @@ class LabelManagerEditView extends View {
this.$cancel.removeClass('disabled').removeAttr('disabled');
for (const key in returnData) {
- const name = key.split('[.]').splice(1).join('[.]');
+ const items = returnData[key];
- this.$el.find(`input.label-value[data-name="${name}"]`).val(returnData[key]);
+ for (const [path, value] of Object.entries(items)) {
+ const input = this.element.querySelector(`input.label-value[data-name="${path}"]`);
+
+ if (!(input instanceof HTMLInputElement)) {
+ continue;
+ }
+
+ input.value = value;
+ }
}
Espo.Ui.success(this.translate('Saved'));