mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
17.59.2021 c6e77f6fa462e292db5f693a33e7c483b5a6e19e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package de.micromata.borgbutler.server.rest
 
import de.micromata.borgbutler.json.JsonUtils
import de.micromata.borgbutler.server.I18nClientMessages
import org.apache.commons.lang3.StringUtils
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.*
import javax.servlet.http.HttpServletRequest
 
@RestController
@RequestMapping("/rest/i18n")
class I18nRest {
 
    /**
     *
     * @param request For detecting the user's client locale.
     * @param locale If not given, the client's language (browser) will be used.
     * @param keysOnly If true, only the keys will be returned. Default is false.
     * @param prettyPrinter If true then the json output will be in pretty format.
     * @see JsonUtils.toJson
     */
    @GetMapping("list")
    fun getList(
        request: HttpServletRequest,
        @RequestParam("prettyPrinter", required = false) prettyPrinter: Boolean?,
        @RequestParam("keysOnly", required = false) keysOnly: Boolean?,
        @RequestParam("locale", required = false) locale: String?
    ): String {
        val localeObject: Locale?
        if (StringUtils.isNotBlank(locale)) {
            localeObject = Locale(locale)
        } else {
            localeObject = RestUtils.getUserLocale(request)
        }
        val translations: Map<String, String> =
            I18nClientMessages.getInstance().getAllMessages(localeObject, keysOnly == true)
        return JsonUtils.toJson(translations, prettyPrinter)
    }
}