1
0
mirror of https://github.com/flarum/core.git synced 2025-07-25 18:51:40 +02:00

Switch to ICU MessageFormat (#2759)

This commit is contained in:
Alexander Skvortsov
2021-04-30 12:44:39 -04:00
committed by GitHub
parent 9461df8803
commit b5ee8a034b
24 changed files with 19152 additions and 3351 deletions

View File

@@ -35,7 +35,7 @@ class SendTestMailController implements RequestHandlerInterface
$actor = RequestUtil::getActor($request);
$actor->assertAdmin();
$body = $this->translator->trans('core.email.send_test.body', ['{username}' => $actor->username]);
$body = $this->translator->trans('core.email.send_test.body', ['username' => $actor->username]);
$this->mailer->raw($body, function (Message $message) use ($actor) {
$message->to($actor->email);

View File

@@ -17,6 +17,7 @@ use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Translation\MessageCatalogueInterface;
class LanguagePack implements ExtenderInterface, LifecycleInterface
{
@@ -107,6 +108,9 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface
// extension) with the list of known names and all extension IDs.
$slug = $file->getBasename(".{$file->getExtension()}");
// Ignore ICU MessageFormat suffixes.
$slug = str_replace(MessageCatalogueInterface::INTL_DOMAIN_SUFFIX, '', $slug);
if (in_array($slug, self::CORE_LOCALE_FILES, true)) {
return true;
}

View File

@@ -13,6 +13,7 @@ use DirectoryIterator;
use Flarum\Extension\Extension;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Translation\MessageCatalogueInterface;
class Locales implements ExtenderInterface, LifecycleInterface
{
@@ -38,8 +39,14 @@ class Locales implements ExtenderInterface, LifecycleInterface
continue;
}
$intlIcu = false;
$locale = $file->getBasename(".$extension");
// Ignore ICU MessageFormat suffixes.
$locale = str_replace(MessageCatalogueInterface::INTL_DOMAIN_SUFFIX, '', $locale);
$locales->addTranslations(
$file->getBasename(".$extension"),
$locale,
$file->getPathname()
);
}

View File

@@ -83,7 +83,7 @@ class ViewFormatter implements HttpFormatter
private function getTranslationIfExists(string $errorType)
{
$key = "core.views.error.$errorType";
$translation = $this->translator->trans($key, ['{forum}' => $this->settings->get('forum_title')]);
$translation = $this->translator->trans($key, ['forum' => $this->settings->get('forum_title')]);
return $translation === $key ? null : $translation;
}

View File

@@ -10,6 +10,7 @@
namespace Flarum\Locale;
use Illuminate\Support\Arr;
use Symfony\Component\Translation\MessageCatalogueInterface;
class LocaleManager
{
@@ -64,7 +65,11 @@ class LocaleManager
{
$prefix = $module ? $module.'::' : '';
$this->translator->addResource('prefixed_yaml', compact('file', 'prefix'), $locale);
// `messages` is the default domain, and we want to support MessageFormat
// for all translations.
$domain = 'messages'.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
$this->translator->addResource('prefixed_yaml', compact('file', 'prefix'), $locale, $domain);
}
public function addJsFile(string $locale, string $js)

View File

@@ -30,6 +30,8 @@ class LocaleServiceProvider extends AbstractServiceProvider
);
$locales->addLocale($this->getDefaultLocale($container), 'Default');
$locales->addTranslations('en', __DIR__.'/../../locale/core.yml');
$locales->addTranslations('en', __DIR__.'/../../locale/validation.yml');
return $locales;
});
@@ -46,8 +48,6 @@ class LocaleServiceProvider extends AbstractServiceProvider
$translator->setFallbackLocales(['en']);
$translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader());
$translator->addResource('prefixed_yaml', ['file' => __DIR__.'/../../locale/core.yml', 'prefix' => null], 'en');
$translator->addResource('prefixed_yaml', ['file' => __DIR__.'/../../locale/validation.yml', 'prefix' => null], 'en');
return $translator;
});

View File

@@ -24,7 +24,8 @@ class Translator extends BaseTranslator implements TranslatorContract
public function choice($key, $number, array $replace = [], $locale = null)
{
return $this->transChoice($key, $number, $replace, nil, $locale);
// Symfony's translator uses ICU MessageFormat, which pluralizes based on arguments.
return $this->trans($key, $replace, null, $locale);
}
/**

View File

@@ -36,9 +36,9 @@ trait AccountActivationMailerTrait
protected function getEmailData(User $user, EmailToken $token)
{
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
'username' => $user->display_name,
'url' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'forum' => $this->settings->get('forum_title')
];
}

View File

@@ -104,13 +104,13 @@ class RequestPasswordResetHandler
$token->save();
$data = [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('resetPassword', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title'),
'username' => $user->display_name,
'url' => $this->url->to('forum')->route('resetPassword', ['token' => $token->token]),
'forum' => $this->settings->get('forum_title'),
];
$body = $this->translator->trans('core.email.reset_password.body', $data);
$subject = '['.$data['{forum}'].'] '.$this->translator->trans('core.email.reset_password.subject');
$subject = '['.$data['forum'].'] '.$this->translator->trans('core.email.reset_password.subject');
$this->queue->push(new SendRawEmailJob($user->email, $subject, $body));

View File

@@ -52,7 +52,7 @@ class EmailConfirmationMailer
$data = $this->getEmailData($event->user, $email);
$body = $this->translator->trans('core.email.confirm_email.body', $data);
$subject = '['.$data['{forum}'].'] '.$this->translator->trans('core.email.confirm_email.subject');
$subject = '['.$data['forum'].'] '.$this->translator->trans('core.email.confirm_email.subject');
$this->queue->push(new SendRawEmailJob($email, $subject, $body));
}
@@ -82,9 +82,9 @@ class EmailConfirmationMailer
$token = $this->generateToken($user, $email);
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
'username' => $user->display_name,
'url' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'forum' => $this->settings->get('forum_title')
];
}
}