Skip to content

Commit

Permalink
Version bump to 1.0.9, update changelog, add PW forum link to readme,…
Browse files Browse the repository at this point in the history
… Add additional checks and config resets for resetEngineData during upgrade
  • Loading branch information
SkyLundy committed Jun 18, 2024
1 parent d15d419 commit 770263f
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 59 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Fluency for ProcessWire Changelog

### 1.0.9 2024-06-18

NOTE: If upgrading from 1.0.7 or earlier, you will be required to configure your selected
translation engine again after upgrading. Existing content will not be affected.

- Fix issue where upgrading from v1.0.7 or earlier to v1.0.8 would cause translation failure due to
updating how the module stores configuration data. Credit to @jacmaes for finding and reporting.
Resolves https://github.com/SkyLundy/Fluency/issues/5

## 1.0.8 2024-24-03

### New features, Bugfixes, Documentation, Code Improvement, Recommended for all users
Expand Down
2 changes: 1 addition & 1 deletion Fluency.info.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

$info = [
'title' => 'Fluency',
'version' => '108',
'version' => '109',
'href' => 'https://processwire.com/talk/topic/24567-fluency-integrated-deepl-powered-content-translation',
'icon' => 'language',
'summary' => 'The complete translation enhancement suite for ProcessWire.',
Expand Down
93 changes: 49 additions & 44 deletions Fluency.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,18 @@ final class Fluency extends Process implements Module, ConfigurableModule {
* @return void
*/
public function init() {
$this->fluencyConfig = (new FluencyConfig())->getConfigData();

if (!$this->translationEngineIsReady()) {
return;
}

// Create global $fluency variable
$this->wire->set('fluency', $this);

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

$this->fluencyConfig = (new FluencyConfig())->getConfigData();
$this->initializeCaches();
$this->initializeTranslationEngine();
$this->processWireFileTranslator = new ProcessWireFileTranslator($this);
Expand All @@ -146,6 +151,48 @@ public function ready() {
$this->insertAdminAssets();
}

/**
* ProcessWire languages are configured within Fluency and translation is available for admin Inputfields
*
* Requires that:
* - The default ProcessWire language has been configured in Fluency
* - At least one additional language has been configured in Fluency
* - The Translation Engine has been configured and is ready to process requests
*
* #pw-notes Requires that ProcessWire languages are configured in Fluency
*
* #pw-group-Translation-Readiness
*
* @return bool
*/
public function inputfieldTranslationIsReady(): bool {
return $this->translationEngineIsReady() && count($this->getConfiguredLanguages()) >= 2;
}

/**
* Translation Engine has been configured and is ready to process requests
*
* This indicates that the Translation Engine authenticates and is able to send and receive data
* via the corresponding translation service API. This is separate from
* Fluency::inputfieldTranslationIsReady() in that it does not indicate whether languages have
* been configured within Fluency.
*
* Requires that:
*
* - A Translation Engine is selected in Fluency
* - The Translation Engine is configured in Fluency and ready
* - The translation service API used by the engine successfuly accepts/returns requests
*
* #pw-notes Does not require ProcessWire languages to be configured in Fluency
*
* #pw-group-Translation-Readiness
*
* @return bool
*/
public function translationEngineIsReady(): bool {
return $this->fluencyConfig?->translation_api_ready ?? false;
}

/**
* Adds ability to disable translation per-field when configuring multi-language fields
*/
Expand Down Expand Up @@ -194,7 +241,7 @@ private function registerFieldRenderHooks(): void {
* Determine if module should initialize
*/
private function moduleShouldInitInAdmin(): bool {
return $this->page->name !== 'login' && $this->userIsAuthorized();
return $this->page->name !== 'login' && $this->userIsAuthorized() && !!$this->fluencyConfig;
}

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

/**
* ProcessWire languages are configured within Fluency and translation is available for admin Inputfields
*
* Requires that:
* - The default ProcessWire language has been configured in Fluency
* - At least one additional language has been configured in Fluency
* - The Translation Engine has been configured and is ready to process requests
*
* #pw-notes Requires that ProcessWire languages are configured in Fluency
*
* #pw-group-Translation-Readiness
*
* @return bool
*/
public function inputfieldTranslationIsReady(): bool {
return $this->translationEngineIsReady() && count($this->getConfiguredLanguages()) >= 2;
}

/**
* Translation Engine has been configured and is ready to process requests
*
* This indicates that the Translation Engine authenticates and is able to send and receive data
* via the corresponding translation service API. This is separate from
* Fluency::inputfieldTranslationIsReady() in that it does not indicate whether languages have
* been configured within Fluency.
*
* Requires that:
*
* - A Translation Engine is selected in Fluency
* - The Translation Engine is configured in Fluency and ready
* - The translation service API used by the engine successfuly accepts/returns requests
*
* #pw-notes Does not require ProcessWire languages to be configured in Fluency
*
* #pw-group-Translation-Readiness
*
* @return bool
*/
public function translationEngineIsReady(): bool {
return $this->fluencyConfig->translation_api_ready ?? false;
}

/**
* Module actions and Translation Engine interfaces
*/
Expand Down
64 changes: 54 additions & 10 deletions FluencyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ public function getConfigData(): ?FluencyConfigData {

$moduleConfig = $this->getModuleConfig();

if (!$moduleConfig->selected_engine) {
// Addresses issues where selected_engine may contain legacy or invalid engine config data
// If so, reset and return null
if (!$this->selectedEngineIsValid()) {
$this->resetEngineData();

return null;
}

Expand Down Expand Up @@ -137,14 +141,30 @@ public function initTranslationEngine(): ?array {
* @param array ...$newConfigData Named arguments
*/
private function saveModuleConfig(...$newConfigData): void {
$this->modules->saveModuleConfigData('Fluency', [
$this->modules->saveConfig('Fluency', [
...(array) $this->getModuleConfig(),
...$newConfigData
]);
}

/**
* Internal module use only
* Removes config keys and their values from the module's config
* @param array<string> $configRemovalKeys Keys of configs to remove
*/
private function removeFromModuleConfig(array $configRemovalKeys): void {
$config = (array) $this->getModuleConfig();

$config = array_filter(
$config,
fn($config) => !in_array($config, $configRemovalKeys),
ARRAY_FILTER_USE_KEY
);

$this->modules->saveConfig('Fluency', $config);
}

/**
* Get module config as an object containing all set and default values
*
* @return object Config as an object
*/
Expand All @@ -154,12 +174,33 @@ private function getModuleConfig(): object {

/**
* Resets all configured engine data by removing the selected engine and it's associated settings
* This may be required when upgrading the module
* This may be required when upgrading the module due to previous storage formats
*/
public function resetEngineData(): void {
$configKeys = array_keys((array) $this->getModuleConfig());

$removals = array_filter(
$configKeys,
fn($key) => !!str_starts_with($key, 'pw_language_')
);

$this->removeFromModuleConfig($removals);

$this->saveModuleConfig(translation_api_ready: false, selected_engine: null);
}

/**
* Determines if there is a currently selected translation engine and that it is stored in the
* valid format
*
* @todo Deprecate this in the future when possible, only needed for upgrades from < 1.0.8
*/
public function selectedEngineIsValid(): bool {
$moduleConfig = $this->getModuleConfig();

return !!$moduleConfig->selected_engine && !!json_decode($moduleConfig->selected_engine);
}

/**
* This renders all Fluency config fields, as well as integrating the Translation Engine Config
* fields, when necessary, when an engine is selected
Expand Down Expand Up @@ -210,7 +251,7 @@ public function getInputFields(): InputfieldWrapper {
]
]);

if (!$engineSelected) {
if (!$this->selectedEngineIsValid()) {
return $inputfields->add($fieldset);
}

Expand Down Expand Up @@ -275,7 +316,7 @@ public function getInputFields(): InputfieldWrapper {

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

if ($engineLanguages->error) {
Expand Down Expand Up @@ -518,11 +559,14 @@ function ($markup, $language) {
Markup::div(
Markup::a(
href: 'https://paypal.me/noonesboy',
content: Markup::img("{$this->wire('urls')->get('Fluency')}/assets/img/paypal_me.png", 'PayPal Me'),
content: Markup::img(
source: "{$this->wire('urls')->get('Fluency')}/assets/img/paypal_me.png",
alt: 'PayPal Me'
),
rel: 'noopener',
target: '_blank'
),
'button-donate'
target: '_blank',
classes: 'button-donate'
)
)
)
]);
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Fluency can be used:
- When adding multi-language abilities to an existing ProcessWire application
- To add translation abilities to an existing multi-language ProcessWire application

You can help out by filing Github issues when bugs are found, or submit a pull request with fixes.
You can help make Fluency better by filing Github issues when bugs are found, or submit a pull request with fixes.

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/).

## Contents

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

## Installing
Expand Down
6 changes: 6 additions & 0 deletions app/Services/FluencyProcessWireFileTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class FluencyProcessWireFileTranslator {
public function __construct(
private Fluency $fluency
) {
if (!!$fluency->fluencyConfig) {
$this->init();
}
}

private function init(): void {
$this->defaultFluencyLanguage = $this->fluency->getConfiguredLanguages()->getDefault();
$this->defaultPwLanguage = wire('languages')->getDefault();
$this->defaultPwLanguageTranslator = $this->defaultPwLanguage->translator();
Expand Down
Loading

0 comments on commit 770263f

Please sign in to comment.