Skip to content

Commit

Permalink
Improve tests and coumentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
alobaton-pricesmart committed Apr 15, 2021
1 parent a84eb45 commit 490000f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
69 changes: 66 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

> :warning: **Still under construction**
simple i18n support that relies on standard go libraries
Simple `i18n` support that relies on standard go libraries

## How to start?

The i18n package mainly includes a set of methods for managing the data. Start by creating a new i18n instance.
The `i18n` package mainly includes a set of methods for managing the data. Start by creating a `en.json` file.
```bash
{
"some": {
"awesome": {
"text": "Hello World!"

}
}
}
```
Create a new `I18N` instance as follows.
```bash
i18n := NewI18N().BindPath("./example/en.json")
i18n, err := i18n.BindMainLocale("en")
Expand All @@ -21,7 +31,7 @@ if err != nil {
}
```
Once you setup the i18n instance, you must be able to lookup for messages.
Once you setup the i18n instance, you should be able to lookup for messages.
```bash
result, err := i18n.Lookup("some.awesome.text")
if err != nil {
Expand All @@ -30,6 +40,59 @@ if err != nil {
fmt.Println(result)
```
The program should print `Hello World!`
### Lookup for a specific locale
```bash
result, err := i18n.LookupWithLocale("en", "some.awesome.text")
if err != nil {
...
}
fmt.Println(result)
```
The program should print `Hello World!`
### Lookup with arguments
`i18n` relies on `fmt.Sprintf(...)` to apply replacements, so you should be able to use it as follows.
Your `.json` file should look like this.
```bash
{
"some": {
"awesome": {
"textWithArgs": "Hello %s!"
}
}
}
```
Lookup for messages like this.
```bash
result, err := i18n.Lookup("some.awesome.textWithArgs", "i18n")
if err != nil {
...
}
fmt.Println(result)
```
The program should print `Hello i18n!`
## JSON Format
```bash
{
"some": {
"awesome": {
"text": "Hello World!",
"textWithArgs": "Hello %s!"
}
}
}
```
## How to install?
```bash
Expand Down
16 changes: 12 additions & 4 deletions i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

func TestLookupMessageFound(t *testing.T) {
var err error

i18n := NewI18N().BindPath("./example/en.json")
i18n, err := i18n.BindMainLocale("en")
i18n, err = i18n.BindMainLocale("en")
if err != nil {
t.Errorf("Shouldn't return error, got %v", err)
}
Expand All @@ -28,8 +30,10 @@ func TestLookupMessageFound(t *testing.T) {
}

func TestLookupMessageFoundWithArgs(t *testing.T) {
var err error

i18n := NewI18N().BindPath("./example/en.json")
i18n, err := i18n.BindMainLocale("en")
i18n, err = i18n.BindMainLocale("en")
if err != nil {
t.Errorf("Shouldn't return error, got %v", err)
}
Expand All @@ -51,8 +55,10 @@ func TestLookupMessageFoundWithArgs(t *testing.T) {
}

func TestLookupMessageNotFound(t *testing.T) {
var err error

i18n := NewI18N().BindPath("./example/en.json")
i18n, err := i18n.BindMainLocale("en")
i18n, err = i18n.BindMainLocale("en")
if err != nil {
t.Errorf("Shouldn't return error, got %v", err)
}
Expand All @@ -74,8 +80,10 @@ func TestLookupMessageNotFound(t *testing.T) {
}

func TestLookupMessageNotFoundMoreLevels(t *testing.T) {
var err error

i18n := NewI18N().BindPath("./example/en.json")
i18n, err := i18n.BindMainLocale("en")
i18n, err = i18n.BindMainLocale("en")
if err != nil {
t.Errorf("Shouldn't return error, got %v", err)
}
Expand Down

0 comments on commit 490000f

Please sign in to comment.