Giter Site home page Giter Site logo

weblate-spring's Introduction

Weblate Spring

Provides a Spring MessageSource backed by a Weblate Server.

Usage

Add the Maven dependency to your project (find the current version on Maven Central):

<dependency>
  <groupId>at.porscheinformatik.weblate</groupId>
  <artifactId>weblate-spring</artifactId>
  <version>${weblate-spring.version}</version>
</dependency>

Then declare the WeblateMessageSource as a bean. You can configure the MessageSource via:

  • baseUrl (required) - the base URL of your Weblate instance
  • project (required) - the project slug
  • component (required) - the component slug

Usually you might want to have the local message bundles as a backup when Weblate is not running. Therefore, you can set a ResourceBundleMessageSource as the parent of the WeblateMessageSource.

WeblateMessageSource implements an interface AllPropertiesSource which can be used to obtain all translations from the message source. This is needed when you want to get all your translations to your client (for example in a Single Page App). The AllPropertiesReloadableResourceBundleMessageSource should also be used in this context.

Here is a full example bean configuration for a Spring Boot app (with "messages" as the default bundle):

@Bean
@Primary
public WeblateMessageSource messageSource() {
  var weblateMessageSource = new WeblateMessageSource();
  weblateMessageSource.setBaseUrl("https://hosted.weblate.org");
  weblateMessageSource.setProject("my-project");
  weblateMessageSource.setComponent("my-component");
  weblateMessageSource.useAuthentication("api-key");
  weblateMessageSource.setParentMessageSource(localMessageSource());
  return weblateMessageSource;
}

@Bean
MessageSource localMessageSource() {
  var localMessageSource = new AllPropertiesReloadableResourceBundleMessageSource();
  localMessageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
  localMessageSource.setBasename("messages");
  localMessageSource.setFallbackToSystemLocale(false);
  return localMessageSource;
}

Authentication / Adapting RestTemplate

For the simple authentication via API key you can use WeblateMessageSource#useAuthentication. If you want further customization of the HTTP calls you can set your own RestTemplate.

Caching

WeblateMessageSource caches loaded translations 30 minutes (this can be changed by setting maxAgeMilis). If you want to update one locale immediately call WeblateMessageSource#reload(Locale).

If you want to fully clear the cache call WeblateMessageSource#clearCache - this will also reload the languages. Please use this with caution as it add heavy load to the Weblate API!

weblate-spring's People

Contributors

derkoe avatar renovate[bot] avatar lproemer avatar renovate-bot avatar robert-hk avatar stefantt avatar poitaf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.