Skip to content

Commit

Permalink
Translate guides
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Dec 7, 2023
1 parent 25dd5b9 commit f4951a5
Show file tree
Hide file tree
Showing 15 changed files with 607 additions and 102 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>jsoup</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>org.fedorahosted.tennera</groupId>
<artifactId>jgettext</artifactId>
<version>0.15.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -132,6 +137,10 @@
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
</dependency>
<dependency>
<groupId>org.fedorahosted.tennera</groupId>
<artifactId>jgettext</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/io/quarkus/search/app/entity/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
import java.util.Set;

public enum Language {
ENGLISH("en"),
SPANISH("es"),
PORTUGUESE("pt"),
CHINESE("cn"),
JAPANESE("ja");
ENGLISH("en", "en_US"),
SPANISH("es", "es_ES"),
PORTUGUESE("pt", "pt_BR"),
CHINESE("cn", "zh_CN"),
JAPANESE("ja", "ja_JP");

public static final Set<Language> nonDefault = EnumSet.of(Language.SPANISH, Language.PORTUGUESE, Language.CHINESE,
Language.JAPANESE);

public final String code;

Language(String code) {
public final String locale;

Language(String code, String locale) {
this.code = code;
this.locale = locale;
}

@SuppressWarnings("unused")
Expand Down
78 changes: 50 additions & 28 deletions src/main/java/io/quarkus/search/app/fetching/FetchingService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package io.quarkus.search.app.fetching;

import static io.quarkus.search.app.quarkusio.QuarkusIO.PAGES_BRANCH;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.quarkus.search.app.entity.Language;
import io.quarkus.search.app.quarkusio.QuarkusIO;
import io.quarkus.search.app.quarkusio.QuarkusIOConfig;
import io.quarkus.search.app.util.GitCloneDirectory;
import io.quarkus.search.app.util.GitUtils;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

Expand All @@ -29,61 +38,74 @@ public class FetchingService {
QuarkusIOConfig quarkusIOConfig;

public QuarkusIO fetchQuarkusIo() {
return fetch("quarkus.io", quarkusIOConfig.gitUri(),
return fetch(quarkusIOConfig.gitUri(),
List.of(QuarkusIO.SOURCE_BRANCH, QuarkusIO.PAGES_BRANCH),
(directory, git) -> new QuarkusIO(quarkusIOConfig, directory, git));
sortMap(quarkusIOConfig.localized()),
List.of(QuarkusIO.LOCALIZED_SOURCE_BRANCH, QuarkusIO.LOCALIZED_PAGES_BRANCH),
(directory, localizedSites) -> new QuarkusIO(quarkusIOConfig, directory, localizedSites));
}

private Map<Language, QuarkusIOConfig.SiteConfig> sortMap(Map<String, QuarkusIOConfig.SiteConfig> localized) {
Map<Language, QuarkusIOConfig.SiteConfig> map = new LinkedHashMap<>();
for (String lang : localized.keySet().stream().sorted().toList()) {
map.put(Language.fromString(lang), localized.get(lang));
}
return map;
}

private <T> T fetch(String name, URI uri, List<String> branches,
IOBiFunction<CloseableDirectory, Git, T> function) {
private <T> T fetch(URI uri, List<String> branches,
Map<Language, QuarkusIOConfig.SiteConfig> localized, List<String> localizedBranches,
IOBiFunction<GitCloneDirectory, Map<Language, GitCloneDirectory>, T> function) {
try {
if (LaunchMode.DEVELOPMENT.equals(LaunchMode.current())
&& uri.getScheme().equals("file")
&& uri.getPath().endsWith(".zip")) {
var zipPath = Path.of(uri);
Log.warn(
"Unzipping '%s': this application is most likely indexing only a sample of quarkus.io. See README to index the full website.");
try (CloseableDirectory unzipped = CloseableDirectory.temp(name + "-unzipped")) {
try (CloseableDirectory unzipped = CloseableDirectory.temp("quarkus.io-unzipped")) {
FileUtils.unzip(zipPath, unzipped.path());
uri = unzipped.path().toUri();
// While technically unnecessary (we could use the unzipped directory directly),
// this cloning ensures we run the same code in dev mode as in prod.
return gitClone(name, uri, branches, function);
return gitClone(uri, branches, Map.of(), List.of(), function);
} catch (RuntimeException | IOException e) {
throw new IllegalStateException("Failed to unzip '" + uri + "': " + e.getMessage(), e);
}
} else {
return gitClone(name, uri, branches, function);
return gitClone(uri, branches, localized, localizedBranches, function);
}
} catch (RuntimeException e) {
throw new IllegalStateException("Failed to fetch '" + name + "': " + e.getMessage(), e);
throw new IllegalStateException("Failed to fetch: " + e.getMessage(), e);
}
}

private <T> T gitClone(String name, URI gitUri, List<String> branches,
IOBiFunction<CloseableDirectory, Git, T> function) {
Log.infof("Fetching %s from %s.", name, gitUri);
CloseableDirectory directory = null;
Git git = null;
private <T> T gitClone(URI gitUri, List<String> branches,
Map<Language, QuarkusIOConfig.SiteConfig> localized, List<String> localizedBranches,
IOBiFunction<GitCloneDirectory, Map<Language, GitCloneDirectory>, T> function) {
List<GitCloneDirectory> directories = new ArrayList<>();
try {
directory = CloseableDirectory.temp(name);
git = Git.cloneRepository()
.setURI(gitUri.toString())
.setDirectory(directory.path().toFile())
.setDepth(1)
.setNoTags()
.setBranch(branches.get(0))
.setBranchesToClone(branches.stream().map(b -> "refs/heads/" + b).toList())
.setProgressMonitor(new TextProgressMonitor())
// Unfortunately sparse checkouts are not supported: https://www.eclipse.org/forums/index.php/t/1094825/
.call();
return function.apply(directory, git);
GitCloneDirectory mainRepository = GitCloneDirectory.mainRepository(gitUri, branches);
directories.add(mainRepository);

Map<Language, GitCloneDirectory> localizedSites = new LinkedHashMap<>();
for (Map.Entry<Language, QuarkusIOConfig.SiteConfig> entry : localized.entrySet()) {
URI localizedUri = entry.getValue().gitUri();
GitCloneDirectory localizedRepository = GitCloneDirectory.localizedRepository(entry.getKey(), localizedUri,
localizedBranches);
directories.add(localizedRepository);
localizedSites.put(entry.getKey(), localizedRepository);
}

return function.apply(mainRepository, localizedSites);
} catch (RuntimeException | IOException | GitAPIException e) {
new SuppressingCloser(e)
.push(git)
.push(directory);
.pushAll(directories);
throw new IllegalStateException(
"Failed to clone git repository '" + gitUri + "': " + e.getMessage(), e);
"Failed to clone git repository '%s'/%s: %s".formatted(
gitUri, localized.values().stream().map(QuarkusIOConfig.SiteConfig::gitUri).toList(),
e.getMessage()),
e);
}
}
}
Loading

0 comments on commit f4951a5

Please sign in to comment.