4 releases
new 0.1.3 | Mar 10, 2025 |
---|---|
0.1.2 | Mar 10, 2025 |
0.1.1 | Mar 10, 2025 |
0.1.0 | Mar 10, 2025 |
#450 in Internationalization (i18n)
60 downloads per month
20KB
310 lines
tran
use std::ops::Range;
use s_::EMPTY;
use lang::Lang;
use aok::{Result, Void};
mod tran;
pub use tran::Tran;
mod update;
pub use update::update;
pub trait Traner {
fn tran(
from_lang: Lang,
to_lang: Lang,
txt_li: &[String],
) -> impl std::future::Future<Output = Result<Vec<Option<String>>>> + Send;
}
pub trait Parse {
fn parse(txt: &str) -> Result<Vec<Range<usize>>>;
}
pub trait Cache {
fn get(
&self,
from_lang: Lang,
to_lang: Lang,
hash_li: &[Vec<u8>],
) -> impl std::future::Future<Output = Result<Vec<Option<String>>>> + Send;
fn set(
&self,
from_lang: Lang,
to_lang: Lang,
hash_li: &[Vec<u8>],
li: &Vec<String>,
) -> impl std::future::Future<Output = Void> + Send;
fn set_user(
&self,
from_lang: Lang,
to_lang: Lang,
hash_str_li: &[(Vec<u8>, String)],
) -> impl std::future::Future<Output = Void> + Send;
}
async fn tran<T: Traner>(
from_lang: Lang,
to_lang: Lang,
cache: impl Cache,
li: Vec<String>,
hash_li: Vec<Vec<u8>>,
) -> Result<Vec<String>> {
let mut result = Vec::with_capacity(li.len());
let mut to_tran = vec![];
if !li.is_empty() {
// todo 术语替换并计算有区别句子的hash
let j2f = from_lang == Lang::Zh && to_lang == Lang::ZhTW;
let f2j = from_lang == Lang::ZhTW && to_lang == Lang::Zh;
let mut pos_li = Vec::new();
for (pos, (s, c)) in li
.into_iter()
.zip(cache.get(from_lang, to_lang, &hash_li).await?.into_iter())
.enumerate()
{
if let Some(c) = c {
result.push(c);
} else if j2f {
result.push(cnu::j2f(s));
} else if f2j {
result.push(cnu::f2j(s));
} else {
to_tran.push(s);
result.push(EMPTY);
pos_li.push(pos);
}
}
if !to_tran.is_empty() {
let to_li = T::tran(from_lang, to_lang, &to_tran[..]).await?;
for (pos, to) in pos_li.into_iter().zip(to_li) {
if let Some(to) = to {
result[pos] = to;
}
}
cache.set(from_lang, to_lang, &hash_li, &result).await?;
}
}
Ok(result)
}
About
This project is an open-source component of i18n.site ⋅ Internationalization Solution.
-
i18 : MarkDown Command Line Translation Tool
The translation perfectly maintains the Markdown format.
It recognizes file changes and only translates the modified files.
The translated Markdown content is editable; if you modify the original text and translate it again, manually edited translations will not be overwritten (as long as the original text has not been changed).
-
i18n.site : MarkDown Multi-language Static Site Generator
Optimized for a better reading experience
关于
本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。
-
翻译能够完美保持 Markdown 的格式。能识别文件的修改,仅翻译有变动的文件。
Markdown 翻译内容可编辑;如果你修改原文并再次机器翻译,手动修改过的翻译不会被覆盖(如果这段原文没有被修改)。
-
i18n.site : MarkDown多语言静态站点生成器 为阅读体验而优化。
Dependencies
~4–11MB
~103K SLoC