Skip to content

Commit

Permalink
Internationalize conferences dates (fixes #116)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwrobel committed Jan 5, 2023
1 parent 09818c9 commit fffdd5c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- Internationalize conferences dates (#116).

### Deprecated

### Removed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lescastcodeurs.bot;

import java.util.Locale;

/** An object that can be converted to markdown. */
public interface MarkdownSerializable {

Expand All @@ -8,5 +10,5 @@ public interface MarkdownSerializable {
*
* @return a non-null {@link String}
*/
String markdown();
String markdown(Locale locale);
}
10 changes: 6 additions & 4 deletions src/main/java/com/lescastcodeurs/bot/conferences/Conference.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

@SuppressWarnings("java:S6218") // don't care
public record Conference(String name, String hyperlink, String location, long[] date, String misc)
Expand Down Expand Up @@ -54,17 +55,18 @@ public boolean matchesAnyOf(List<String> criteria) {
}

@Override
public String markdown() {
public String markdown(Locale locale) {
LocalDate start = timestampToDate(date[0]);
LocalDate end = timestampToDate(date[1]);
DateTimeFormatter formatter = FORMAT.withLocale(locale);

String date;
if (start.equals(end)) {
date = end.format(FORMAT);
date = end.format(formatter);
} else if (start.getMonth() == end.getMonth()) {
date = start.getDayOfMonth() + "-" + end.format(FORMAT);
date = start.getDayOfMonth() + "-" + end.format(formatter);
} else {
date = start.format(FORMAT) + "-" + end.format(FORMAT);
date = start.format(formatter) + "-" + end.format(formatter);
}

return "- %s : [%s](%s) - %s %s%n".formatted(date, name, hyperlink, location, misc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.lescastcodeurs.bot.MarkdownSerializable;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,13 +26,13 @@ public record Conferences(String json, List<String> selectionCriteria)
}

@Override
public String markdown() {
public String markdown(Locale locale) {
LocalDate now = LocalDate.now();

try {
return MAPPER.readValue(json, new TypeReference<List<Conference>>() {}).stream()
.filter(c -> c.isValidCandidate(selectionCriteria, now))
.map(Conference::markdown)
.map(c -> c.markdown(locale))
.collect(Collectors.joining());
} catch (JsonProcessingException e) {
LOG.warn("An error occurred during JSON processing", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.lescastcodeurs.bot.conferences;

import com.lescastcodeurs.bot.MarkdownSerializable;
import java.util.Locale;

public record NoConferenceMarkdown() implements MarkdownSerializable {

public static final String MESSAGE =
"La liste des conférences n'a pas pu être récupérée. Pour plus d'informations voir les logs du bot.";

@Override
public String markdown() {
public String markdown(Locale locale) {
return MESSAGE;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/show-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Téléchargement de l’épisode [LesCastCodeurs-Episode-{episodeNumber}.mp3](ht
La liste des conférences provenant de [Developers Conferences Agenda/List](https://github.com/scraly/developers-conferences-agenda)
par [Aurélie Vache](https://github.com/scraly) et contributeurs :

{conferences.markdown}
{conferences.markdown(locale)}

## Nous contacter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lescastcodeurs.bot.conferences;

import static java.util.Locale.FRANCE;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -24,7 +25,7 @@ void criteriaCannotBeNull() {
void withMalformedJson() {
Conferences confs = new Conferences("a malformed json", EMPTY);

assertTrue(confs.markdown().contains(NoConferenceMarkdown.MESSAGE));
assertTrue(confs.markdown(FRANCE).contains(NoConferenceMarkdown.MESSAGE));
}

@Test
Expand Down Expand Up @@ -67,7 +68,7 @@ void withCorrectJson() {
""",
List.of("(France)", "Devoxx"));

String markdown = confs.markdown();
String markdown = confs.markdown(FRANCE);
assertTrue(markdown.contains("Devoxx"));
}
}

0 comments on commit fffdd5c

Please sign in to comment.