LanguageMappings.php
5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace WPML\TM\API\ATE;
use WPML\Element\API\Languages;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\FP\Wrapper;
use WPML\LIB\WP\Option;
use WPML\Element\API\Entity\LanguageMapping;
use WPML\TM\ATE\API\CacheStorage\StaticVariable;
use WPML\TM\ATE\API\CachedATEAPI;
use function WPML\Container\make;
use function WPML\FP\curryN;
use function WPML\FP\invoke;
use function WPML\FP\pipe;
class LanguageMappings {
const IGNORE_MAPPING_OPTION = 'wpml-languages-ignore-mapping';
const IGNORE_MAPPING_ID = - 1;
public static function withCanBeTranslatedAutomatically( $languages = null ) {
$fn = curryN( 1, function ( $languages ) {
if ( ! is_object( $languages ) && ! is_array( $languages ) ) {
return $languages;
}
$ateAPI = static::getATEAPI();
$targetCodes = Lst::pluck( 'code', Obj::values( $languages ) );
$supportedLanguages = $ateAPI->get_languages_supported_by_automatic_translations( $targetCodes )->getOrElse( [] );
$areThereAnySupportedLanguages = Lst::find( Logic::isNotNull(), $supportedLanguages );
$isSupportedCode = pipe( Obj::prop( Fns::__, $supportedLanguages ), Logic::isNotNull() );
$isNotMarkedAsDontMap = Logic::complement( Lst::includes( Fns::__, Option::getOr( self::IGNORE_MAPPING_OPTION, [] ) ) );
$isDefaultCode = Relation::equals( Languages::getDefaultCode() );
$isSupportedByAnyEngine = pipe(
pipe( [ $ateAPI, 'get_language_details' ], invoke( 'getOrElse' )->with( [] ) ),
Logic::anyPass( [ Obj::prop( 'ms_api_iso' ), Obj::prop( 'google_api_iso' ), Obj::prop( 'deepl_api_iso' ) ] )
);
$isDefaultLangSupported = Logic::anyPass( [ Fns::always( $areThereAnySupportedLanguages ), $isSupportedByAnyEngine ] );
$isSupported = pipe( Obj::prop( 'code' ), Logic::both(
$isNotMarkedAsDontMap,
Logic::ifElse( $isDefaultCode, $isDefaultLangSupported, $isSupportedCode )
) );
return Fns::map( Obj::addProp( 'can_be_translated_automatically', $isSupported ), $languages );
} );
return call_user_func_array( $fn, func_get_args() );
}
public static function isCodeEligibleForAutomaticTranslations( $languageCode = null ) {
$fn = Lst::includes( Fns::__, static::geCodesEligibleForAutomaticTranslations() );
return call_user_func_array( $fn, func_get_args() );
}
/**
* @return LanguageMapping[] $mappings
*/
public static function get() {
$ignoredMappings = Fns::map( function ( $code ) {
return new LanguageMapping( $code, '', self::IGNORE_MAPPING_ID );
}, Option::getOr( self::IGNORE_MAPPING_OPTION, [] ) );
$mappingInATE = Fns::map( function ( $record ) {
return new LanguageMapping(
Obj::prop( 'source_code', $record ),
Obj::path( [ 'source_language', 'name' ], $record ),
Obj::path( [ 'target_language', 'id' ], $record ),
Obj::prop( 'target_code', $record )
);
}, static::getATEAPI()->get_language_mapping()->getOrElse( [] ) );
return Lst::concat( $ignoredMappings, $mappingInATE );
}
public static function withMapping( $languages = null ) {
$fn = curryN( 1, function ( $languages ) {
$mapping = self::get();
$findMappingByCode = function ( $language ) use ( $mapping ) {
return Lst::find( invoke( 'matches' )->with( Obj::prop( 'code', $language ) ), $mapping );
};
return Fns::map( Obj::addProp( 'mapping', $findMappingByCode ), $languages );
} );
return call_user_func_array( $fn, func_get_args() );
}
/**
* @return array
*/
public static function getAvailable() {
$mapping = static::getATEAPI()->get_available_languages();
return Relation::sortWith( [ Fns::ascend( Obj::prop( 'name' ) ) ], $mapping );
}
/**
* @param LanguageMapping[] $mappings
*
* @return Either
*/
public static function saveMapping( array $mappings ) {
list( $ignoredMapping, $mappingSet ) = \wpml_collect( $mappings )->partition( Relation::propEq( 'targetId', self::IGNORE_MAPPING_ID ) );
$ignoredCodes = $ignoredMapping->pluck( 'sourceCode' )->toArray();
Option::update( self::IGNORE_MAPPING_OPTION, $ignoredCodes );
$ateAPI = static::getATEAPI();
if ( count( $ignoredCodes ) ) {
$ateAPI->get_language_mapping()
->map( Fns::filter( pipe( Obj::prop( 'source_code' ), Lst::includes( Fns::__, $ignoredCodes ) ) ) )
->map( Lst::pluck( 'id' ) )
->filter( Logic::complement( Logic::isEmpty() ) )
->map( [ $ateAPI, 'remove_language_mapping' ] );
}
return $ateAPI->create_language_mapping( $mappingSet->values()->toArray() );
}
/**
* @return array
*/
public static function getLanguagesEligibleForAutomaticTranslations() {
return Wrapper::of( Languages::getSecondaries() )
->map( static::withCanBeTranslatedAutomatically() )
->map( Fns::filter( Obj::prop( 'can_be_translated_automatically' ) ) )
->get();
}
/**
* @return string[]
*/
public static function geCodesEligibleForAutomaticTranslations() {
return Lst::pluck( 'code', static::getLanguagesEligibleForAutomaticTranslations() );
}
public static function hasTheSameMappingAsDefaultLang( $language = null ) {
$fn = curryN( 1, function ( $language ) {
$defaultLanguage = Lst::last( static::withMapping( [ Languages::getDefault() ] ) );
if ( ! is_object( $defaultLanguage ) && ! is_array( $defaultLanguage ) ) {
return false;
}
$defaultLanguageMappingTargetCode = Obj::pathOr( Obj::prop( 'code', $defaultLanguage ), [ 'mapping', 'targetCode' ], $defaultLanguage );
return Obj::pathOr( null, [ 'mapping', 'targetCode' ], $language ) === $defaultLanguageMappingTargetCode;
} );
return call_user_func_array( $fn, func_get_args() );
}
/**
* @return CachedATEAPI
*/
protected static function getATEAPI() {
return new CachedATEAPI( make( \WPML_TM_ATE_API::class ), StaticVariable::getInstance() );
}
}