Añade componentes básicos traducibles Html/Text

This commit is contained in:
Manuel Cillero 2023-05-30 19:13:13 +02:00
parent 6c76e3519c
commit 1d83bbc80a
20 changed files with 243 additions and 369 deletions

View file

@ -5,64 +5,64 @@ use pagetop_minimal::component::*;
pub async fn summary(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
let top_menu = MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::t("module_name", &LOCALE_ADMIN)))
.with_item(MegaMenuItem::label(Text::t("module_name", &LOCALE_ADMIN)))
.with_item(MegaMenuItem::link(
L10n::n("Opción 2"),
Text::n("Opción 2"),
"https://www.google.es",
))
.with_item(MegaMenuItem::link_blank(
L10n::n("Opción 3"),
Text::n("Opción 3"),
"https://www.google.es",
))
.with_item(MegaMenuItem::submenu(
L10n::n("Submenú 1"),
Text::n("Submenú 1"),
MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
.with_item(MegaMenuItem::label(Text::n("Opción 1")))
.with_item(MegaMenuItem::label(Text::n("Opción 2"))),
))
.with_item(MegaMenuItem::separator())
.with_item(MegaMenuItem::submenu(
L10n::n("Submenú 2"),
Text::n("Submenú 2"),
MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
.with_item(MegaMenuItem::label(Text::n("Opción 1")))
.with_item(MegaMenuItem::label(Text::n("Opción 2"))),
))
.with_item(MegaMenuItem::label(L10n::n("Opción 4")));
.with_item(MegaMenuItem::label(Text::n("Opción 4")));
let side_menu = MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(Text::n("Opción 1")))
.with_item(MegaMenuItem::link(
L10n::n("Opción 2"),
Text::n("Opción 2"),
"https://www.google.es",
))
.with_item(MegaMenuItem::link_blank(
L10n::n("Opción 3"),
Text::n("Opción 3"),
"https://www.google.es",
))
.with_item(MegaMenuItem::submenu(
L10n::n("Submenú 1"),
Text::n("Submenú 1"),
MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
.with_item(MegaMenuItem::label(Text::n("Opción 1")))
.with_item(MegaMenuItem::label(Text::n("Opción 2"))),
))
.with_item(MegaMenuItem::separator())
.with_item(MegaMenuItem::submenu(
L10n::n("Submenú 2"),
Text::n("Submenú 2"),
MegaMenu::new()
.with_item(MegaMenuItem::label(L10n::n("Opción 1")))
.with_item(MegaMenuItem::label(L10n::n("Opción 2"))),
.with_item(MegaMenuItem::label(Text::n("Opción 1")))
.with_item(MegaMenuItem::label(Text::n("Opción 2"))),
))
.with_item(MegaMenuItem::label(L10n::n("Opción 4")));
.with_item(MegaMenuItem::label(Text::n("Opción 4")));
Page::new(request)
.with_context(ContextOp::Theme("Bootsier"))
.with_title(L10n::n("Admin"))
.with_title(Text::n("Admin"))
.with_this_in("top-menu", top_menu)
.with_this_in(
"region-content",
grid::Row::new()
.with_column(grid::Column::new().with_component(side_menu))
.with_column(grid::Column::new().with_component(Html::with(html! {
.with_column(grid::Column::new().with_component(Html::n(html! {
p { "Columna 2"}
}))),
)

View file

@ -34,7 +34,7 @@ impl ModuleTrait for HomeDemo {
async fn demo(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request)
.with_title(L10n::t("page_title", &LOCALE_DEMOHOME))
.with_title(Text::t("page_title", &LOCALE_DEMOHOME))
.with_context(ContextOp::AddStyleSheet(StyleSheet::located(
"/homedemo/css/styles.css",
)))
@ -55,11 +55,11 @@ fn hello_world() -> Container {
.with_classes(ClassesOp::Add, "hello-col-text")
.with_size(grid::ColumnSize::Is5of12)
.with_component(
Heading::h1(L10n::t("page_title", &LOCALE_DEMOHOME))
Heading::h1(Text::t("page_title", &LOCALE_DEMOHOME))
.with_display(HeadingDisplay::Medium),
)
.with_component(
Paragraph::with(L10n::e("hello_intro", &LOCALE_DEMOHOME).with_arg(
Paragraph::with(Text::e("hello_intro", &LOCALE_DEMOHOME).with_arg(
"app",
format!(
"<span class=\"app-name\">{}</span>",
@ -69,7 +69,7 @@ fn hello_world() -> Container {
.with_display(ParagraphDisplay::Small),
)
.with_component(Paragraph::with(
L10n::e("hello_powered", &LOCALE_DEMOHOME).with_arg(
Text::e("hello_powered", &LOCALE_DEMOHOME).with_arg(
"pagetop",
format!(
"<a href=\"{}\" target=\"_blank\">{}</a>",
@ -80,14 +80,14 @@ fn hello_world() -> Container {
.with_component(
Anchor::button(
"https://github.com/manuelcillero/pagetop",
L10n::t("hello_code", &LOCALE_DEMOHOME),
Text::t("hello_code", &LOCALE_DEMOHOME),
)
.with_target(AnchorTarget::Blank)
.with_left_icon(Icon::with("git"))
.with_classes(ClassesOp::Add, "code-link"),
)
.with_component(
Anchor::link("#welcome", L10n::t("hello_welcome", &LOCALE_DEMOHOME))
Anchor::link("#welcome", Text::t("hello_welcome", &LOCALE_DEMOHOME))
.with_left_icon(Icon::with("arrow-down-circle-fill"))
.with_classes(ClassesOp::Add, "welcome-link"),
),
@ -104,9 +104,9 @@ fn welcome() -> Container {
Container::section()
.with_id("welcome")
.with_classes(ClassesOp::Add, "welcome-col-text")
.with_component(Heading::h2(L10n::t("welcome_page", &LOCALE_DEMOHOME)))
.with_component(Heading::h2(Text::t("welcome_page", &LOCALE_DEMOHOME)))
.with_component(
Heading::h3(L10n::e("welcome_subtitle", &LOCALE_DEMOHOME).with_arg(
Heading::h3(Text::e("welcome_subtitle", &LOCALE_DEMOHOME).with_arg(
"app",
format!(
"<span class=\"app-name\">{}</span>",
@ -116,10 +116,10 @@ fn welcome() -> Container {
.with_display(HeadingDisplay::Subtitle),
)
.with_component(
Paragraph::with(L10n::t("welcome_text1", &LOCALE_DEMOHOME))
Paragraph::with(Text::t("welcome_text1", &LOCALE_DEMOHOME))
.with_display(ParagraphDisplay::Small),
)
.with_component(Paragraph::with(L10n::t("welcome_text2", &LOCALE_DEMOHOME)))
.with_component(Paragraph::with(Text::t("welcome_text2", &LOCALE_DEMOHOME)))
}
fn about_pagetop() -> Container {
@ -134,14 +134,14 @@ fn about_pagetop() -> Container {
.with_column(
grid::Column::new()
.with_classes(ClassesOp::Add, "pagetop-col-text")
.with_component(Heading::h2(L10n::t("pagetop_title", &LOCALE_DEMOHOME)))
.with_component(Heading::h2(Text::t("pagetop_title", &LOCALE_DEMOHOME)))
.with_component(
Paragraph::with(L10n::t("pagetop_text1", &LOCALE_DEMOHOME))
Paragraph::with(Text::t("pagetop_text1", &LOCALE_DEMOHOME))
.with_display(ParagraphDisplay::Small),
)
.with_component(Paragraph::with(L10n::t("pagetop_text2", &LOCALE_DEMOHOME)))
.with_component(Paragraph::with(Text::t("pagetop_text2", &LOCALE_DEMOHOME)))
.with_component(Paragraph::with(
L10n::e("pagetop_text3", &LOCALE_DEMOHOME).with_arg(
Text::e("pagetop_text3", &LOCALE_DEMOHOME).with_arg(
"pagetop_website",
format!(
"<a href=\"{}\" target=\"_blank\">{}</a>",
@ -160,12 +160,12 @@ fn promo_pagetop() -> Container {
.with_column(
grid::Column::new()
.with_classes(ClassesOp::Add, "promo-col-text")
.with_component(Heading::h2(L10n::t(
.with_component(Heading::h2(Text::t(
"pagetop_promo_title",
&LOCALE_DEMOHOME,
)))
.with_component(
Paragraph::with(L10n::e("pagetop_promo_text1", &LOCALE_DEMOHOME).with_arg(
Paragraph::with(Text::e("pagetop_promo_text1", &LOCALE_DEMOHOME).with_arg(
"pagetop",
format!(
"<a href=\"{}\" target=\"_blank\">{}</a>",
@ -196,15 +196,15 @@ fn reporting_issues() -> Container {
grid::Column::new()
.with_classes(ClassesOp::Add, "reporting-col-text")
.with_size(grid::ColumnSize::Is6of12)
.with_component(Heading::h2(L10n::t(
.with_component(Heading::h2(Text::t(
"report_problems_title",
&LOCALE_DEMOHOME,
)))
.with_component(
Paragraph::with(L10n::t("report_problems_text1", &LOCALE_DEMOHOME))
Paragraph::with(Text::t("report_problems_text1", &LOCALE_DEMOHOME))
.with_display(ParagraphDisplay::Small),
)
.with_component(Paragraph::with(L10n::t(
.with_component(Paragraph::with(Text::t(
"report_problems_text2",
&LOCALE_DEMOHOME,
))),

View file

@ -16,6 +16,7 @@ license = "Apache-2.0 OR MIT"
[dependencies]
pagetop = { version = "0.0", path = "../pagetop" }
pagetop-jquery = { version = "0.0", path = "../pagetop-jquery" }
pagetop-minimal = { version = "0.0", path = "../pagetop-minimal" }
static-files = "0.2.3"
[build-dependencies]

View file

@ -2,7 +2,8 @@ use pagetop::prelude::*;
define_handle!(COMPONENT_MEGAMENUITEM);
type Label = OneComponent<L10n>;
type Label = OneComponent<Text>;
type Content = OneComponent<Html>;
#[derive(Default)]
pub enum MegaMenuItemType {
@ -11,7 +12,7 @@ pub enum MegaMenuItemType {
Label(Label),
Link(Label, String),
LinkBlank(Label, String),
Html(Markup),
Html(Content),
Submenu(Label, MegaMenu),
Separator,
}
@ -58,8 +59,8 @@ impl ComponentTrait for MegaMenuItem {
a href=(path) target="_blank" { (label.render(rcx)) }
}
},
MegaMenuItemType::Html(html) => html! {
li class="html" { (*html) }
MegaMenuItemType::Html(content) => html! {
li class="html" { (content.render(rcx)) }
},
MegaMenuItemType::Submenu(label, menu) => html! {
li class="submenu" {
@ -85,35 +86,35 @@ impl ComponentTrait for MegaMenuItem {
}
impl MegaMenuItem {
pub fn label(label: L10n) -> Self {
pub fn label(label: Text) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Label(OneComponent::new_with(label)),
..Default::default()
}
}
pub fn link(label: L10n, path: &str) -> Self {
pub fn link(label: Text, path: &str) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Link(OneComponent::new_with(label), path.to_owned()),
..Default::default()
}
}
pub fn link_blank(label: L10n, path: &str) -> Self {
pub fn link_blank(label: Text, path: &str) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::LinkBlank(OneComponent::new_with(label), path.to_owned()),
..Default::default()
}
}
pub fn html(html: Markup) -> Self {
pub fn html(content: Html) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Html(html),
item_type: MegaMenuItemType::Html(OneComponent::new_with(content)),
..Default::default()
}
}
pub fn submenu(label: L10n, menu: MegaMenu) -> Self {
pub fn submenu(label: Text, menu: MegaMenu) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Submenu(OneComponent::new_with(label), menu),
..Default::default()

View file

@ -14,7 +14,7 @@ impl ModuleTrait for MegaMenu {
}
fn dependencies(&self) -> Vec<ModuleStaticRef> {
vec![&pagetop_jquery::JQuery]
vec![&pagetop_jquery::JQuery, &pagetop_minimal::Minimal]
}
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {

View file

@ -3,8 +3,6 @@ pub use container::{Container, ContainerType, COMPONENT_CONTAINER};
pub mod grid;
mod html;
pub use html::{Html, COMPONENT_HTML};
mod icon;
pub use icon::{Icon, COMPONENT_ICON};
mod heading;

View file

@ -23,7 +23,7 @@ pub enum AnchorTarget {
}
type AnchorIcon = OneComponent<Icon>;
type AnchorHtml = OneComponent<L10n>;
type AnchorHtml = OneComponent<Text>;
#[rustfmt::skip]
#[derive(Default)]
@ -34,7 +34,7 @@ pub struct Anchor {
classes : Classes,
anchor_type: AnchorType,
href : AttributeValue,
html10n : AnchorHtml,
html : AnchorHtml,
left_icon : AnchorIcon,
right_icon : AnchorIcon,
target : AnchorTarget,
@ -91,15 +91,15 @@ impl ComponentTrait for Anchor {
}
impl Anchor {
pub fn link(href: &str, html10n: L10n) -> Self {
Anchor::new().with_href(href).with_html(html10n)
pub fn link(href: &str, html: Text) -> Self {
Anchor::new().with_href(href).with_html(html)
}
pub fn button(href: &str, html10n: L10n) -> Self {
pub fn button(href: &str, html: Text) -> Self {
Anchor::new()
.with_type(AnchorType::Button)
.with_href(href)
.with_html(html10n)
.with_html(html)
}
pub fn location(id: &str) -> Self {
@ -152,8 +152,8 @@ impl Anchor {
}
#[fn_builder]
pub fn alter_html(&mut self, html10n: L10n) -> &mut Self {
self.html10n.set(html10n);
pub fn alter_html(&mut self, html: Text) -> &mut Self {
self.html.set(html);
self
}
@ -200,7 +200,7 @@ impl Anchor {
}
pub fn html(&self) -> &AnchorHtml {
&self.html10n
&self.html
}
pub fn left_icon(&self) -> &AnchorIcon {

View file

@ -10,7 +10,7 @@ pub enum ButtonType {
Reset,
}
type ButtonValue = OneComponent<L10n>;
type ButtonValue = OneComponent<Text>;
#[rustfmt::skip]
#[derive(Default)]
@ -77,11 +77,11 @@ impl ComponentTrait for Button {
}
impl Button {
pub fn with(value: L10n) -> Self {
pub fn with(value: Text) -> Self {
Button::new().with_value(value)
}
pub fn submit(value: L10n) -> Self {
pub fn submit(value: Text) -> Self {
let mut button = Button::new()
.with_classes(ClassesOp::Replace("form-button"), "form-submit")
.with_value(value);
@ -89,7 +89,7 @@ impl Button {
button
}
pub fn reset(value: L10n) -> Self {
pub fn reset(value: Text) -> Self {
let mut button = Button::new()
.with_classes(ClassesOp::Replace("form-button"), "form-reset")
.with_value(value);
@ -124,7 +124,7 @@ impl Button {
}
#[fn_builder]
pub fn alter_value(&mut self, value: L10n) -> &mut Self {
pub fn alter_value(&mut self, value: Text) -> &mut Self {
self.value.set(value);
self
}

View file

@ -13,8 +13,8 @@ pub enum InputType {
Url,
}
type InputLabel = OneComponent<L10n>;
type InputHelpText = OneComponent<L10n>;
type InputLabel = OneComponent<Text>;
type InputHelpText = OneComponent<Text>;
#[rustfmt::skip]
#[derive(Default)]
@ -206,7 +206,7 @@ impl Input {
}
#[fn_builder]
pub fn alter_label(&mut self, label: L10n) -> &mut Self {
pub fn alter_label(&mut self, label: Text) -> &mut Self {
self.label.set(label);
self
}
@ -281,7 +281,7 @@ impl Input {
}
#[fn_builder]
pub fn alter_help_text(&mut self, help_text: L10n) -> &mut Self {
pub fn alter_help_text(&mut self, help_text: Text) -> &mut Self {
self.help_text.set(help_text);
self
}

View file

@ -25,7 +25,7 @@ pub enum HeadingDisplay {
Subtitle,
}
type HeadingText = OneComponent<L10n>;
type HeadingText = OneComponent<Text>;
#[rustfmt::skip]
#[derive(Default)]
@ -80,37 +80,37 @@ impl ComponentTrait for Heading {
}
impl Heading {
pub fn h1(text: L10n) -> Self {
pub fn h1(text: Text) -> Self {
Heading::new()
.with_heading_type(HeadingType::H1)
.with_text(text)
}
pub fn h2(text: L10n) -> Self {
pub fn h2(text: Text) -> Self {
Heading::new()
.with_heading_type(HeadingType::H2)
.with_text(text)
}
pub fn h3(text: L10n) -> Self {
pub fn h3(text: Text) -> Self {
Heading::new()
.with_heading_type(HeadingType::H3)
.with_text(text)
}
pub fn h4(text: L10n) -> Self {
pub fn h4(text: Text) -> Self {
Heading::new()
.with_heading_type(HeadingType::H4)
.with_text(text)
}
pub fn h5(text: L10n) -> Self {
pub fn h5(text: Text) -> Self {
Heading::new()
.with_heading_type(HeadingType::H5)
.with_text(text)
}
pub fn h6(text: L10n) -> Self {
pub fn h6(text: Text) -> Self {
Heading::new()
.with_heading_type(HeadingType::H6)
.with_text(text)
@ -149,7 +149,7 @@ impl Heading {
}
#[fn_builder]
pub fn alter_text(&mut self, text: L10n) -> &mut Self {
pub fn alter_text(&mut self, text: Text) -> &mut Self {
self.text.set(text);
self
}

View file

@ -1,84 +0,0 @@
use pagetop::prelude::*;
define_handle!(COMPONENT_HTML);
#[rustfmt::skip]
#[derive(Default)]
pub struct Html {
weight : isize,
renderable: Renderable,
html : HtmlMarkup,
template : String,
}
impl ComponentTrait for Html {
fn new() -> Self {
Html::default()
}
fn handle(&self) -> Handle {
COMPONENT_HTML
}
fn weight(&self) -> isize {
self.weight
}
fn is_renderable(&self, rcx: &RenderContext) -> bool {
(self.renderable.check)(rcx)
}
fn default_render(&self, _: &mut RenderContext) -> Markup {
html! { (*self.html()) }
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Html {
pub fn with(html: Markup) -> Self {
Html::new().with_html(html)
}
// Html BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, weight: isize) -> &mut Self {
self.weight = weight;
self
}
#[fn_builder]
pub fn alter_renderable(&mut self, check: IsRenderable) -> &mut Self {
self.renderable.check = check;
self
}
#[fn_builder]
pub fn alter_html(&mut self, html: Markup) -> &mut Self {
self.html.markup = html;
self
}
#[fn_builder]
pub fn alter_template(&mut self, template: &str) -> &mut Self {
self.template = template.to_owned();
self
}
// Html GETTERS.
pub fn html(&self) -> &Markup {
&self.html.markup
}
pub fn template(&self) -> &str {
self.template.as_str()
}
}

View file

@ -41,7 +41,7 @@ impl ModuleTrait for Node {
}
async fn node(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request).with_title(L10n::n("Nodo")).render()
Page::new(request).with_title(Text::n("Nodo")).render()
}
fn before_render_page(page: &mut Page) {

View file

@ -42,7 +42,7 @@ impl ModuleTrait for User {
async fn login(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request)
.with_title(L10n::n("Identificación del usuario"))
.with_title(Text::n("Identificación del usuario"))
.with_this_in(
"region-content",
Container::new()
@ -58,9 +58,9 @@ fn form_login() -> Form {
.with_element(
form_element::Input::textfield()
.with_name("name")
.with_label(L10n::t("username", &LOCALE_USER))
.with_label(Text::t("username", &LOCALE_USER))
.with_help_text(
L10n::t("username_help", &LOCALE_USER)
Text::t("username_help", &LOCALE_USER)
.with_arg("app", config::SETTINGS.app.name.to_owned()),
)
.with_autofocus(true),
@ -68,8 +68,8 @@ fn form_login() -> Form {
.with_element(
form_element::Input::password()
.with_name("pass")
.with_label(L10n::t("password", &LOCALE_USER))
.with_help_text(L10n::t("password_help", &LOCALE_USER)),
.with_label(Text::t("password", &LOCALE_USER))
.with_help_text(Text::t("password_help", &LOCALE_USER)),
)
.with_element(form_element::Button::submit(L10n::t("login", &LOCALE_USER)))
.with_element(form_element::Button::submit(Text::t("login", &LOCALE_USER)))
}

View file

@ -17,8 +17,6 @@ pub(crate) use all::common_components;
mod renderable;
pub use renderable::{IsRenderable, Renderable};
mod html_markup;
pub use html_markup::HtmlMarkup;
mod l10n;
pub use l10n::L10n;
mod basic;
pub use basic::{Html, COMPONENT_HTML};
pub use basic::{Text, COMPONENT_TEXT};

View file

@ -0,0 +1,143 @@
use crate::core::component::{AnyComponent, ComponentTrait, RenderContext};
use crate::html::{html, Markup, PreEscaped};
use crate::locale::{translate, Locale, Locales};
use crate::{define_handle, fn_builder, paste, Handle};
use std::collections::HashMap;
macro_rules! basic_components {
( $($COMPONENT_HANDLE:ident: $Component:ty => $TypeValue:ty),* ) => { $( paste! {
define_handle!($COMPONENT_HANDLE);
pub enum [< $Component Op >] {
None,
Value($TypeValue),
Translated(&'static str, &'static Locales),
Escaped(&'static str, &'static Locales),
}
pub struct $Component {
op: [< $Component Op >],
args: HashMap<&'static str, String>,
}
impl Default for $Component {
fn default() -> Self {
$Component {
op: [< $Component Op >]::None,
args: HashMap::new(),
}
}
}
impl ComponentTrait for $Component {
fn new() -> Self {
$Component::default()
}
fn handle(&self) -> Handle {
$COMPONENT_HANDLE
}
fn default_render(&self, rcx: &mut RenderContext) -> Markup {
match self.op() {
[< $Component Op >]::None => html! {},
[< $Component Op >]::Value(value) => html! { (value) },
[< $Component Op >]::Translated(key, locales) => html! {
(translate(
key,
Locale::Using(
rcx.langid(),
locales,
&self.args().iter().fold(HashMap::new(), |mut args, (key, value)| {
args.insert(key.to_string(), value.to_owned().into());
args
})
)
))
},
[< $Component Op >]::Escaped(key, locales) => html! {
(PreEscaped(translate(
key,
Locale::Using(
rcx.langid(),
locales,
&self.args().iter().fold(HashMap::new(), |mut args, (key, value)| {
args.insert(key.to_string(), value.to_owned().into());
args
})
)
)))
},
}
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl $Component {
pub fn n(value: $TypeValue) -> Self {
$Component {
op: [< $Component Op >]::Value(value),
..Default::default()
}
}
pub fn t(key: &'static str, locales: &'static Locales) -> Self {
$Component {
op: [< $Component Op >]::Translated(key, locales),
..Default::default()
}
}
pub fn e(key: &'static str, locales: &'static Locales) -> Self {
$Component {
op: [< $Component Op >]::Escaped(key, locales),
..Default::default()
}
}
// $Component BUILDER.
#[fn_builder]
pub fn alter_op(&mut self, op: [< $Component Op >]) -> &mut Self {
self.op = op;
self
}
#[fn_builder]
pub fn alter_arg(&mut self, arg: &'static str, value: String) -> &mut Self {
self.args.insert(arg, value);
self
}
pub fn clear_args(&mut self) -> &mut Self {
self.args.drain();
self
}
// $Component GETTERS.
pub fn op(&self) -> &[< $Component Op >] {
&self.op
}
pub fn args(&self) -> &HashMap<&str, String> {
&self.args
}
}
} )* };
}
basic_components!(
COMPONENT_HTML: Html => Markup,
COMPONENT_TEXT: Text => &'static str
);

View file

@ -1,46 +0,0 @@
use crate::core::component::{AnyComponent, ComponentTrait, RenderContext};
use crate::html::{html, Markup};
use crate::{define_handle, Handle};
define_handle!(HTML_MARKUP);
pub struct HtmlMarkup {
pub markup: Markup,
}
impl Default for HtmlMarkup {
fn default() -> Self {
HtmlMarkup { markup: html! {} }
}
}
impl ComponentTrait for HtmlMarkup {
fn new() -> Self {
HtmlMarkup::default()
}
fn handle(&self) -> Handle {
HTML_MARKUP
}
fn default_render(&self, _rcx: &mut RenderContext) -> Markup {
html! {
(self.markup)
}
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl HtmlMarkup {
pub fn with(mut self, html: Markup) -> Self {
self.markup = html;
self
}
}

View file

@ -1,138 +0,0 @@
use crate::core::component::{AnyComponent, ComponentTrait, RenderContext};
use crate::html::{html, Markup, PreEscaped};
use crate::locale::{translate, Locale, Locales};
use crate::{define_handle, fn_builder, Handle};
use std::collections::HashMap;
define_handle!(COMPONENT_L10N);
#[rustfmt::skip]
#[derive(Default)]
pub struct L10n {
key : &'static str,
locales: Option<&'static Locales>,
args : HashMap<&'static str, String>,
escaped: bool,
}
impl ComponentTrait for L10n {
fn new() -> Self {
L10n::default()
}
fn handle(&self) -> Handle {
COMPONENT_L10N
}
fn default_render(&self, rcx: &mut RenderContext) -> Markup {
if let Some(locales) = self.locales() {
html! {
@if self.escaped() {
(PreEscaped(translate(
self.key(),
Locale::Using(
rcx.langid(),
locales,
&self.args().iter().fold(HashMap::new(), |mut args, (key, value)| {
args.insert(key.to_string(), value.to_owned().into());
args
})
)
)))
} @else {
(translate(
self.key(),
Locale::Using(
rcx.langid(),
locales,
&self.args().iter().fold(HashMap::new(), |mut args, (key, value)| {
args.insert(key.to_string(), value.to_owned().into());
args
})
)
))
}
}
} else {
html! { (self.key()) }
}
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl L10n {
pub fn n(text: &'static str) -> Self {
L10n {
key: text,
..Default::default()
}
}
pub fn t(key: &'static str, locales: &'static Locales) -> Self {
L10n {
key,
locales: Some(locales),
..Default::default()
}
}
pub fn e(key: &'static str, locales: &'static Locales) -> Self {
L10n {
key,
locales: Some(locales),
escaped: true,
..Default::default()
}
}
// HtmL10n BUILDER.
#[fn_builder]
pub fn alter_key(&mut self, key: &'static str) -> &mut Self {
self.key = key;
self
}
#[fn_builder]
pub fn alter_locales(&mut self, locales: &'static Locales) -> &mut Self {
self.locales = Some(locales);
self
}
#[fn_builder]
pub fn alter_arg(&mut self, arg: &'static str, value: String) -> &mut Self {
self.args.insert(arg, value);
self
}
pub fn clear_args(&mut self) -> &mut Self {
self.args.drain();
self
}
// HtmL10n GETTERS.
pub fn key(&self) -> &str {
self.key
}
pub fn locales(&self) -> Option<&Locales> {
self.locales
}
pub fn args(&self) -> &HashMap<&str, String> {
&self.args
}
pub fn escaped(&self) -> bool {
self.escaped
}
}

View file

@ -94,8 +94,9 @@
//! println!("* {}\n", _t("shared-photos", Locale::With(&LOCALE_SAMPLE, &args)));
//! }
//! ```
//! Aunque normalmente usarás el componente [L10n](crate::core::component::L10n) para añadir textos
//! traducibles en las respuestas web según el contexto del renderizado.
//! Aunque preferirás usar normalmente los componentes básicos [Text](crate::core::component::Text)
//! y [Html](crate::core::component::Html) para incluir, en respuestas a las peticiones web, textos
//! y contenidos opcionalmente traducibles según el contexto de renderizado.
use crate::html::{Markup, PreEscaped};
use crate::{args, config, trace, LazyStatic};

View file

@ -3,7 +3,7 @@ pub use error403::ERROR_403;
mod error404;
pub use error404::ERROR_404;
use crate::core::component::L10n;
use crate::core::component::Text;
use crate::response::{page::Page, ResponseError};
use crate::server::http::{header::ContentType, StatusCode};
use crate::server::{HttpRequest, HttpResponse};
@ -32,7 +32,7 @@ impl fmt::Display for FatalError {
FatalError::AccessDenied(request) => {
let error_page = Page::new(request.clone());
if let Ok(page) = error_page
.with_title(L10n::n("Error FORBIDDEN"))
.with_title(Text::n("Error FORBIDDEN"))
.with_this_in("region-content", error403::Error403)
.with_template("error")
.render()
@ -46,7 +46,7 @@ impl fmt::Display for FatalError {
FatalError::NotFound(request) => {
let error_page = Page::new(request.clone());
if let Ok(page) = error_page
.with_title(L10n::n("Error RESOURCE NOT FOUND"))
.with_title(Text::n("Error RESOURCE NOT FOUND"))
.with_this_in("region-content", error404::Error404)
.with_template("error")
.render()

View file

@ -11,8 +11,8 @@ use unic_langid::CharacterDirection;
use std::collections::HashMap;
type PageTitle = OneComponent<L10n>;
type PageDescription = OneComponent<L10n>;
type PageTitle = OneComponent<Text>;
type PageDescription = OneComponent<Text>;
#[rustfmt::skip]
pub struct Page {
@ -60,13 +60,13 @@ impl Page {
}
#[fn_builder]
pub fn alter_title(&mut self, title: L10n) -> &mut Self {
pub fn alter_title(&mut self, title: Text) -> &mut Self {
self.title.set(title);
self
}
#[fn_builder]
pub fn alter_description(&mut self, description: L10n) -> &mut Self {
pub fn alter_description(&mut self, description: Text) -> &mut Self {
self.description.set(description);
self
}