https://github.com/shouya/simple_i18n
A simple internationalization library for easily creating multilanguage ruby program
https://github.com/shouya/simple_i18n
Last synced: 27 days ago
JSON representation
A simple internationalization library for easily creating multilanguage ruby program
- Host: GitHub
- URL: https://github.com/shouya/simple_i18n
- Owner: shouya
- License: mit
- Created: 2012-07-21T17:14:10.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2012-07-21T19:18:54.000Z (almost 13 years ago)
- Last Synced: 2025-03-25T02:54:29.009Z (about 1 month ago)
- Language: Ruby
- Homepage: https://rubygems.org/gems/simple_i18n
- Size: 97.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple I18n
Using this gem is very easy to add multilanguage support for small projects.
## Installation
Add this line to your application's Gemfile:
gem 'simple_i18n'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_i18n
## Usage
First you'd create a language specification file. This file is recommended to
named as `i18nspec.rb` and put in the root directory of project.Which shall contain the content like:
require 'simple_i18n'
SimpleI18n.load_language_file('language/en')
SimpleI18n.load_language_file('language/zh')
# ... other languages
SimpleI18n.set_default_language('en')Where you'd create a directory names `language` too. And inside there shall be
the language translation files, right name as form of `lang-en` as above.The language files should contain the translation tables. Fortunately they are
defined in ruby also, in the form of:# in file `en`
define_language('en', 'English') do |en|
en['display-username'] = 'Display Username'
en['reset-password'] = 'Reset Password'
# ... others translations
endDifferent translation tables should define same tokens, for example:
# in file `zh`
# -*- encoding: utf-8 -*-
define_language('zh', '中文') do |zh|
zh['display-username'] = '顯示用戶名'
zh['reset-password'] = '重置密碼'
# ... blah blah blah
endNote the encoding specification might be required for non-ascii files.
Then you can use these in your code:
# a file in project
require '../i18nspec'
def turn_to_english
swith_language('en')
end
def turn_to_chinese
switch_language('zh')
end
def foo
tr['display-username'] #=> 'Display Username' or '顯示用戶名'
endVia refering translations with `tr` and changing current language with
`switch_language` methods whereever you need.If there are places that you don't understand, you can move on the
demonstration project, which is located in `demo_project` directory.
And if that doesn't help too, welcome to write to me.## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request