Skip to content

Commit

Permalink
Don't panic if no language name found (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Jun 30, 2024
1 parent fbfb537 commit dce4938
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 298 deletions.
28 changes: 25 additions & 3 deletions book/src/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ By default, **leptos-fluent** supports JSON languages files. To use other
formats to load custom languages, the `json5` or `yaml` features can be
enabled:

<!-- markdownlint-disable MD013 -->

```toml
[dependencies]
fluent-templates = "0.9"
leptos-fluent = {
version = "0.1", features = ["json5"], default-features = false
}
leptos-fluent = { version = "0.1", features = ["json5"], default-features = false }
```

<!-- markdownlint-enable MD013 -->

```admonish tip
See [**4. Languages**](https://mondeja.github.io/leptos-fluent/languages.html).
```
Expand All @@ -113,5 +115,25 @@ See [**4. Languages**](https://mondeja.github.io/leptos-fluent/languages.html).
- **YAML languages file**: `yaml`
- **JSON5 languages file**: `json5`

## Tracking locales files with [`cargo leptos`]

Using [`cargo leptos`] watch of the _locales/_ folder for reloads:

```toml
# Relative to Cargo.toml file
[package.metadata.leptos]
watch-additional-files = ["locales"]
```

When inside a workspace, use the full path to the folder from the
workspace _Cargo.toml_ file:

```toml
# Relative to workspace Cargo.toml file
[package.metadata.leptos]
watch-additional-files = ["examples/csr/locales"]
```

[`fluent-templates`]: https://github.com/XAMPPRocky/fluent-templates
[`leptos_fluent!`]: https://mondeja.github.io/leptos-fluent/leptos_fluent.html
[`cargo leptos`]: https://github.com/leptos-rs/cargo-leptos
20 changes: 0 additions & 20 deletions book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,5 @@ Available features for languages file formats are:
- `yaml`: YAML
- `json5`: JSON5

## Tracking locales files with [`cargo leptos`]

Using [`cargo leptos`] watch of the _locales/_ folder for reloads:

```toml
# Relative to Cargo.toml file
[package.metadata.leptos]
watch-additional-files = ["locales"]
```

When inside a workspace, use the full path to the folder from the
workspace _Cargo.toml_ file:

```toml
# Relative to workspace Cargo.toml file
[package.metadata.leptos]
watch-additional-files = ["examples/csr/locales"]
```

[ISO 639-1 code]: https://en.wikipedia.org/wiki/ISO_639-1
[`cargo leptos`]: https://github.com/leptos-rs/cargo-leptos
[`leptos_fluent!`]: https://mondeja.github.io/leptos-fluent/leptos_fluent.html
26 changes: 19 additions & 7 deletions leptos-fluent-macros/src/languages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs;
use std::path::PathBuf;

type BuiltLanguagesFileLanguage = (String, String, String, Option<String>);
pub(crate) type ParsedLanguage = (String, String, String, Option<String>);

#[cfg(any(feature = "json", feature = "yaml", feature = "json5"))]
#[derive(serde::Deserialize)]
Expand All @@ -15,7 +15,7 @@ enum RawLanguagesFileLanguage {
#[cfg(any(feature = "json", feature = "yaml", feature = "json5"))]
fn fill_languages_file(
languages: &[RawLanguagesFileLanguage],
) -> Vec<(String, String, String, Option<String>)> {
) -> Vec<ParsedLanguage> {
let mut locales = vec![];
for tuple in languages {
match tuple {
Expand Down Expand Up @@ -69,7 +69,7 @@ fn fill_languages_file(

pub(crate) fn read_languages_file(
path: &PathBuf,
) -> Result<Vec<BuiltLanguagesFileLanguage>, String> {
) -> Result<Vec<ParsedLanguage>, String> {
#[cfg(feature = "json")]
{
let file_extension = path.extension().unwrap_or_default();
Expand Down Expand Up @@ -191,7 +191,9 @@ pub(crate) fn read_languages_file(

pub(crate) fn read_locales_folder(
path: &PathBuf,
) -> Vec<(String, String, String, Option<String>)> {
) -> (Vec<ParsedLanguage>, Vec<String>) {
let mut errors = vec![];

let mut iso639_language_codes = vec![];
let mut entries = vec![];
for entry in fs::read_dir(path).expect("Couldn't read locales folder") {
Expand Down Expand Up @@ -222,6 +224,16 @@ pub(crate) fn read_locales_folder(
> 1;
let lang_name =
language_name_from_language_code(&lang_code, use_country_code);
if lang_name.is_empty() {
errors.push(format!(
concat!(
"Couldn't find language name for code \"{}\". Provide it",
" from a languages file (see `languages` parameter).",
),
lang_code,
));
continue;
}
let lang_dir = iso639_to_dir(&iso639_code);
let flag = match code_to_country_code(&lang_code) {
Some(country_code) => country_code_to_emoji_flag(&country_code),
Expand All @@ -235,11 +247,11 @@ pub(crate) fn read_locales_folder(
));
}
locales.sort_by(|a, b| a.1.cmp(&b.1));
locales
(locales, errors)
}

pub(crate) fn build_languages_quote(
languages: &[(String, String, String, Option<String>)],
languages: &[ParsedLanguage],
) -> proc_macro2::TokenStream {
format!(
"[{}]",
Expand Down Expand Up @@ -1950,7 +1962,7 @@ fn language_name_from_language_code(
"za" => "Saɯ cueŋƅ",
"zh" => "中文",
"zu" => "isiZulu",
_ => panic!("Language name for language code '{code}' not found",),
_ => "",
}
}

Expand Down
Loading

0 comments on commit dce4938

Please sign in to comment.