Vadik Sirekanyan 4 роки тому
батько
коміт
e4513d38d8

+ 7 - 4
src/main/kotlin/com/sirekanyan/andersrobot/AndersRobot.kt

@@ -14,6 +14,7 @@ import org.telegram.telegrambots.meta.api.objects.Update
 import org.telegram.telegrambots.meta.generics.LongPollingBot
 import org.telegram.telegrambots.util.WebhookUtils
 import java.io.File
+import java.util.*
 
 private const val DEFAULT_CITY_ID = 524901L // Moscow
 
@@ -41,7 +42,9 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
     private fun onUpdate(update: Update) {
         val message = update.message
         val chatId = message.chatId
-        val language = message.from?.languageCode?.takeIf { it in supportedLanguages }
+        val languageCode = message.from?.languageCode
+        val language = languageCode?.takeIf { it in supportedLanguages }
+        val locale = Locale.forLanguageTag(languageCode.orEmpty())
         println("${message.from?.id} (chat $chatId) => ${message.text}")
         val cityCommand = getCityCommand(message.text)
         val forecastCommand = getForecastCommand(message.text)
@@ -69,7 +72,7 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
                 if (forecast == null) {
                     sendText(chatId, "Не знаю такого города")
                 } else {
-                    showForecast(chatId, forecast)
+                    showForecast(chatId, forecast, locale)
                 }
             }
             !addCityCommand.isNullOrEmpty() -> {
@@ -106,9 +109,9 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
         sendText(chatId, temperatures.joinToString("\n") { it.format() })
     }
 
-    private fun showForecast(chatId: Long, forecast: Forecast) {
+    private fun showForecast(chatId: Long, forecast: Forecast, locale: Locale) {
         val city = forecast.city.name
-        ggsave(plotForecast(forecast), "$city.png")
+        ggsave(plotForecast(forecast, locale), "$city.png")
         sendPhoto(chatId, File("lets-plot-images/$city.png"))
     }
 

+ 7 - 6
src/main/kotlin/com/sirekanyan/andersrobot/LetsPlot.kt

@@ -16,18 +16,19 @@ import java.time.LocalDateTime
 import java.time.LocalTime
 import java.time.ZoneOffset
 import java.time.format.DateTimeFormatter
+import java.util.*
 
-fun plotForecast(forecast: Forecast): Plot {
+fun plotForecast(forecast: Forecast, locale: Locale): Plot {
     val offset = ZoneOffset.ofTotalSeconds(forecast.city.timezone)
     val values = forecast.list.take(33)
     val xValues = values.map { it.dt }
     val yValues = values.map { it.main.temp }
     val xBreaks = xBreaks(xValues, offset)
     val yBreaks = yBreaks(yValues)
-    val xLabels = xBreaks.map { it.toLocalDateTime(offset).formatDateTime("d MMM") }
-    val current = (currentTimeMillis() / 1000).toLocalDateTime(offset).formatDateTime("d MMMM, HH:mm")
+    val xLabels = xBreaks.map { it.toLocalDateTime(offset).formatDateTime("d MMM", locale) }
+    val current = (currentTimeMillis() / 1000).toLocalDateTime(offset).formatDateTime("d MMMM, HH:mm", locale)
     return letsPlot(mapOf("x" to xValues, "y" to yValues)) { x = "x"; y = "y" } +
-            ggsize(400, 250) +
+            ggsize(300, 200) +
             ggtitle("${forecast.city.name}, $current") +
             geomSmooth(method = "loess", se = false, span = 2.0 / values.size, color = Color.BLUE) +
             scaleXContinuous("", breaks = xBreaks, labels = xLabels) +
@@ -63,5 +64,5 @@ private fun yBreaks(temps: List<Double>): List<Int> {
 private fun Long.toLocalDateTime(offset: ZoneOffset) =
     LocalDateTime.ofEpochSecond(this, 0, offset)
 
-private fun LocalDateTime.formatDateTime(pattern: String) =
-    format(DateTimeFormatter.ofPattern(pattern))
+private fun LocalDateTime.formatDateTime(pattern: String, locale: Locale) =
+    format(DateTimeFormatter.ofPattern(pattern, locale))