• Stars
    star
    38
  • Rank 706,870 (Top 14 %)
  • Language
    Scala
  • License
    MIT License
  • Created about 13 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GNU Gettext .po file loader for Scala

image

Scaposer is a GNU gettext po file parser written in Scala. It's strange that there's not many JVM libraries of this kind, see the discussion on Stackoverflow.

To extract i18n strings from Scala source code files, use Scala xgettext.

Presentation: I18nize Scala programs à la gettext

Discussion group: https://groups.google.com/group/scala-xgettext

Basic usage

See Scaladoc.

val po = """
msgid "Hello"
msgstr "Bonjour"
"""

val result = scaposer.Parser.parse(po)
// => An Either,
// Left(scaposer.ParseFailure) or
// Right(Seq[scaposer.Translation])

Use t methods to get the translations:

val translations = result.right.get
val i18n         = scaposer.I18n(translations)
i18n.t("Hello")  // => "Bonjour"

If there's no translation, or the translation is an empty string (not translated yet), the original input is returned:

i18n.t("Hi")  // => "Hi"

Context

val po = """
msgid "Hello"
msgstr "Bonjour"

msgctxt "Casual"
msgid "Hello"
msgstr "Salut"
"""

val translations = scaposer.Parser.parse(po).right.get
val i18n         = scaposer.I18n(translations)
i18n.tc("Casual", "Hello")  // => "Salut"

If there's no translation for the context, the translation without context is tried:

i18n.tc("Missing context", "Hello")  // => "Bonjour"

Plural-Forms

val po = """
msgid ""
msgstr "Plural-Forms: nplurals=2; plural=n>1;"

msgid "I have one apple"
msgid_plural "I have %d apples"
msgstr[0] "J'ai une pomme"
msgstr[1] "J'ai %d pommes"
"""

val translations = scaposer.Parser.parse(po).right.get
val i18n         = scaposer.I18n(translations)
i18n.tn("I have one apple", "I have %d apples", 1)                // => "J'ai une pomme"
i18n.tn("I have one apple", "I have %d apples", 2)                // => "J'ai %d pommes"
i18n.tcn("A context", "I have one apple", "I have %d apples", 3)  // => "J'ai %d pommes"

For performance, your po file should define Plural-Forms exactly as at:

Otherwise, Scaposer cannot compare the plural form string, and it needs to parse and evaluate (slower).

Merge Po objects

You can merge multiple I18ns together.

val i18n4 = i18n1 ++ i18n2 ++ i18n3

Just like when you merge maps, translations in i18n3 will overwrite those in i18n2 will overwrite those in i18n1.

Use with SBT

Supported Scala versions: 2.11-2.13

build.sbt example:

libraryDependencies += "tv.cntt" %% "scaposer" % "1.11.1"

Scaposer is used in Xitrum web framework.

More Repositories

1

xitrum

Async and clustered Scala web framework and HTTP(S) server
Scala
446
star
2

scalive

Connect a Scala REPL to running JVM processes without any prior setup
Java
196
star
3

glokka

Library to register and lookup actors by names in an Akka cluster
Scala
56
star
4

scala-xgettext

Scala compiler plugin that acts like GNU xgettext command to extract i18n strings in Scala source code files to Gettext .po file
Scala
25
star
5

xitrum-new

Empty Xitrum project skeleton, like the one created by "rails new"
Scala
21
star
6

xitrum-demos

Demos for Xitrum
Scala
19
star
7

xitrum-package

SBT plugin for collecting dependency .jar files for standalone Scala programs
Scala
18
star
8

comy

Simple URL shortener using Xitrum and MongoDB
Scala
16
star
9

sclasner

Scala classpath scanner
Scala
10
star
10

xitrum-doc

Xitrum Guide Book and presentations about Xitrum
CSS
8
star
11

RhinoCoffeeScript

coffee-script.js precompiled to JVM .class file by Rhino
Java
7
star
12

agent7

Java agent to reload .class files; it uses file watch API available in Java 7+
Java
5
star
13

xitrum-scalate

Template engine for Xitrum based on Scalate
Scala
4
star
14

xitrum-hazelcast

Cache and server side session store for Xitrum, based on Hazelcast
Scala
3
star
15

xitrum-multimodule-demo

http://groups.google.com/group/xitrum-framework/browse_thread/thread/7588995934854a56
Scala
2
star
16

xitrum-ko

Knockout.js plugin for Xitrum
Scala
1
star
17

xitrum-scalatags

Template engine for Xitrum based on ScalaTags
Scala
1
star
18

xitrum-framework.github.io

Source code of the home page of Xitrum framework
HTML
1
star
19

xitrum-tictactoe

Tictactoe web game - Demo for Akka and Xitrum
Scala
1
star