Skip to content

Commit

Permalink
[FEATURE] Implemented new way of getting the correct language researc…
Browse files Browse the repository at this point in the history
…hed, added supported languages to fallback to EN
  • Loading branch information
RinyVT committed Feb 9, 2025
1 parent 1cc7a8d commit 432d2e6
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 88 deletions.
5 changes: 5 additions & 0 deletions Build/resources/javascript/Components/FleschReadingScore.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ function getDescriptionElement( score, difficulty, link ) {

const FleschReadingScore = ({flesch}) => {
if (flesch) {
if (flesch.result === false) {
// No flesch reading score available for the current language
return <></>
}

let score = flesch.result.score;

const description = useMemo(() => {
Expand Down
8 changes: 7 additions & 1 deletion Build/resources/javascript/analysis/createAnalysisWorker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {AnalysisWorkerWrapper, createWorker} from 'yoastseo';

const loadWebWorker = ( language ) => {
const workerUnwrapped = createWorker( YoastConfig.urls.workerUrl );
workerUnwrapped.postMessage( { language } );
return new AnalysisWorkerWrapper( workerUnwrapped);
}

export default function createAnalysisWorker(useCornerstone, locale) {
const worker = new AnalysisWorkerWrapper( createWorker( YoastConfig.urls.workerUrl ) );
const worker = loadWebWorker(locale);

worker.initialize( {
locale: locale,
Expand Down
10 changes: 7 additions & 3 deletions Build/resources/javascript/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ let YoastTypo3 = {

_progressBarInitialized: false,

_fallbackLanguage: 'en',

init: function () {
if (typeof YoastConfig === 'undefined') {
return;
Expand All @@ -45,11 +47,13 @@ let YoastTypo3 = {
}
},

setWorker: function (cornerstone, locale = 'en_US') {
setWorker: function (cornerstone, locale = YoastTypo3._fallbackLanguage) {
if (YoastTypo3._yoastWorker !== null) {
YoastTypo3._yoastWorker._worker.terminate();
}
YoastTypo3._yoastWorker = createAnalysisWorker(cornerstone, locale);
const languageCode = locale.slice(0, 2);
const supportedLocale = YoastConfig.supportedLanguages.includes(languageCode) ? languageCode : YoastTypo3._fallbackLanguage;
YoastTypo3._yoastWorker = createAnalysisWorker(cornerstone, supportedLocale);
},

setLocales: function () {
Expand Down Expand Up @@ -246,7 +250,7 @@ let YoastPlugin = {
let cornerStoneField = YoastPlugin.getFormEngineElement('cornerstone');
if (cornerStoneField !== null) {
cornerStoneField.addEventListener('change', function () {
YoastTypo3.setWorker(this.checked);
YoastTypo3.setWorker(this.checked, store.getState().content.locale);
YoastPlugin.refreshAnalysis();
});
}
Expand Down
11 changes: 8 additions & 3 deletions Build/resources/javascript/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { AnalysisWebWorker } from "yoastseo";
import EnglishResearcher from "yoastseo/build/languageProcessing/languages/en/Researcher";

const worker = new AnalysisWebWorker( self, new EnglishResearcher() );
worker.register();
self.onmessage = ( event ) => {
// Set the language for the Researcher
const language = event.data.language;
const { "default": Researcher } = require( `yoastseo/build/languageProcessing/languages/${language}/Researcher` );
const researcher = new Researcher();
const worker = new AnalysisWebWorker( self, researcher );
worker.register();
};
1 change: 1 addition & 0 deletions Classes/Form/Element/InternalLinkingSuggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function render(): array
->buildUriFromRoute('ajax_yoast_internal_linking_suggestions'),
],
'translations' => [$this->localeService->getTranslations()],
'supportedLanguages' => $this->localeService->getSupportedLanguages(),
]);

$this->javascriptService->loadPluginJavascript();
Expand Down
8 changes: 8 additions & 0 deletions Classes/Service/LocaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,12 @@ public function getLanguageIdFromData(array $data): int
}
return (int)$data['databaseRow']['sys_language_uid'];
}

/**
* @return array<int, string>
*/
public function getSupportedLanguages(): array
{
return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['supportedLanguages'] ?? [];
}
}
1 change: 1 addition & 0 deletions Classes/Service/SnippetPreviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function buildSnippetPreview(
'synonyms' => (string)($currentData['tx_yoastseo_focuskeyword_synonyms'] ?? ''),
],
'translations' => [$this->localeService->getTranslations()],
'supportedLanguages' => $this->localeService->getSupportedLanguages(),
];

$this->javascriptService->loadPluginJavascript();
Expand Down
42 changes: 41 additions & 1 deletion Classes/Utility/ConfigurationUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ public static function getDefaultConfiguration(): array
'page' => 1,
'backend_section' => 6,
],
// Translations for the backend
'translations' => [
'availableLocales' => [
'ar',
'bg_BG',
'bs_BA',
'ca',
'ca_valencia',
'da_DK',
'de_AT',
'de_CH',
'de_DE',
'el',
Expand All @@ -51,19 +54,24 @@ public static function getDefaultConfiguration(): array
'en_ZA',
'es_AR',
'es_CL',
'es_CO',
'es_CR',
'es_EC',
'es_ES',
'es_MX',
'es_PE',
'es_VE',
'fa_IR',
'fi',
'fr_BE',
'fr_CA',
'fr_FR',
'fy',
'gl_ES',
'he_IL',
'hi_IN',
'hr',
'hu_HU',
'id_ID',
'it_IT',
'ja',
Expand All @@ -73,7 +81,7 @@ public static function getDefaultConfiguration(): array
'nl_BE',
'nl_NL',
'pl_PL',
'pl_AO',
'pt_AO',
'pt_BR',
'pt_PT',
'ro_RO',
Expand All @@ -91,24 +99,56 @@ public static function getDefaultConfiguration(): array
],
'languageKeyToLocaleMapping' => [
'bg' => 'bg_BG',
'cs' => 'cs_CZ',
'da' => 'da_DK',
'de' => 'de_DE',
'en' => 'en_GB',
'es' => 'es_ES',
'fa' => 'fa_IR',
'fr' => 'fr_FR',
'he' => 'he_IL',
'hu' => 'hu_HU',
'id' => 'id_ID',
'it' => 'it_IT',
'ko' => 'ko_KR',
'no' => 'nb_NO',
'nl' => 'nl_NL',
'pl' => 'pl_PL',
'pt' => 'pt_PT',
'ro' => 'ro_RO',
'ru' => 'ru_RU',
'sk' => 'sk_SK',
'sr' => 'sr_RS',
'sv' => 'sv_SE',
'tr' => 'tr_TR',
'zh' => 'zh_CN',
],
],
// Supported languages for content analysis
'supportedLanguages' => [
'ar',
'ca',
'cs',
'de',
'el',
'en',
'es',
'fa',
'fr',
'he',
'hu',
'id',
'it',
'ja',
'nb',
'nl',
'pl',
'pt',
'ru',
'sk',
'sv',
'tr'
],
'previewSettings' => [
'basicAuth' => [
'username' => '',
Expand Down
2 changes: 1 addition & 1 deletion Resources/Public/JavaScript/dist/plugin.js

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions Resources/Public/JavaScript/dist/worker.js

Large diffs are not rendered by default.

0 comments on commit 432d2e6

Please sign in to comment.