-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Localization Language Feature (#618)
Co-authored-by: jansul <jon@sul.ly>
- Loading branch information
1 parent
5c491a6
commit d04f5c0
Showing
37 changed files
with
569 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Locale { | ||
/* Sets the current locale. */ | ||
fun set (locale : String) : Bool { | ||
`_L.set(#{locale})` | ||
} | ||
|
||
/* Returns the current locale. */ | ||
fun get : Maybe(String) { | ||
` | ||
(() => { | ||
if (_L.locale) { | ||
return #{Maybe::Just(`_L.locale`)} | ||
} else { | ||
return #{Maybe::Nothing} | ||
} | ||
})() | ||
` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Locale | ||
|
||
This feature of the language allows specifing localization tokens and values for languages indentified by [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes. | ||
|
||
```mint | ||
locale en { | ||
ui: { | ||
buttons: { | ||
ok: "OK" | ||
} | ||
} | ||
} | ||
locale hu { | ||
ui: { | ||
buttons: { | ||
ok: "Rendben" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
A locale consists of a tree structure where the keys are lowercase identifiers and the values are expressions. | ||
|
||
To localize a value you need to use the locale token: | ||
|
||
``` | ||
:ui.buttons.ok | ||
``` | ||
|
||
or if it's a function if can be called: | ||
|
||
``` | ||
locale en { | ||
ui: { | ||
buttons: { | ||
ok: (param1 : String, param2 : String) { | ||
"Button #{param1} #{param2}!" | ||
} | ||
} | ||
} | ||
} | ||
:ui.buttons.ok(param1, param2) | ||
``` | ||
|
||
To get and set the current locale the `Locale` module can be used: | ||
|
||
``` | ||
Locale.set("en") | ||
Locale.get() // Maybe::Just("en") | ||
``` | ||
|
||
Every translation is typed checked: | ||
|
||
* all translations of the same key must have the same type | ||
* locale keys must have translations in every defined language | ||
|
||
Locales are open like Modules are so they can be defined in multiple places. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
locale en { | ||
test: "Hello" | ||
} | ||
|
||
component Main { | ||
fun render : String { | ||
:test | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
class A extends _C { | ||
componentWillUnmount() { | ||
_L._unsubscribe(this); | ||
} | ||
|
||
componentDidMount() { | ||
_L._subscribe(this); | ||
} | ||
|
||
render() { | ||
return _L.t("test"); | ||
} | ||
}; | ||
|
||
A.displayName = "Main"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
locale en { | ||
ui: { | ||
button: { | ||
ok: "OK" | ||
} | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
locale en { | ||
ui: { button: { ok: "OK" } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
locale en { | ||
ui: { | ||
button: { | ||
ok: "OK" | ||
} | ||
} | ||
} | ||
|
||
component Main { | ||
fun render : String { | ||
:ui.button.ok | ||
} | ||
} | ||
-------------------------------------------------------------------------------- | ||
locale en { | ||
ui: { button: { ok: "OK" } } | ||
} | ||
|
||
component Main { | ||
fun render : String { | ||
:ui.button.ok | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "../spec_helper" | ||
|
||
describe "Locale" do | ||
subject locale_key | ||
|
||
expect_ignore "comp" | ||
expect_ignore "asd" | ||
expect_ignore ":" | ||
|
||
expect_ok ":ui.button.ok" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "../spec_helper" | ||
|
||
describe "Locale" do | ||
subject locale | ||
|
||
expect_ignore "comp" | ||
expect_ignore "asd" | ||
|
||
expect_error "locale", Mint::Parser::LocaleExpectedLanguage | ||
expect_error "locale{", Mint::Parser::LocaleExpectedLanguage | ||
expect_error "locale ", Mint::Parser::LocaleExpectedLanguage | ||
expect_error "locale en", Mint::Parser::LocaleExpectedOpeningBracket | ||
expect_error "locale en {", Mint::Parser::LocaleExpectedClosingBracket | ||
|
||
expect_ok "locale en { a: \"\" }" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
locale en { | ||
test: "" | ||
} | ||
|
||
component Main { | ||
fun render : String { | ||
:test | ||
} | ||
} | ||
-------------------------------------------------------------TranslationMissing | ||
component Main { | ||
fun render : String { | ||
:test | ||
} | ||
} | ||
-------------------------------------------------------TranslationNotTranslated | ||
locale en { | ||
test: "" | ||
} | ||
|
||
locale hu { | ||
|
||
} | ||
|
||
component Main { | ||
fun render : String { | ||
:test | ||
} | ||
} | ||
------------------------------------------------------------TranslationMismatch | ||
locale en { | ||
test: "" | ||
} | ||
|
||
locale hu { | ||
test: 0 | ||
} | ||
|
||
component Main { | ||
fun render : String { | ||
:test | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Mint | ||
class Ast | ||
class Locale < Node | ||
getter fields, comment, language | ||
|
||
def initialize(@fields : Array(RecordField), | ||
@comment : Comment?, | ||
@language : String, | ||
@input : Data, | ||
@from : Int32, | ||
@to : Int32) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mint | ||
class Ast | ||
class LocaleKey < Node | ||
getter value | ||
|
||
def initialize(@value : String, | ||
@input : Data, | ||
@from : Int32, | ||
@to : Int32) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Mint | ||
class Compiler | ||
def _compile(node : Ast::LocaleKey) : String | ||
%(_L.t("#{node.value}")) | ||
end | ||
end | ||
end |
Oops, something went wrong.