Skip to content

Commit

Permalink
Merge pull request #91 from hashworks/2.x-englishTelegram
Browse files Browse the repository at this point in the history
Use ctl10n to provide english localization
  • Loading branch information
iovxw authored Jun 2, 2020
2 parents 8375a1a + ab7867b commit f5623f0
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
ctl10n_macros.rs
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ default = []
# enable this to set a subscription limit
hosted-by-iovxw = []

[build-dependencies]
ctl10n = "0.2.0"

[dependencies]
lazy_static = "1.4"
once_cell = "1.4"
Expand All @@ -34,4 +37,4 @@ features = ["rustls", "proxy"]
[dependencies.reqwest]
version = "0.10"
default-features = false
features = ["rustls-tls", "gzip", "json", "trust-dns"]
features = ["rustls-tls", "gzip", "json", "trust-dns"]
11 changes: 2 additions & 9 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ Chinese Telegram RSS bot [@RustRssBot](http://t.me/RustRssBot)
- [x] Atom 1.0
- [x] JSON Feed 1

## WIP

English version is still work in progress.

**Unresolved problems:**
* The command description in Telegram can't be internationalized

## Usage

/rss - Display a list of currently subscribed RSS feeds
Expand All @@ -32,7 +25,7 @@ English version is still work in progress.

## Download

The pre-compiled binaries can be downloaded directly from [Releases](https://github.com/iovxw/rssbot/releases). The Linux version is statically linked to *musl*, no other dependencies required.
The pre-compiled binaries can be downloaded directly from [Releases](https://github.com/iovxw/rssbot/releases). Make sure to use the english binary (`rssbot-en-amd64-linux`). The Linux version is statically linked to *musl*, no other dependencies required.

## Compile

Expand All @@ -41,7 +34,7 @@ The pre-compiled binaries can be downloaded directly from [Releases](https://git
Install *Rust Nightly* and *Cargo* ([`rustup` recommended](https://www.rustup.rs/)) first, then:

```
cargo build --release
LOCALE=en cargo build --release
```

The compiled files are available at: `./target/release/rssbot`
Expand Down
16 changes: 16 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ctl10n;
use std::env;

fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=locales/*.toml");
if let Err(err) = ctl10n::convert_strings_file(
format!(
"locales/{}.toml",
&env::var("LOCALE").unwrap_or("ch".to_string())
),
"ctl10n_macros.rs",
) {
panic!("{}", err);
}
}
34 changes: 34 additions & 0 deletions locales/ch.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
rss_size_limit_exceeded = "RSS 超出大小限制({size})"
continuous_fetch_error = "《<a href=\"{link}\">{title}</a>》已经连续 5 天拉取出错 ({error}), 可能已经关闭, 请取消订阅"
feed_renamed = "<a href=\"{link}\">{title}</a> 已更名为 {new_title}"
network_error = "网络错误({source})"
parsing_error = "解析错误({source})"
commands_in_private_channel = "请在私聊中使用命令为频道管理订阅"
start_message = """命令列表:
/rss - 显示当前订阅的 RSS 列表
/sub - 订阅一个 RSS:`/sub http://example.com/feed.xml`
/unsub - 退订一个 RSS:`/unsub http://example.com/feed.xml`
/export - 导出为 OPML
所有命令均可在后面跟上频道 ID 来管理频道订阅
例如 `/sub @BotNews http://example.com/feed.xml`"""
subscription_list = "订阅列表:"
subscription_list_empty = "订阅列表为空"
sub_how_to_use = "使用方法: /sub [Channel ID] <RSS URL>"
subscribed_to_rss = "已订阅过的 RSS"
subscription_rate_limit = """已达到全局最大订阅数量, 为防止服务器压力过大请退订不需要的 RSS 或者
[自己搭建服务](https://github.com/iovxw/rssbot)
注: 本机器人主要用于提供即时提醒功能, 例如服务器状态监控和社区论坛提醒
默认更新频率为 5 分钟, 不建议用于其他类型的 RSS 订阅
如有相关需求推荐使用其他 RSS 机器人实现"""
processing_please_wait = "处理中,请稍候"
subscription_succeeded = "《<a href=\"{link}\">{title}</a>》 订阅成功"
subscription_failed = "订阅失败: {error}"
unsub_how_to_use = "使用方法: /unsub [Channel ID] <RSS URL>"
unsubscription_succeeded = "《<a href=\"{link}\">{title}</a>》 退订成功"
unsubscribed_from_rss = "未订阅过的 RSS"
verifying_channel = "正在验证 Channel"
unable_to_find_target_channel = "无法找到目标 Channel:{desc}"
target_must_be_a_channel = "目标需为 Channel"
unable_to_get_channel_info = "无法获取频道信息({desc}),请将本 Bot 设为管理员"
admin_only_command = "该命令只能由 Channel 管理员使用"
make_bot_admin = "请将本 Bot 设为管理员"
36 changes: 36 additions & 0 deletions locales/en.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
rss_size_limit_exceeded = "RSS size limit exceeded ({size})"
continuous_fetch_error = "《<a href=\"{link}\">{title}</a>》has been pulled unsuccessfully for 5 consecutive days ({error}). It may have been closed, please unsubscribe."
feed_renamed = "<a href=\"{link}\">{title}</a> has been renamed to {new_title}"
network_error = "Network error ({source})"
parsing_error = "Parsing error ({source})"
commands_in_private_channel = "Please use commands in private chat to manage subscriptions for the channel"
start_message = """Command list:
/rss - Display a list of currently subscribed RSS feeds
/sub - Subscribe to an RSS: `/sub http://example.com/feed.xml`
/unsub - Unsubscribe from an RSS: `/unsub http://example.com/feed.xml`
/export - Export to OPML
All commands can be followed by the channel ID to manage channel subscriptions,
f.e. `/sub @BotNews http://example.com/feed.xml`"""
subscription_list = "Subscription list:"
subscription_list_empty = "Subscription list is empty"
sub_how_to_use = "How to use: /sub [Channel ID] <RSS URL>"
subscribed_to_rss = "Subscribed to RSS"
subscription_rate_limit = """The global maximum number of subscriptions has been reached.
To prevent excessive server pressure, please unsubscribe from unnecessary RSS or
[Build your own service](https://github.com/iovxw/rssbot).
Note: This bot is mainly used to provide instant reminder functions,
such as server status monitoring and community forum reminders.
The default update frequency is 5 minutes, not recommended for other types of RSS subscriptions.
If there are related requirements, it is recommended to use other RSS bots to achieve those."""
processing_please_wait = "Processing, please wait"
subscription_succeeded = "《<a href=\"{link}\">{title}</a>》 Subscription succeeded"
subscription_failed = "Subscription failed ({error})"
unsub_how_to_use = "How to use: /unsub [Channel ID] <RSS URL>"
unsubscription_succeeded = "《<a href=\"{link}\">{title}</a>》 Unsubscription succeeded"
unsubscribed_from_rss = "Unsubscribed from RSS"
verifying_channel = "Verifying channel"
unable_to_find_target_channel = "Unable to find the target channel: {desc}"
target_must_be_a_channel = "Target must be a channel"
unable_to_get_channel_info = "Unable to get channel information ({desc}), please grant this bot administrator rights"
admin_only_command = "This command can only be used by Channel administrators"
make_bot_admin = "Please grant this bot administrator rights"
12 changes: 7 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const RESP_SIZE_LIMIT: usize = 2 * 1024 * 1024;

static CLIENT: OnceCell<reqwest::Client> = OnceCell::new();

include! { "../ctl10n_macros.rs" }

#[derive(Error, Debug)]
pub enum FeedError {
#[error("network error")]
Expand All @@ -27,11 +29,11 @@ pub enum FeedError {
impl FeedError {
pub fn to_user_friendly(&self) -> String {
match self {
Self::Network(source) => format!("网络错误({})", source),
Self::Parsing(source) => format!("解析错误({})", source),
Self::TooLarge => format!(
"RSS 超出大小限制({})",
format_byte_size(RESP_SIZE_LIMIT as u64)
Self::Network(source) => tr!("network_error", source = source),
Self::Parsing(source) => tr!("parsing_error", source = source),
Self::TooLarge => tr!(
"rss_size_limit_exceeded",
size = format_byte_size(RESP_SIZE_LIMIT as u64)
),
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::client::pull_feed;
use crate::data::{Database, Feed, FeedUpdate};
use crate::messages::{format_large_msg, Escape};

include! { "../ctl10n_macros.rs" }

pub fn start(bot: Bot, db: Arc<Mutex<Database>>, min_interval: u32, max_interval: u32) {
let mut queue = FetchQueue::new();
// TODO: Don't use interval, it can accumulate ticks
Expand Down Expand Up @@ -76,13 +78,11 @@ async fn fetch_and_push_updates(
// 5 days
if down_time.unwrap().as_secs() > 5 * 24 * 60 * 60 {
db.lock().unwrap().reset_down_time(&feed.link);
let msg = format!(
"《<a href=\"{}\">{}</a>》\
已经连续 5 天拉取出错 ({}),\
可能已经关闭, 请取消订阅",
Escape(&feed.link),
Escape(&feed.title),
Escape(&e.to_user_friendly())
let msg = tr!(
"continuous_fetch_error",
link = Escape(&feed.link),
title = Escape(&feed.title),
error = Escape(&e.to_user_friendly())
);
push_updates(&bot, &db, feed.subscribers, parameters::Text::html(&msg)).await?;
}
Expand Down Expand Up @@ -119,11 +119,11 @@ async fn fetch_and_push_updates(
}
}
FeedUpdate::Title(new_title) => {
let msg = format!(
"<a href=\"{}\">{}</a> 已更名为 {}",
Escape(&feed.link),
Escape(&feed.title),
Escape(&new_title)
let msg = tr!(
"feed_renamed",
link = Escape(&feed.link),
title = Escape(&feed.title),
new_title = Escape(&new_title)
);
push_updates(
&bot,
Expand Down
Loading

0 comments on commit f5623f0

Please sign in to comment.