简体   繁体   English

Spring 引导内部化无法按请求工作

[英]Spring boot internalization does not work per request

I know this question might have an answer but am a newbie to spring boot.我知道这个问题可能有答案,但我是 spring 引导的新手。 I need to do internalization to my rest endpoints and I followed this blog to implement internalization but the problem is one.我需要对我的 rest 端点进行内部化,我按照这个博客来实现内部化,但问题是一个。 The language does not change per request it only change only after application launch that is suppose at launch in my post man the language was fr that will work but after I change fr to pt (Portuguese) it does not pick pt it still remains with fr.语言不会因请求而改变,它仅在应用程序启动后才会改变. Here is my code that am working with.这是我正在使用的代码。 I have created 4 different messages.properties that is messages_fr.properties, messages_pt.properties, messages_sw.properties under resources dir我在资源目录下创建了 4 个不同的 messages.properties,即 messages_fr.properties、messages_pt.properties、messages_sw.properties

@Configuration
public class MyCustomLocaleResolver extends AcceptHeaderLocaleResolver
        implements WebMvcConfigurer {
    List<Locale> LOCALES = Arrays.asList(
            new Locale("en"),
            new Locale("pt"),
            new Locale("sw"),
            new Locale("fr"));

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String headerLang = request.getHeader("Accept-Language");
        System.out.println("header:"+headerLang);
        return headerLang == null || headerLang.isEmpty()
                ? Locale.getDefault()
                : Locale.lookup(Locale.LanguageRange.parse(headerLang), LOCALES);
    }

    @Bean
    public MessageSource messageSource() {
        final ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
        rs.setDefaultEncoding(StandardCharsets.ISO_8859_1.name());
        rs.setBasename("messages");

        rs.setUseCodeAsDefaultMessage(true);
        return rs;
    }

}

And Translator class和翻译器class

@Component
public class LanguageTranslator {

    private static ResourceBundleMessageSource messageSource;

    @Autowired
    LanguageTranslator(ResourceBundleMessageSource messageSource) {
        LanguageTranslator.messageSource = messageSource;
    }

    public static String translate(String msg) {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(msg, null, locale);
    }
}

And here is my postman request这是我的 postman 请求在此处输入图像描述 and here is messages_fr.properties这是messages_fr.properties

在此处输入图像描述 and project structure和项目结构在此处输入图像描述

And here is my controller code这是我的 controller 代码

@RestController
@RequestMapping("/home")
public class HomeContoller {

    @Autowired
    private SystemSettingService settingService;



    @GetMapping("/")
    public String home() {

        return "Home page";
    }

    @RequestMapping(value = "/demo", method = RequestMethod.POST)
    public ResponseEntity all_menu_assignment(HttpServletRequest req)  {
   

        return  ResponseEntity
                .ok().body(new ServerResponse(Common.SUCCESS_CODE,Common.SUCCESS_MESSAGE));

    }
}

and here is my Common class这是我的常见 class

public class Common{
  public static String SUCCESS_MESSAGE= LanguageTranslator.translate("SUCCESS_MESSAGE");
}

The LocaleContextHolder holds the locale of the current thread - so referencing this in a static variable will result in the default VM local to be used (because the constant will be initialized during startup). LocaleContextHolder保存当前线程的语言环境——因此在 static 变量中引用它将导致使用默认的本地 VM(因为该常量将在启动期间初始化)。

Something like this should work:像这样的东西应该工作:

@RestController
@RequestMapping("/home")
public class HomeContoller {

    @Autowired
    private SystemSettingService settingService;

    @GetMapping("/")
    public String home() {

        return "Home page";
    }

    @RequestMapping(value = "/demo", method = RequestMethod.POST)
    public ResponseEntity all_menu_assignment(HttpServletRequest req)  {
   

        return  ResponseEntity
                .ok().body(new ServerResponse(Common.SUCCESS_CODE, LanguageTranslator.translate("SUCCESS_MESSAGE")));

    }
}

Also, it would be better make the translate method in LanguageTranslator non-static and to inject (autowire) the object in the controller.此外,最好将LanguageTranslator中的translate方法设为非静态,并在 controller 中注入(自动装配)object。

Translator.toLocale(Common.SUCCESS_MESSAGE) is missing in your controller api method Translator.toLocale(Common.SUCCESS_MESSAGE) 在您的 controller api 方法中丢失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM