mirror of
https://github.com/friendica/friendica
synced 2025-04-30 17:04:23 +02:00
Use ISO-639-1 for the language detection
This commit is contained in:
parent
74b4eddcf7
commit
3f2b0b9422
7 changed files with 113 additions and 114 deletions
|
@ -2034,15 +2034,12 @@ class Item
|
|||
return [];
|
||||
}
|
||||
|
||||
$availableLanguages = DI::l10n()->getAvailableLanguages(true);
|
||||
$availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
|
||||
|
||||
$ld = new Language(array_keys($availableLanguages));
|
||||
$ld = new Language(DI::l10n()->getDetectableLanguages());
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach (self::splitByBlocks($searchtext) as $block) {
|
||||
$languages = $ld->detect($block)->limit(0, $count)->close() ?: [];
|
||||
$languages = $ld->detect($block)->close() ?: [];
|
||||
|
||||
$data = [
|
||||
'text' => $block,
|
||||
|
@ -2057,10 +2054,32 @@ class Item
|
|||
}
|
||||
}
|
||||
|
||||
arsort($result);
|
||||
$result = array_slice($result, 0, $count);
|
||||
$result = self::compactLanguages($result);
|
||||
|
||||
return $result;
|
||||
arsort($result);
|
||||
return array_slice($result, 0, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Concert the language code in the detection result to ISO 639-1.
|
||||
* On duplicates the system uses the higher quality value.
|
||||
*
|
||||
* @param array $result
|
||||
* @return array
|
||||
*/
|
||||
private static function compactLanguages(array $result): array
|
||||
{
|
||||
$languages = [];
|
||||
foreach ($result as $language => $quality) {
|
||||
if ($quality == 0) {
|
||||
continue;
|
||||
}
|
||||
$code = DI::l10n()->toISO6391($language);
|
||||
if (empty($languages[$code]) || ($languages[$code] < $quality)) {
|
||||
$languages[$code] = $quality;
|
||||
}
|
||||
}
|
||||
return $languages;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,9 +35,6 @@ use Friendica\Model\Verb;
|
|||
use Friendica\Protocol\Activity;
|
||||
use Friendica\Protocol\Relay;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
// Channel
|
||||
|
||||
class Engagement
|
||||
{
|
||||
|
|
|
@ -127,7 +127,6 @@ class User
|
|||
|
||||
case 'community':
|
||||
return User::ACCOUNT_TYPE_COMMUNITY;
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -425,7 +424,7 @@ class User
|
|||
* @return array user
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getFirstAdmin(array $fields = []) : array
|
||||
public static function getFirstAdmin(array $fields = []): array
|
||||
{
|
||||
if (!empty(DI::config()->get('config', 'admin_nickname'))) {
|
||||
return self::getByNickname(DI::config()->get('config', 'admin_nickname'), $fields);
|
||||
|
@ -560,22 +559,20 @@ class User
|
|||
return $default_circle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the language code from the given user. If the code is invalid, return the system language
|
||||
*
|
||||
* @param integer $uid User-Id
|
||||
* @return string
|
||||
*/
|
||||
/**
|
||||
* Fetch the language code from the given user. If the code is invalid, return the system language
|
||||
*
|
||||
* @param integer $uid User-Id
|
||||
* @return string
|
||||
*/
|
||||
public static function getLanguageCode(int $uid): string
|
||||
{
|
||||
$owner = self::getOwnerDataById($uid);
|
||||
$languages = DI::l10n()->getAvailableLanguages(true);
|
||||
if (in_array($owner['language'], array_keys($languages))) {
|
||||
$language = $owner['language'];
|
||||
} else {
|
||||
$language = DI::config()->get('system', 'language');
|
||||
$owner = self::getOwnerDataById($uid);
|
||||
$language = DI::l10n()->toISO6391($owner['language']);
|
||||
if (in_array($language, array_keys(DI::l10n()->getLanguageCodes()))) {
|
||||
return $language;
|
||||
}
|
||||
return $language;
|
||||
return DI::l10n()->toISO6391(DI::config()->get('system', 'language'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1480,7 +1477,7 @@ class User
|
|||
Photo::delete(['uid' => $register['uid']]);
|
||||
|
||||
return DBA::delete('user', ['uid' => $register['uid']]) &&
|
||||
Register::deleteByHash($register['hash']);
|
||||
Register::deleteByHash($register['hash']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue