Skip to content

Commit 770263f

Browse files
committed
Version bump to 1.0.9, update changelog, add PW forum link to readme, Add additional checks and config resets for resetEngineData during upgrade
1 parent d15d419 commit 770263f

File tree

9 files changed

+130
-59
lines changed

9 files changed

+130
-59
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Fluency for ProcessWire Changelog
22

3+
### 1.0.9 2024-06-18
4+
5+
NOTE: If upgrading from 1.0.7 or earlier, you will be required to configure your selected
6+
translation engine again after upgrading. Existing content will not be affected.
7+
8+
- Fix issue where upgrading from v1.0.7 or earlier to v1.0.8 would cause translation failure due to
9+
updating how the module stores configuration data. Credit to @jacmaes for finding and reporting.
10+
Resolves https://github.com/SkyLundy/Fluency/issues/5
11+
312
## 1.0.8 2024-24-03
413

514
### New features, Bugfixes, Documentation, Code Improvement, Recommended for all users

Fluency.info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
$info = [
88
'title' => 'Fluency',
9-
'version' => '108',
9+
'version' => '109',
1010
'href' => 'https://processwire.com/talk/topic/24567-fluency-integrated-deepl-powered-content-translation',
1111
'icon' => 'language',
1212
'summary' => 'The complete translation enhancement suite for ProcessWire.',

Fluency.module.php

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ final class Fluency extends Process implements Module, ConfigurableModule {
120120
* @return void
121121
*/
122122
public function init() {
123+
$this->fluencyConfig = (new FluencyConfig())->getConfigData();
124+
125+
if (!$this->translationEngineIsReady()) {
126+
return;
127+
}
128+
123129
// Create global $fluency variable
124130
$this->wire->set('fluency', $this);
125131

126132
$this->moduleJsPath = "{$this->urls->$this}assets/scripts/";
127133
$this->moduleCssPath = "{$this->urls->$this}assets/styles/";
128134

129-
$this->fluencyConfig = (new FluencyConfig())->getConfigData();
130135
$this->initializeCaches();
131136
$this->initializeTranslationEngine();
132137
$this->processWireFileTranslator = new ProcessWireFileTranslator($this);
@@ -146,6 +151,48 @@ public function ready() {
146151
$this->insertAdminAssets();
147152
}
148153

154+
/**
155+
* ProcessWire languages are configured within Fluency and translation is available for admin Inputfields
156+
*
157+
* Requires that:
158+
* - The default ProcessWire language has been configured in Fluency
159+
* - At least one additional language has been configured in Fluency
160+
* - The Translation Engine has been configured and is ready to process requests
161+
*
162+
* #pw-notes Requires that ProcessWire languages are configured in Fluency
163+
*
164+
* #pw-group-Translation-Readiness
165+
*
166+
* @return bool
167+
*/
168+
public function inputfieldTranslationIsReady(): bool {
169+
return $this->translationEngineIsReady() && count($this->getConfiguredLanguages()) >= 2;
170+
}
171+
172+
/**
173+
* Translation Engine has been configured and is ready to process requests
174+
*
175+
* This indicates that the Translation Engine authenticates and is able to send and receive data
176+
* via the corresponding translation service API. This is separate from
177+
* Fluency::inputfieldTranslationIsReady() in that it does not indicate whether languages have
178+
* been configured within Fluency.
179+
*
180+
* Requires that:
181+
*
182+
* - A Translation Engine is selected in Fluency
183+
* - The Translation Engine is configured in Fluency and ready
184+
* - The translation service API used by the engine successfuly accepts/returns requests
185+
*
186+
* #pw-notes Does not require ProcessWire languages to be configured in Fluency
187+
*
188+
* #pw-group-Translation-Readiness
189+
*
190+
* @return bool
191+
*/
192+
public function translationEngineIsReady(): bool {
193+
return $this->fluencyConfig?->translation_api_ready ?? false;
194+
}
195+
149196
/**
150197
* Adds ability to disable translation per-field when configuring multi-language fields
151198
*/
@@ -194,7 +241,7 @@ private function registerFieldRenderHooks(): void {
194241
* Determine if module should initialize
195242
*/
196243
private function moduleShouldInitInAdmin(): bool {
197-
return $this->page->name !== 'login' && $this->userIsAuthorized();
244+
return $this->page->name !== 'login' && $this->userIsAuthorized() && !!$this->fluencyConfig;
198245
}
199246

200247
/**
@@ -296,48 +343,6 @@ public function insertApiUsageTableFieldsetAssets(): void {
296343
$this->config->styles->add("{$this->moduleCssPath}fluency_api_usage.min.css");
297344
}
298345

299-
/**
300-
* ProcessWire languages are configured within Fluency and translation is available for admin Inputfields
301-
*
302-
* Requires that:
303-
* - The default ProcessWire language has been configured in Fluency
304-
* - At least one additional language has been configured in Fluency
305-
* - The Translation Engine has been configured and is ready to process requests
306-
*
307-
* #pw-notes Requires that ProcessWire languages are configured in Fluency
308-
*
309-
* #pw-group-Translation-Readiness
310-
*
311-
* @return bool
312-
*/
313-
public function inputfieldTranslationIsReady(): bool {
314-
return $this->translationEngineIsReady() && count($this->getConfiguredLanguages()) >= 2;
315-
}
316-
317-
/**
318-
* Translation Engine has been configured and is ready to process requests
319-
*
320-
* This indicates that the Translation Engine authenticates and is able to send and receive data
321-
* via the corresponding translation service API. This is separate from
322-
* Fluency::inputfieldTranslationIsReady() in that it does not indicate whether languages have
323-
* been configured within Fluency.
324-
*
325-
* Requires that:
326-
*
327-
* - A Translation Engine is selected in Fluency
328-
* - The Translation Engine is configured in Fluency and ready
329-
* - The translation service API used by the engine successfuly accepts/returns requests
330-
*
331-
* #pw-notes Does not require ProcessWire languages to be configured in Fluency
332-
*
333-
* #pw-group-Translation-Readiness
334-
*
335-
* @return bool
336-
*/
337-
public function translationEngineIsReady(): bool {
338-
return $this->fluencyConfig->translation_api_ready ?? false;
339-
}
340-
341346
/**
342347
* Module actions and Translation Engine interfaces
343348
*/

FluencyConfig.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public function getConfigData(): ?FluencyConfigData {
7070

7171
$moduleConfig = $this->getModuleConfig();
7272

73-
if (!$moduleConfig->selected_engine) {
73+
// Addresses issues where selected_engine may contain legacy or invalid engine config data
74+
// If so, reset and return null
75+
if (!$this->selectedEngineIsValid()) {
76+
$this->resetEngineData();
77+
7478
return null;
7579
}
7680

@@ -137,14 +141,30 @@ public function initTranslationEngine(): ?array {
137141
* @param array ...$newConfigData Named arguments
138142
*/
139143
private function saveModuleConfig(...$newConfigData): void {
140-
$this->modules->saveModuleConfigData('Fluency', [
144+
$this->modules->saveConfig('Fluency', [
141145
...(array) $this->getModuleConfig(),
142146
...$newConfigData
143147
]);
144148
}
145149

146150
/**
147-
* Internal module use only
151+
* Removes config keys and their values from the module's config
152+
* @param array<string> $configRemovalKeys Keys of configs to remove
153+
*/
154+
private function removeFromModuleConfig(array $configRemovalKeys): void {
155+
$config = (array) $this->getModuleConfig();
156+
157+
$config = array_filter(
158+
$config,
159+
fn($config) => !in_array($config, $configRemovalKeys),
160+
ARRAY_FILTER_USE_KEY
161+
);
162+
163+
$this->modules->saveConfig('Fluency', $config);
164+
}
165+
166+
/**
167+
* Get module config as an object containing all set and default values
148168
*
149169
* @return object Config as an object
150170
*/
@@ -154,12 +174,33 @@ private function getModuleConfig(): object {
154174

155175
/**
156176
* Resets all configured engine data by removing the selected engine and it's associated settings
157-
* This may be required when upgrading the module
177+
* This may be required when upgrading the module due to previous storage formats
158178
*/
159179
public function resetEngineData(): void {
180+
$configKeys = array_keys((array) $this->getModuleConfig());
181+
182+
$removals = array_filter(
183+
$configKeys,
184+
fn($key) => !!str_starts_with($key, 'pw_language_')
185+
);
186+
187+
$this->removeFromModuleConfig($removals);
188+
160189
$this->saveModuleConfig(translation_api_ready: false, selected_engine: null);
161190
}
162191

192+
/**
193+
* Determines if there is a currently selected translation engine and that it is stored in the
194+
* valid format
195+
*
196+
* @todo Deprecate this in the future when possible, only needed for upgrades from < 1.0.8
197+
*/
198+
public function selectedEngineIsValid(): bool {
199+
$moduleConfig = $this->getModuleConfig();
200+
201+
return !!$moduleConfig->selected_engine && !!json_decode($moduleConfig->selected_engine);
202+
}
203+
163204
/**
164205
* This renders all Fluency config fields, as well as integrating the Translation Engine Config
165206
* fields, when necessary, when an engine is selected
@@ -210,7 +251,7 @@ public function getInputFields(): InputfieldWrapper {
210251
]
211252
]);
212253

213-
if (!$engineSelected) {
254+
if (!$this->selectedEngineIsValid()) {
214255
return $inputfields->add($fieldset);
215256
}
216257

@@ -275,7 +316,7 @@ public function getInputFields(): InputfieldWrapper {
275316

276317
// Verify API credentials by getting translatable languages
277318
// 2 birds, 1 stone
278-
if ($moduleConfig->selected_engine && !$moduleConfig->translation_api_ready || $engineChanged) {
319+
if ($this->selectedEngineIsValid() && !$moduleConfig->translation_api_ready || $engineChanged) {
279320
$engineLanguages = $engine->getLanguages();
280321

281322
if ($engineLanguages->error) {
@@ -518,11 +559,14 @@ function ($markup, $language) {
518559
Markup::div(
519560
Markup::a(
520561
href: 'https://paypal.me/noonesboy',
521-
content: Markup::img("{$this->wire('urls')->get('Fluency')}/assets/img/paypal_me.png", 'PayPal Me'),
562+
content: Markup::img(
563+
source: "{$this->wire('urls')->get('Fluency')}/assets/img/paypal_me.png",
564+
alt: 'PayPal Me'
565+
),
522566
rel: 'noopener',
523-
target: '_blank'
524-
),
525-
'button-donate'
567+
target: '_blank',
568+
classes: 'button-donate'
569+
)
526570
)
527571
)
528572
]);

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Fluency can be used:
1010
- When adding multi-language abilities to an existing ProcessWire application
1111
- To add translation abilities to an existing multi-language ProcessWire application
1212

13-
You can help out by filing Github issues when bugs are found, or submit a pull request with fixes.
13+
You can help make Fluency better by filing Github issues when bugs are found, or submit a pull request with fixes.
14+
15+
For support and community discussion about Fluency, visit [the module thread in the ProcessWire forums](https://processwire.com/talk/topic/24567-fluency-the-complete-translation-enhancement-suite-for-processwire/).
1416

1517
## Contents
1618

@@ -55,7 +57,7 @@ You can help out by filing Github issues when bugs are found, or submit a pull r
5557
- LanguageTabs
5658
- ProcessLanguage
5759
- UIKit admin theme
58-
- At least 2 languages configured in ProcessWire to add translation to fields
60+
- At least 2 languages configured in ProcessWire and Fluency to add translation to fields
5961
- API key for the chosen third party translation service, free tiers are available
6062

6163
## Installing

app/Services/FluencyProcessWireFileTranslator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class FluencyProcessWireFileTranslator {
3030
public function __construct(
3131
private Fluency $fluency
3232
) {
33+
if (!!$fluency->fluencyConfig) {
34+
$this->init();
35+
}
36+
}
37+
38+
private function init(): void {
3339
$this->defaultFluencyLanguage = $this->fluency->getConfiguredLanguages()->getDefault();
3440
$this->defaultPwLanguage = wire('languages')->getDefault();
3541
$this->defaultPwLanguageTranslator = $this->defaultPwLanguage->translator();

0 commit comments

Comments
 (0)