Skip to content

Commit

Permalink
Merge pull request #4 for v0.4.0 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 24, 2018
2 parents 8558a9d + 75d1605 commit 82db044
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 51 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ branches:
# skip tags build, we are building branch and master that is enough for
# consistenty check and release. Let's use Travis CI resources optimally
# for aah framework.
- /^v[0-9]\.[0-9]/
- /^v[0-9.]+$/

go:
- 1.8
- 1.8.x
- 1.9.x
- 1.x
- tip

go_import_path: aahframework.org/i18n.v0

before_install:
- bash <(curl -s https://aahframework.org/base-before-install) "essentials vfs forge config ahttp log"

install:
- git config --global http.https://aahframework.org.followRedirects true
- go get -t -v ./...

script:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-2017 Jeevanandam M., https://myjeeva.com <[email protected]>
Copyright (c) 2016-2018 Jeevanandam M., https://myjeeva.com <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# i18n - aah framework
[![Build Status](https://travis-ci.org/go-aah/i18n.svg?branch=master)](https://travis-ci.org/go-aah/i18n) [![codecov](https://codecov.io/gh/go-aah/i18n/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/i18n/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/i18n.v0)](https://goreportcard.com/report/aahframework.org/i18n.v0) [![Version](https://img.shields.io/badge/version-0.3-blue.svg)](https://github.com/go-aah/i18n/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/i18n.v0?status.svg)](https://godoc.org/aahframework.org/i18n.v0) [![License](https://img.shields.io/github/license/go-aah/i18n.svg)](LICENSE)

***v0.3 [released](https://github.com/go-aah/i18n/releases/latest) and tagged on Aug 14, 2017***
<p align="center">
<img src="https://cdn.aahframework.org/assets/img/aah-logo-64x64.png" />
<h2 align="center">i18n by aah framework</h2>
</p>
<p align="center">
<p align="center"><a href="https://travis-ci.org/go-aah/i18n"><img src="https://travis-ci.org/go-aah/i18n.svg?branch=master" alt="Build Status"></a> <a href="https://codecov.io/gh/go-aah/i18n/branch/master"><img src="https://codecov.io/gh/go-aah/i18n/branch/master/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/aahframework.org/i18n.v0"><img src="https://goreportcard.com/badge/aahframework.org/i18n.v0" alt="Go Report Card"></a> <a href="https://github.com/go-aah/i18n/releases/latest"><img src="https://img.shields.io/badge/version-0.4.0-blue.svg" alt="Release Version"></a> <a href="https://godoc.org/aahframework.org/i18n.v0"><img src="https://godoc.org/aahframework.org/i18n.v0?status.svg" alt="Godoc"></a> <a href="https://twitter.com/aahframework"><img src="https://img.shields.io/badge/[email protected]" alt="Twitter @aahframework"></a></p>
</p>

i18n library is built to manage and access message from store for internalization and localization.

*`i18n` developed for aah framework. However, it's an independent library, can be used separately with any `Go` language project. Feel free to use it.*
### News

* `v0.4.0` [released](https://github.com/go-aah/i18n/releases/latest) and tagged on Jun 24, 2018.

## Installation

# Installation
#### Stable Version - Production Ready
```sh
# install the library
```bash
go get -u aahframework.org/i18n.v0
```

Visit official website https://aahframework.org to learn more.
Visit official website https://aahframework.org to learn more about `aah` framework.
72 changes: 45 additions & 27 deletions i18n.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/i18n source code and usage is governed by a MIT style
// aahframework.org/i18n source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

// Package i18n is internationalization and localization support for aah
Expand Down Expand Up @@ -39,46 +39,52 @@ import (
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
"aahframework.org/vfs.v0"
)

// Version no. of aah framework i18n library
const Version = "0.3"

// I18n holds the message store and related information for internationalization
// and localization.
type I18n struct {
Store map[string]*config.Config
DefaultLocale string
fileExtRegex string
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//___________________________________
//______________________________________________________________________________

// New method creates aah i18n message store
func New() *I18n {
return NewWithVFS(nil)
}

// NewWithVFS method creates aah i18n message store with given Virtual FileSystem.
func NewWithVFS(fs *vfs.VFS) *I18n {
return &I18n{
Store: make(map[string]*config.Config),
fileExtRegex: `messages\.[a-z]{2}(\-[a-zA-Z]{2})?$`,
vfs: fs,
}
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// I18n methods
//___________________________________
//______________________________________________________________________________

// I18n holds the message store and related information for internationalization
// and localization.
type I18n struct {
Store map[string]*config.Config
DefaultLocale string

fileExtRegex string
vfs *vfs.VFS
}

// Load processes the given message file or directory and adds to the
// message store
func (s *I18n) Load(paths ...string) error {
for _, path := range paths {
if !ess.IsFileExists(path) {
if !vfs.IsExists(s.vfs, path) {
log.Warnf("Path: %v not exists, let's move on", path)
continue
}

if ess.IsDir(path) {
_ = ess.Walk(path, func(fpath string, f os.FileInfo, _ error) error {
if s.isDir(path) {
_ = vfs.Walk(s.vfs, path, func(fpath string, f os.FileInfo, _ error) error {
if !f.IsDir() {
match, err := regexp.MatchString(s.fileExtRegex, f.Name())
if err == nil && match {
Expand Down Expand Up @@ -111,10 +117,10 @@ func (s *I18n) Lookup(locale *ahttp.Locale, key string, args ...interface{}) str
// Lookup by language and region-id. For eg.: en-us
store := s.findStoreByLocale(locale.String())
if store == nil {
log.Tracef("Locale (%v) doesn't exists in message store", locale.String())
log.Tracef("Locale (%v) doesn't exists in message store", locale)
goto langStore
}
log.Tracef("Message is retrieved from locale: %v, key: %v", locale.String(), key)
log.Tracef("Message is retrieved from locale: %v, key: %v", locale, key)
if msg, found := retriveValue(store, key, args...); found {
return msg
}
Expand Down Expand Up @@ -154,13 +160,13 @@ func (s *I18n) Locales() []string {
return locales
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// I18n Unexported methods
//___________________________________
//______________________________________________________________________________

func (s *I18n) processMsgFile(file string) {
key := strings.ToLower(filepath.Ext(file)[1:])
msgFile, err := config.LoadFile(file)
msgFile, err := config.VFSLoadFile(s.vfs, file)
if err != nil {
log.Errorf("Unable to load message file: %v, error: %v", file, err)
}
Expand All @@ -184,9 +190,9 @@ func (s *I18n) findStoreByLocale(locale string) *config.Config {
return nil
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//___________________________________
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package Unexported methods
//______________________________________________________________________________

func retriveValue(store *config.Config, key string, args ...interface{}) (string, bool) {
if msg, found := store.String(key); found {
Expand All @@ -197,3 +203,15 @@ func retriveValue(store *config.Config, key string, args ...interface{}) (string
}
return "", false
}

func (s *I18n) isDir(name string) bool {
if s.vfs == nil {
return ess.IsDir(name)
}

fi, err := s.vfs.Lstat(name)
if err != nil {
return false
}
return fi.IsDir()
}
23 changes: 14 additions & 9 deletions i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (

func TestLoadMessage(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))
err := store.Load(filepath.Join(wd, "testdata"))
assert.Nil(t, err)
_ = store.Load(filepath.Join(wd, "testdata", "english", "messages.en"))
_ = store.Load(filepath.Join(wd, "testdata", "english", "message-not-exists.en"))

Expand All @@ -34,7 +35,7 @@ func TestLoadMessage(t *testing.T) {

func TestMsgRetrive_enUS(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))

Expand All @@ -52,7 +53,7 @@ func TestMsgRetrive_enUS(t *testing.T) {

func TestMsgRetrive_enGB(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))
store.DefaultLocale = "en"
Expand All @@ -74,7 +75,7 @@ func TestMsgRetrive_enGB(t *testing.T) {

func TestMsgRetrive_en(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))
store.DefaultLocale = "en"
Expand All @@ -99,7 +100,7 @@ func TestMsgRetrive_en(t *testing.T) {

func TestMsgRetrive_frCA(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))

Expand All @@ -117,7 +118,7 @@ func TestMsgRetrive_frCA(t *testing.T) {

func TestMsgRetrive_fr(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))

Expand All @@ -132,7 +133,7 @@ func TestMsgRetrive_fr(t *testing.T) {

func TestMsgRetrive_it(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))

Expand All @@ -147,7 +148,7 @@ func TestMsgRetrive_it(t *testing.T) {

func TestMsgRetriveNotFoundLocale(t *testing.T) {
wd, _ := os.Getwd()
store := New()
store := newI18n()

_ = store.Load(filepath.Join(wd, "testdata"))

Expand All @@ -156,3 +157,7 @@ func TestMsgRetriveNotFoundLocale(t *testing.T) {
notFoundStore := store.Lookup(&locale, "store.not.exists")
assert.Equal(t, "", notFoundStore)
}

func newI18n() *I18n {
return New()
}
8 changes: 8 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/i18n source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package i18n

// Version no. of aah framework i18n library
const Version = "0.4.0"

0 comments on commit 82db044

Please sign in to comment.