Skip to content

Commit

Permalink
Display Vietnamese title for categories
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquan committed Oct 27, 2023
1 parent acef55b commit baced44
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion minijinja/blog/block_post_content.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<svg xmlns='http://www.w3.org/2000/svg' class='h-6 w-6 inline-block' fill='none' viewBox='0 0 24 24' stroke='currentColor'>
<path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z' />
</svg>
<span>{{ cat.title }}</span>
<span>{{ cat.title_vi if lang == 'vi' and cat.title_vi else cat.title }}</span>
</a>
{% else %}
<a rel='category' href='{{ UNCATEGORIZED_URL }}'>Uncategorized</a>
Expand Down
2 changes: 1 addition & 1 deletion minijinja/navbar.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
x-show='open' x-on:click.away='open = false' x-cloak>
{% for cat in categories %}
<li>
<a href='/category/{{ cat.slug }}/' class='block px-4 py-2 mt-2 bg-transparent rounded-lg dark-mode:bg-transparent dark-mode:hover:bg-gray-600 dark-mode:focus:bg-gray-600 dark-mode:focus:text-white dark-mode:hover:text-white dark-mode:text-gray-200 md:mt-0 hover:text-gray-900 focus:text-gray-900 hover:bg-gray-50 focus:bg-gray-100 focus:outline-none focus:shadow-outline'>{{ cat.title }}</a>
<a href='/category/{{ cat.slug }}/' class='block px-4 py-2 mt-2 bg-transparent rounded-lg dark-mode:bg-transparent dark-mode:hover:bg-gray-600 dark-mode:focus:bg-gray-600 dark-mode:focus:text-white dark-mode:hover:text-white dark-mode:text-gray-200 md:mt-0 hover:text-gray-900 focus:text-gray-900 hover:bg-gray-50 focus:bg-gray-100 focus:outline-none focus:shadow-outline'>{{ cat.title_vi if lang == 'vi' and cat.title_vi else cat.title }}</a>
</li>
{% endfor %}
<li class='divider'></li>
Expand Down
47 changes: 29 additions & 18 deletions src/front/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
pub mod blog;
pub mod minors;
pub mod feeds;
pub mod minors;
pub mod old_urls;

use std::num::NonZeroU16;

use axum_sessions::extractors::{WritableSession, ReadableSession};
use axum::extract::Form;
use serde::ser::Serialize;
use minijinja::Environment;
use http::{StatusCode, Uri, HeaderName};
use axum::extract::{Query, State, OriginalUri};
use axum::extract::{OriginalUri, Query, State};
use axum::response::{Html, IntoResponse, Result as AxumResult};
use axum_sessions::extractors::{ReadableSession, WritableSession};
use http::{HeaderName, StatusCode, Uri};
use minijinja::context;
use minijinja::Environment;
use serde::ser::Serialize;
use unic_langid::LanguageIdentifier;

pub use crate::errors::PageError;
use crate::auth::Auth;
use crate::types::{AppState, Paginator, StaticFile};
use super::structs::{LaxPaging, SetLangReq};
use crate::auth::Auth;
use crate::consts::{DEFAULT_LANG, DEFAULT_PAGE_SIZE, KEY_LANG, STATIC_URL};
pub use crate::errors::PageError;
use crate::stores;
use crate::consts::{DEFAULT_PAGE_SIZE, STATIC_URL, KEY_LANG, DEFAULT_LANG};
use crate::types::{AppState, Paginator, StaticFile};

pub fn render_with<S: Serialize>(template_name: &str, context: S, engine: Environment) -> Result<String, PageError> {
pub fn render_with<S: Serialize>(
template_name: &str,
context: S,
engine: Environment,
) -> Result<String, PageError> {
let tpl = engine.get_template(template_name)?;
let content = tpl.render(context)?;
Ok(content)
}


pub async fn fallback_view() -> (StatusCode, &'static str) {
(StatusCode::NOT_FOUND, "Not found")
}


pub async fn home(
auth: Auth,
OriginalUri(current_url): OriginalUri,
Expand Down Expand Up @@ -65,7 +67,9 @@ pub async fn home(
.await
.map_err(PageError::EdgeDBQueryError)?;
let no_tracking = auth.current_user.is_some();
let lang = session.get::<String>(KEY_LANG).unwrap_or(DEFAULT_LANG.into());
let lang = session
.get::<String>(KEY_LANG)
.unwrap_or(DEFAULT_LANG.into());
let context = context!(
lang => lang,
posts => posts,
Expand All @@ -89,10 +93,17 @@ pub async fn static_handler(uri: Uri) -> impl IntoResponse {

type HTMXResponse = ([(HeaderName, &'static str); 1], Html<String>);

pub async fn set_lang(mut session: WritableSession, Form(payload): Form<SetLangReq>) -> AxumResult<HTMXResponse>
{
let li: LanguageIdentifier = payload.lang.parse().map_err(|_e| StatusCode::UNPROCESSABLE_ENTITY)?;
session.insert(KEY_LANG, li.clone()).map_err(|_e| StatusCode::SERVICE_UNAVAILABLE)?;
pub async fn set_lang(
mut session: WritableSession,
Form(payload): Form<SetLangReq>,
) -> AxumResult<HTMXResponse> {
let li: LanguageIdentifier = payload
.lang
.parse()
.map_err(|_e| StatusCode::UNPROCESSABLE_ENTITY)?;
session
.insert(KEY_LANG, li.clone())
.map_err(|_e| StatusCode::SERVICE_UNAVAILABLE)?;
let lang_code = li.to_string();
let header_name = HeaderName::from_static("hx-refresh");
let r = ([(header_name, "true")], Html(lang_code));
Expand Down

0 comments on commit baced44

Please sign in to comment.