Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Babel
https://github.com/taig/babel
Admin
Internationalization (i18n) for Scala applications
Tokens:
7,365
Snippets:
21
Trust Score:
9.3
License:
MIT
Update:
1 year ago
Context
Chat
Benchmark
0
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Default Babel Setup Configuration (Scala) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/installation.md This block defines the default setup configuration for the Babel project, combining the Circe serialization, generic codecs, and HOCON loader dependencies. This setup is assumed in the project's documentation. ```scala "@ORGANIZATION@" %%% "@ARTIFACT@-circe" % "@VERSION@" "@ORGANIZATION@" %%% "@ARTIFACT@-generic" % "@VERSION@" "@ORGANIZATION@" %%% "@ARTIFACT@-loader" % "@VERSION@" ``` -------------------------------- ### Add Babel HOCON Loader Dependency (Scala) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/installation.md This snippet adds the dependency for reading HOCON translation definitions from resources within the Babel project. It facilitates loading configuration and translations. ```scala "@ORGANIZATION@" %%% "@ARTIFACT@-loader" % "@VERSION@" ``` -------------------------------- ### Add Babel Core Dependency (Scala) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/installation.md This snippet adds the dependency for the Babel project's core module, which contains all data class definitions, type classes, and pre-defined locales. It is dependency-free. ```scala "@ORGANIZATION@" %%% "@ARTIFACT@-core" % "@VERSION@" ``` -------------------------------- ### Add Babel Circe Serialization Format (Scala) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/installation.md This snippet adds the dependency for Circe serialization formats for the Babel project. It enables the use of Circe for handling serialization and deserialization of Babel's data classes. ```scala "@ORGANIZATION@" %%% "@ARTIFACT@-circe" % "@VERSION@" ``` -------------------------------- ### Add Babel Cats Type Class Instances (Scala) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/installation.md This snippet adds the dependency for Cats type class instances for the Babel project. This is required for integrating Babel's data structures with the Cats ecosystem. ```scala "@ORGANIZATION@" %%% "@ARTIFACT@-cats" % "@VERSION@" ``` -------------------------------- ### Add Babel Generic Codecs Dependency (Scala) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/installation.md This snippet adds the dependency for codecs used to populate custom data classes from translation objects in the Babel project. It provides generic mapping capabilities. ```scala "@ORGANIZATION@" %%% "@ARTIFACT@-generic" % "@VERSION@" ``` -------------------------------- ### Babel: Plural Ranges for Complex Rules Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-plurals.md Explains how to handle complex plural rules in different languages by mapping quantity ranges. This example demonstrates loading and using translations for various numerical ranges. ```scala import cats.effect._ import cats.effect.unsafe.implicits.global import cats.syntax.all._ import io.taig.babel._ import io.taig.babel.generic.auto._ final case class I18n(ranges: Quantities[String]) val i18n = Loader .default[IO] .load("plurals-ranges", Set(Locales.en)) .map(Decoder[I18n].decodeAll) .rethrow .map(_.withFallback(Locales.en)) .flatMap(_.liftTo[IO](new IllegalStateException("Translations for en missing"))) .map(_.apply(Locales.en)) .unsafeRunSync() i18n.ranges(0) i18n.ranges(1) i18n.ranges(9) i18n.ranges(10) i18n.ranges(11) i18n.ranges(20) i18n.ranges(21) ``` -------------------------------- ### Accessing Localized ADT Values (English) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/cookbook.md This code snippet demonstrates how to retrieve a localized string value for an ADT instance using the loaded translations. It accesses the `i18ns` object, specifies the English locale, and then uses the `country` helper method to get the translation for `Country.Italy`. ```scala i18ns(Locales.en).country(Country.Italy) ``` -------------------------------- ### Accessing Localized ADT Values (German) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/cookbook.md This code snippet demonstrates how to retrieve a localized string value for an ADT instance using the loaded translations. It accesses the `i18ns` object, specifies the German locale, and then uses the `country` helper method to get the translation for `Country.France`. ```scala i18ns(Locales.de).country(Country.France) ``` -------------------------------- ### Run Sample Backend Source: https://github.com/taig/babel/blob/main/modules/documentation/src/sample.md Command to run the sample backend application using sbt. This command executes the sampleBackend module's run task. ```bash > sbt sampleBackend/run ``` -------------------------------- ### Load Translations into Babel Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-loading.md Demonstrates loading translation files into a Babel instance using the `Loader.default` method. It specifies the directory for translation files and the set of locales to load. The `unsafeRunSync()` method is used to execute the IO effect. ```Scala import cats.effect._ import cats.effect.unsafe.implicits.global import io.taig.babel._ val babels = Loader.default[IO] .load("babel", Set(Locales.en, Locales.de, Locales.de_AT)) .unsafeRunSync() ``` -------------------------------- ### Babel: Basic Plurals with Quantities Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-plurals.md Demonstrates the basic usage of Babel's `Quantities` class for handling plural translations. It shows how to load translations and access pluralized strings based on a given count. ```scala import cats.effect._ import cats.effect.unsafe.implicits.global import cats.syntax.all._ import io.taig.babel._ import io.taig.babel.generic.auto._ final case class I18n(bicycles: Quantities[String]) val i18n = Loader .default[IO] .load("plurals", Set(Locales.en)) .map(Decoder[I18n].decodeAll) .rethrow .map(_.withFallback(Locales.en)) .flatMap(_.liftTo[IO](new IllegalStateException("Translations for en missing"))) .map(_.apply(Locales.en)) .unsafeRunSync() i18n.bicycles(0) i18n.bicycles(1) i18n.bicycles(100) ``` -------------------------------- ### ADT Helper Methods and Localization Source: https://github.com/taig/babel/blob/main/modules/documentation/src/cookbook.md This section demonstrates how to add helper methods to Abstract Data Types (ADTs) for easier use, particularly for lookups. It shows how to define a sealed trait for countries, case objects for specific countries, and a case class to hold localized string values for these countries, enabling type-safe access to translations. ```scala import cats.effect._ import cats.effect.unsafe.implicits.global import cats.syntax.all._ import io.taig.babel._ import io.taig.babel.generic.auto._ sealed abstract class Country extends Product with Serializable object Country { case object France extends Country case object Italy extends Country } final case class CountryI18n(france: String, italy: String) { def apply(country: Country): String = country match { case Country.France => france case Country.Italy => italy } } final case class I18n(country: CountryI18n) val i18ns = Loader .default[IO] .load("cookbook-country", Set(Locales.en, Locales.de)) .map(Decoder[I18n].decodeAll) .rethrow .map(_.withFallback(Locales.en)) .flatMap(_.liftTo[IO](new IllegalStateException("Translations for en missing"))) .unsafeRunSync() ``` -------------------------------- ### Decode and Apply StringFormat with Babel Scala Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-arguments.md Demonstrates manually decoding a raw string containing placeholders into a StringFormat instance and then applying the required arguments to generate the final string. ```Scala Decoder[StringFormat2] .decode(Babel.text("It's always rainy in {0}")) .map(_.apply("London", "3")) ``` -------------------------------- ### Decode Babel Translations into Data Class Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-loading.md Shows how to decode the loaded Babel translations into a Scala case class using generic derivation. It defines an `I18n` case class and its `Decoder`, then decodes all loaded translations, applying a fallback to English and handling potential missing keys. ```Scala import cats.syntax.all._ import io.taig.babel.generic.auto._ final case class I18n(greeting: String) object I18n { implicit val decoder: Decoder[I18n] = deriveDecoder[I18n] } val i18ns = IO.fromEither(Decoder[I18n].decodeAll(babels)) .map(_.withFallback(Locales.en)) .flatMap(_.liftTo[IO](new IllegalStateException("Translations for en missing"))) .unsafeRunSync() ``` -------------------------------- ### Babel: Plurals with Arguments using StringFormat Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-plurals.md Illustrates how to use plurals with dynamic arguments by combining `Quantities` with `StringFormat`. This allows referencing the quantity value within the translated string. ```scala import cats.effect._ import cats.effect.unsafe.implicits.global import cats.syntax.all._ import io.taig.babel._ import io.taig.babel.generic.auto._ final case class I18n(bicycles: Quantities[StringFormat1]) val i18n = Loader .default[IO] .load("plurals-arguments", Set(Locales.en)) .map(Decoder[I18n].decodeAll) .rethrow .map(_.withFallback(Locales.en)) .flatMap(_.liftTo[IO](new IllegalStateException("Translations for en missing"))) .map(_.apply(Locales.en)) .unsafeRunSync() i18n.bicycles(0) i18n.bicycles(100)("100") i18n.bicycles(9000000)("9 million") ``` -------------------------------- ### Http4s Middleware for Accept-Language Header Source: https://github.com/taig/babel/blob/main/modules/documentation/src/cookbook.md This middleware resolves the preferred language from the `Accept-Language` header in an http4s request. It uses Java's `Locale.LanguageRange.parse` to create an ordered list of `java.util.Locale`s and matches them against the application's supported locales, falling back to a default if no match is found. ```scala import java.util.{Locale => JavaLocale} import scala.jdk.CollectionConverters._ import cats.Monad import io.taig.babel.Locale import org.http4s.HttpRoutes import org.http4s.headers.`Accept-Language` import org.http4s.implicits._ final class LocalesMiddleware[F[_]: Monad](locales: Set[Locale], fallback: Locale) { def apply(routes: Locale => HttpRoutes[F]): HttpRoutes[F] = HttpRoutes[F] { request => val locale = request.headers .get[`Accept-Language`] .map(_.value) .flatMap { value => JavaLocale.LanguageRange .parse(value) .asScala .sortWith(_.getWeight > _.getWeight) .map(language => JavaLocale.forLanguageTag(language.getRange)) .flatMap(Locale.fromJavaLocale) .find(locales.contains) } .getOrElse(fallback) routes(locale).run(request) } } ``` -------------------------------- ### Load and Decode Translations with Babel Scala Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-arguments.md Loads internationalization resources using Babel's default loader, decodes them into a case class, and applies fallback mechanisms. It requires dependencies like cats-effect and io.taig.babel. ```Scala import cats.effect._ import cats.effect.unsafe.implicits.global import cats.syntax.all._ import io.taig.babel._ import io.taig.babel.generic.auto._ final case class I18n(weather: StringFormat2) val i18ns = Loader .default[IO] .load("arguments", Set(Locales.en, Locales.de)) .map(Decoder[I18n].decodeAll) .rethrow .map(_.withFallback(Locales.en)) .flatMap(_.liftTo[IO](new IllegalStateException("Translations for en missing"))) .unsafeRunSync() ``` -------------------------------- ### Use Loaded Translations with Babel Scala (German) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-arguments.md Shows how to use the internationalization data for the German locale, calling the parameterized string function with arguments relevant to the German translation. ```Scala i18ns(Locales.de).weather("Frankfurt am Main", "18") ``` -------------------------------- ### Use Loaded Translations with Babel Scala (English) Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-arguments.md Demonstrates accessing the loaded and decoded internationalization data for a specific locale (English) and invoking a parameterized string function with the correct arguments. ```Scala i18ns(Locales.en).weather("Cape Town", "26") ``` -------------------------------- ### Compile-time Argument Count Check with Babel Scala Source: https://github.com/taig/babel/blob/main/modules/documentation/src/guide-arguments.md Illustrates Babel's compile-time guarantee for StringFormat. Passing an incorrect number of arguments to a StringFormatN instance (e.g., StringFormat2) results in a compilation error. ```Scala i18ns(Locales.en).weather("Stockholm") ```