Browse Source

Make WordpressTranslation support different locales and versions

master
Adrian Heine 6 years ago
parent
commit
bf74ebb7db
  1. 44
      src/symbols/wordpress/translation.rs

44
src/symbols/wordpress/translation.rs

@ -5,55 +5,49 @@ use std::fmt;
use std::fs::File as FsFile; use std::fs::File as FsFile;
use std::io; use std::io;
use std::io::Read; use std::io::Read;
use std::ops::Deref;
use std::path::Path; use std::path::Path;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
use resources::Resource; use resources::Resource;
pub struct WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: 'a + CommandRunner {
pub struct WordpressTranslation<'a, C, D, R> where C: AsRef<str> + fmt::Display, D: AsRef<str> + fmt::Display, R: 'a + CommandRunner {
path: D, path: D,
language: C,
version: &'a str,
locale: C,
command_runner: &'a R command_runner: &'a R
} }
impl<'a, C, R> WordpressTranslation<'a, C, String, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
pub fn new<D: AsRef<str> + fmt::Display>(path: D, language: C, command_runner: &'a R) -> Self {
impl<'a, C, R> WordpressTranslation<'a, C, String, R> where C: AsRef<str> + fmt::Display, R: CommandRunner {
pub fn new<D: AsRef<str> + fmt::Display>(path: D, version: &'a str, locale: C, command_runner: &'a R) -> Self {
WordpressTranslation { WordpressTranslation {
path: Path::new(path.as_ref()).join("wp-content/languages").to_string_lossy().to_string(), path: Path::new(path.as_ref()).join("wp-content/languages").to_string_lossy().to_string(),
language: language,
version: version,
locale: locale,
command_runner: command_runner command_runner: command_runner
} }
} }
} }
/*
https://translate.wordpress.org/projects/wp/4.7.x/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/de_DE.po
https://translate.wordpress.org/projects/wp/4.7.x/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/de_DE.mo
https://translate.wordpress.org/projects/wp/4.7.x/cc/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/continents-cities-de_DE.po
https://translate.wordpress.org/projects/wp/4.7.x/cc/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/continents-cities-de_DE.mo
https://translate.wordpress.org/projects/wp/4.7.x/admin/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-de_DE.po
https://translate.wordpress.org/projects/wp/4.7.x/admin/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-de_DE.mo
https://translate.wordpress.org/projects/wp/4.7.x/admin/network/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-network-de_DE.po
https://translate.wordpress.org/projects/wp/4.7.x/admin/network/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-network-de_DE.mo
*/
impl<'a, C, D, R> WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
impl<'a, C, D, R> WordpressTranslation<'a, C, D, R> where C: AsRef<str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
fn get_pairs(&self) -> Vec<(String, String)> { fn get_pairs(&self) -> Vec<(String, String)> {
let version_x = self.version.trim_right_matches(|c: char| c.is_digit(10)).to_owned() + "x";
let locale: &str = self.locale.as_ref();
let path_locale = if locale == "de_DE" { "de".to_owned() } else { locale.to_lowercase().replace('_', "-") };
let mut res = vec![]; let mut res = vec![];
for &(in_slug, out_slug) in [("", ""), ("cc/", "continents-cities-"), ("admin/", "admin-"), ("admin/network/", "admin-network-")].into_iter() { for &(in_slug, out_slug) in [("", ""), ("cc/", "continents-cities-"), ("admin/", "admin-"), ("admin/network/", "admin-network-")].into_iter() {
for format in ["po", "mo"].into_iter() { for format in ["po", "mo"].into_iter() {
res.push((format!("https://translate.wordpress.org/projects/wp/4.7.x/{}{}/default/export-translations?format={}", in_slug, self.language, format), format!("{}/{}de_DE.{}", self.path, out_slug, format)))
res.push((
format!("https://translate.wordpress.org/projects/wp/{}/{}{}/default/export-translations?format={}", version_x, in_slug, path_locale, format),
format!("{}/{}{}.{}", self.path, out_slug, self.locale, format)
))
} }
} }
res res
} }
} }
impl<'a, C, D, R> Symbol for WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
impl<'a, C, D, R> Symbol for WordpressTranslation<'a, C, D, R> where C: AsRef<str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
fn target_reached(&self) -> Result<bool, Box<Error>> { fn target_reached(&self) -> Result<bool, Box<Error>> {
let mut newest = String::new(); let mut newest = String::new();
let match_date = Regex::new("(?m)^\"PO-Revision-Date: (.+)\\+0000\\\\n\"$").unwrap(); let match_date = Regex::new("(?m)^\"PO-Revision-Date: (.+)\\+0000\\\\n\"$").unwrap();
@ -74,8 +68,8 @@ impl<'a, C, D, R> Symbol for WordpressTranslation<'a, C, D, R> where C: Deref<Ta
newest = max(newest, file_date.to_string()); newest = max(newest, file_date.to_string());
} }
} }
let upstream = try!(self.command_runner.get_output("curl", &["https://api.wordpress.org/core/version-check/1.7/?version=4.7.10&locale=de_DE"]));
Ok(try!(String::from_utf8(upstream)).contains(&format!(r###"language":"de_DE","version":"4.7.10","updated":"{}"###, newest)))
let upstream = try!(self.command_runner.get_output("curl", &[&format!("https://api.wordpress.org/core/version-check/1.7/?version={}&locale={}", self.version, self.locale)]));
Ok(try!(String::from_utf8(upstream)).contains(&format!(r###"language":"{}","version":"{}","updated":"{}"###, self.locale, self.version, newest)))
} }
fn execute(&self) -> Result<(), Box<Error>> { fn execute(&self) -> Result<(), Box<Error>> {
@ -98,7 +92,7 @@ impl<'a, C, D, R> Symbol for WordpressTranslation<'a, C, D, R> where C: Deref<Ta
} }
} }
impl<'a, C, D, R> fmt::Display for WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
impl<'a, C, D, R> fmt::Display for WordpressTranslation<'a, C, D, R> where C: AsRef<str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
write!(f, "WordpressTranslation {}", self.path) write!(f, "WordpressTranslation {}", self.path)
} }

Loading…
Cancel
Save