compose: Convert compose_error messages to FormatJS.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-04-12 16:44:36 -07:00
committed by Tim Abbott
parent 810ba7fce1
commit d761077dac
5 changed files with 107 additions and 51 deletions

View File

@@ -1,4 +1,3 @@
import Handlebars from "handlebars/runtime";
import $ from "jquery";
import _ from "lodash";
@@ -18,7 +17,7 @@ import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";
import * as drafts from "./drafts";
import * as echo from "./echo";
import {i18n} from "./i18n";
import {$t_html, i18n} from "./i18n";
import * as loading from "./loading";
import * as markdown from "./markdown";
import * as notifications from "./notifications";
@@ -282,13 +281,13 @@ export function create_message_object() {
return message;
}
export function compose_error(error_text, bad_input) {
export function compose_error(error_html, bad_input) {
$("#compose-send-status")
.removeClass(common.status_classes)
.addClass("alert-error")
.stop(true)
.fadeTo(0, 1);
$("#compose-error-msg").html(error_text);
$("#compose-error-msg").html(error_html);
$("#compose-send-button").prop("disabled", false);
$("#sending-indicator").hide();
if (bad_input !== undefined) {
@@ -304,13 +303,13 @@ export function nonexistent_stream_reply_error() {
}, 5000);
}
function compose_not_subscribed_error(error_text, bad_input) {
function compose_not_subscribed_error(error_html, bad_input) {
$("#compose-send-status")
.removeClass(common.status_classes)
.addClass("home-error-bar")
.stop(true)
.fadeTo(0, 1);
$("#compose-error-msg").html(error_text);
$("#compose-error-msg").html(error_html);
$("#compose-send-button").prop("disabled", false);
$("#sending-indicator").hide();
$(".compose-send-status-close").hide();
@@ -554,7 +553,10 @@ function validate_stream_message_mentions(stream_id) {
if (wildcard_mention !== null && stream_count > wildcard_mention_large_stream_threshold) {
if (!wildcard_mention_allowed()) {
compose_error(
i18n.t("You do not have permission to use wildcard mentions in this stream."),
$t_html({
defaultMessage:
"You do not have permission to use wildcard mentions in this stream.",
}),
);
return false;
}
@@ -610,12 +612,16 @@ function validate_stream_message_post_policy(sub) {
const stream_post_policy = sub.stream_post_policy;
if (stream_post_policy === stream_post_permission_type.admins.code) {
compose_error(i18n.t("Only organization admins are allowed to post to this stream."));
compose_error(
$t_html({
defaultMessage: "Only organization admins are allowed to post to this stream.",
}),
);
return false;
}
if (page_params.is_guest && stream_post_policy !== stream_post_permission_type.everyone.code) {
compose_error(i18n.t("Guests are not allowed to post to this stream."));
compose_error($t_html({defaultMessage: "Guests are not allowed to post to this stream."}));
return false;
}
@@ -623,16 +629,19 @@ function validate_stream_message_post_policy(sub) {
const current_datetime = new Date(Date.now());
const person_date_joined = new Date(person.date_joined);
const days = (current_datetime - person_date_joined) / 1000 / 86400;
let error_text;
let error_html;
if (
stream_post_policy === stream_post_permission_type.non_new_members.code &&
days < page_params.realm_waiting_period_threshold
) {
error_text = i18n.t(
"New members are not allowed to post to this stream.<br>Permission will be granted in __days__ days.",
error_html = $t_html(
{
defaultMessage:
"New members are not allowed to post to this stream.<br />Permission will be granted in {days} days.",
},
{days},
);
compose_error(error_text);
compose_error(error_html);
return false;
}
return true;
@@ -641,20 +650,23 @@ function validate_stream_message_post_policy(sub) {
export function validation_error(error_type, stream_name) {
let response;
const context = {};
context.stream_name = Handlebars.Utils.escapeExpression(stream_name);
switch (error_type) {
case "does-not-exist":
response = i18n.t(
"<p>The stream <b>__stream_name__</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>",
context,
response = $t_html(
{
defaultMessage:
"<p>The stream <b>{stream_name}</b> does not exist.</p><p>Manage your subscriptions <z-link>on your Streams page</z-link>.</p>",
},
{
stream_name,
"z-link": (content_html) => `<a href='#streams/all'>${content_html}</a>`,
},
);
compose_error(response, $("#stream_message_recipient_stream"));
return false;
case "error":
compose_error(
i18n.t("Error checking subscription"),
$t_html({defaultMessage: "Error checking subscription"}),
$("#stream_message_recipient_stream"),
);
return false;
@@ -682,14 +694,20 @@ export function validate_stream_message_address_info(stream_name) {
function validate_stream_message() {
const stream_name = compose_state.stream_name();
if (stream_name === "") {
compose_error(i18n.t("Please specify a stream"), $("#stream_message_recipient_stream"));
compose_error(
$t_html({defaultMessage: "Please specify a stream"}),
$("#stream_message_recipient_stream"),
);
return false;
}
if (page_params.realm_mandatory_topics) {
const topic = compose_state.topic();
if (topic === "") {
compose_error(i18n.t("Please specify a topic"), $("#stream_message_recipient_topic"));
compose_error(
$t_html({defaultMessage: "Please specify a topic"}),
$("#stream_message_recipient_topic"),
);
return false;
}
}
@@ -740,7 +758,7 @@ function validate_private_message() {
if (user_ids.length !== 1 || !people.get_by_user_id(user_ids[0]).is_bot) {
// Unless we're composing to a bot
compose_error(
i18n.t("Private messages are disabled in this organization."),
$t_html({defaultMessage: "Private messages are disabled in this organization."}),
$("#private_message_recipient"),
);
return false;
@@ -749,7 +767,7 @@ function validate_private_message() {
if (compose_state.private_message_recipient().length === 0) {
compose_error(
i18n.t("Please specify at least one valid recipient"),
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
$("#private_message_recipient"),
);
return false;
@@ -764,14 +782,14 @@ function validate_private_message() {
if (invalid_recipients.length === 1) {
context = {recipient: invalid_recipients.join()};
compose_error(
i18n.t("The recipient __recipient__ is not valid", context),
$t_html({defaultMessage: "The recipient {recipient} is not valid"}, context),
$("#private_message_recipient"),
);
return false;
} else if (invalid_recipients.length > 1) {
context = {recipients: invalid_recipients.join()};
compose_error(
i18n.t("The recipients __recipients__ are not valid", context),
$t_html({defaultMessage: "The recipients {recipients} are not valid"}, context),
$("#private_message_recipient"),
);
return false;
@@ -789,12 +807,20 @@ export function validate() {
}
if (/^\s*$/.test(message_content)) {
compose_error(i18n.t("You have nothing to send!"), $("#compose-textarea"));
compose_error(
$t_html({defaultMessage: "You have nothing to send!"}),
$("#compose-textarea"),
);
return false;
}
if ($("#zephyr-mirror-error").is(":visible")) {
compose_error(i18n.t("You need to be running Zephyr mirroring in order to send messages!"));
compose_error(
$t_html({
defaultMessage:
"You need to be running Zephyr mirroring in order to send messages!",
}),
);
return false;
}