Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback for role descriptions #1905

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

66 changes: 61 additions & 5 deletions src/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,45 @@ impl Default for TeamHelper {
}
}

enum TeamHelperParam {
/// `{{team-text team name}}`
Name,

/// `{{team-text team description}}`
Description,

/// `{{team-text team role (lookup member.roles 0)}}`
Role(String),
}

impl TeamHelperParam {
fn fluent_id(&self, team_name: &str) -> String {
match self {
TeamHelperParam::Name => format!("governance-team-{team_name}-name"),
TeamHelperParam::Description => format!("governance-team-{team_name}-description"),
TeamHelperParam::Role(role_id) => format!("governance-role-{role_id}"),
}
}

fn english<'a>(&'a self, team: &'a serde_json::Value) -> &'a str {
match self {
TeamHelperParam::Name => team["website_data"]["name"].as_str().unwrap(),
TeamHelperParam::Description => team["website_data"]["description"].as_str().unwrap(),
TeamHelperParam::Role(role_id) => {
for role in team["roles"].as_array().unwrap() {
if role["id"] == *role_id {
return role["description"].as_str().unwrap();
}
}
// This should never happen. The `validate_member_roles` test in
// the team repo enforces that `.members.*.roles.*` lines up
// with exactly one `.roles.*.id`.
role_id
}
}
}
}

impl HelperDef for TeamHelper {
fn call<'reg: 'rc, 'rc>(
&self,
Expand Down Expand Up @@ -137,6 +176,25 @@ impl HelperDef for TeamHelper {
"{{team-text}} takes only identifier parameters",
));
};

let param = match id.as_str() {
"name" => TeamHelperParam::Name,
"description" => TeamHelperParam::Description,
"role" => {
let Some(role_id) = h.param(2) else {
return Err(RenderError::new(
"{{team-text}} requires a third parameter for the role id",
));
};
TeamHelperParam::Role(role_id.value().as_str().unwrap().to_owned())
}
unrecognized => {
return Err(RenderError::new(format!(
"unrecognized {{{{team-text}}}} param {unrecognized:?}",
)));
}
};

let team = rcx
.evaluate(context, name)
.map_err(|e| RenderError::from_error(&format!("Cannot find team {}", name), e))?;
Expand All @@ -148,22 +206,20 @@ impl HelperDef for TeamHelper {
.expect("Language must be string");
let team_name = team.as_json()["name"].as_str().unwrap();

let fluent_id = format!("governance-team-{}-{}", team_name, id);

// English uses the team data directly, so that it gets autoupdated
if lang == "en-US" {
let english = team.as_json()["website_data"][id].as_str().unwrap();
let english = param.english(team.as_json());
out.write(english)
.map_err(|e| RenderError::from_error("failed to render English team data", e))?;
} else if let Some(value) = self.i18n.lookup_no_default_fallback(
&lang.parse().expect("language must be valid"),
&fluent_id,
&param.fluent_id(team_name),
None,
) {
out.write(&value)
.map_err(|e| RenderError::from_error("failed to render translated team data", e))?;
} else {
let english = team.as_json()["website_data"][id].as_str().unwrap();
let english = param.english(team.as_json());
out.write(english)
.map_err(|e| RenderError::from_error("failed to render", e))?;
}
Expand Down
6 changes: 0 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ use sass_rs::{compile_file, Options};
use category::Category;

use caching::CachedNamedFile;
use handlebars::handlebars_helper;
use handlebars_fluent::{loader::Loader, FluentHelper};
use i18n::{create_loader, LocaleInfo, SupportedLocale, TeamHelper, EXPLICIT_LOCALE_INFO};

Expand Down Expand Up @@ -492,11 +491,6 @@ async fn rocket() -> _ {
engine
.handlebars
.register_helper("encode-zulip-stream", Box::new(encode_zulip_stream));

handlebars_helper!(concat: |x: String, y: String| x + &y);
engine
.handlebars
.register_helper("concat", Box::new(concat));
});

let rust_version = RustVersion::fetch().await.unwrap_or_default();
Expand Down
1 change: 1 addition & 0 deletions src/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ mod tests {
zulip_stream: None,
weight: 0,
}),
roles: Vec::new(),
github: None,
discord: vec![],
}
Expand Down
2 changes: 1 addition & 1 deletion templates/governance/group-team.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<div>{{fluent "governance-user-team-leader"}}</div>
{{else}}
{{#if member.roles}}
<div>{{fluent (concat "governance-role-" (lookup member.roles 0))}}</div>
<div>{{team-text team role (lookup member.roles 0)}}</div>
{{/if}}
{{/if}}
</div>
Expand Down
Loading