Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 1cd68c76 authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Pull request #114: [URGENT] Fix locale if folder not exist on file path

Merge in EDELIVERY/smp from bugfix/EDELIVERY-13818-locale-not-found to development

* commit 'c0956e6e':
  [URGENT] Fix locale if folder not exist on file path
parents 00db8155 c0956e6e
No related branches found
No related tags found
No related merge requests found
Checking pipeline status
package eu.europa.ec.edelivery.smp.ui.external;
import eu.europa.ec.edelivery.smp.i18n.SMPLocale;
import eu.europa.ec.edelivery.smp.logging.SMPLogger;
import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
import eu.europa.ec.edelivery.smp.services.SMPLocaleService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;
import java.nio.file.Path;
import static eu.europa.ec.edelivery.smp.ui.ResourceConstants.CONTEXT_PATH_PUBLIC_LOCALE;
/**
* Provides support for returning locale files required by the ngx-translation Angular library.
*
* @since 5.1
* @author Sebastian-Ion TINCU
* @since 5.1
*/
@RestController
@RequestMapping(value = CONTEXT_PATH_PUBLIC_LOCALE)
public class LocaleController {
private static final SMPLogger LOG = SMPLoggerFactory.getLogger(LocaleController.class);
private static final String DEFAULT_LOCALE_RESOURCE = "/META-INF/resources/ui/assets/i18n/en.json";
private final SMPLocaleService smpLocaleService;
public LocaleController(SMPLocaleService smpLocaleService) {
......@@ -26,7 +33,21 @@ public class LocaleController {
@GetMapping(value = "/{code}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
@ResponseBody
public FileSystemResource getLocale(@PathVariable("code") String code) {
return new FileSystemResource(smpLocaleService.getLocaleFile(SMPLocale.fromCodeDefaultingToEnglish(code)));
public Resource getLocale(@PathVariable("code") String code) {
Path langResourcePath = smpLocaleService.getLocaleFile(SMPLocale.fromCodeDefaultingToEnglish(code));
if (langResourcePath.toFile().exists()) {
LOG.debug("Returning locale file [{}]", langResourcePath.toAbsolutePath());
return new FileSystemResource(langResourcePath);
} else {
LOG.warn("Locale file [{}] does not exist. Return default translation!", langResourcePath.toAbsolutePath());
ClassPathResource defResource = new ClassPathResource(DEFAULT_LOCALE_RESOURCE);
if (defResource.exists()) {
return defResource;
} else {
LOG.error("Default locale file [{}] does not exist in classpath!", DEFAULT_LOCALE_RESOURCE);
return null;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment