Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor project and add Google Drive import&export #22

Closed
wants to merge 61 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
c62083d
Ignore .ruby-version
AntoineInsa Nov 28, 2014
8465899
Add TranslationsManager
AntoineInsa Nov 28, 2014
fba10bf
Modify TranslationsImporter
AntoineInsa Nov 28, 2014
045a3e6
Modify TranslationsExporter
AntoineInsa Nov 28, 2014
d86b44b
Modify SubTranslationConverter
AntoineInsa Nov 28, 2014
1592aa4
Add GoogleDriveManager
AntoineInsa Nov 28, 2014
4799da2
Modify manage translations tasks
AntoineInsa Nov 28, 2014
9ed271c
Move MissingKeysFinder
AntoineInsa Nov 28, 2014
62881b9
Modify requirements and dependencies
AntoineInsa Nov 28, 2014
793d992
Merge into TranslationManager and SubTranslation
AntoineInsa Dec 3, 2014
24b501a
Erase and resize spreadsheet after upload
AntoineInsa Dec 3, 2014
08a3415
Set config['options'] to empty when not defined
AntoineInsa Dec 3, 2014
2ce8a2a
update google-api-client and google_drive gems; switch completely to …
tarix Mar 4, 2015
5385b3f
reference spreadsheets by the key and worksheet name
tarix Mar 4, 2015
7997b90
save and refresh the google access token
tarix May 23, 2015
137718e
when importing csv files surround values in quotes in order to preser…
tarix May 23, 2015
915f3af
refactor csv value quoting
tarix May 24, 2015
19e78fc
Use translations.yml configuration file for Google OAuth credentials.
Jun 2, 2015
782792d
Revert "when importing csv files surround values in quotes in order t…
Jun 4, 2015
4d4e769
Revert "refactor csv value quoting"
Jun 4, 2015
0091e52
Bump version.
Jun 4, 2015
d865ae5
Merge pull request #1 from attendease/master
AntoineInsa Jun 11, 2015
7e56b0f
Add a Utils class for blank? and present?
AntoineInsa Jun 12, 2015
158b5c8
Move methods to I18nDocs::TranslationsManager
AntoineInsa Jun 12, 2015
10535c7
Remove Rails dependencies
AntoineInsa Jun 12, 2015
6386502
Add GoogleAPIClient application name and version
AntoineInsa Jun 12, 2015
ff2d458
Update README with new config and options
AntoineInsa Jun 12, 2015
2e85174
Bump to v0.0.8
AntoineInsa Jun 12, 2015
a15ce65
Update options fallback, rename config to i18n-docs.yml
AntoineInsa Jun 15, 2015
457014a
Add include_locale_key option
AntoineInsa Jun 15, 2015
f29f877
Rewrite ready.md
AntoineInsa Jun 15, 2015
cd91bc5
Update export to use single_locale_file and include_locale_key
AntoineInsa Jun 15, 2015
43e3ee5
Export local documents, sort keys
AntoineInsa Jun 15, 2015
b8284ff
Only use GoogleDrive if needed
AntoineInsa Jun 15, 2015
1acfe65
Update output messages
AntoineInsa Jun 15, 2015
2f1c0c6
Make upload work again, with spreadsheet_key and worksheet_name
AntoineInsa Jun 16, 2015
c8065d7
Add a force_fallback options to force all value to be present
AntoineInsa Jun 16, 2015
0528338
add waffle.io badge
waffle-iron Jun 16, 2015
df9492e
Bump to consistent 0.0.73
AntoineInsa Jul 9, 2015
80ad0b7
Add ability to overwrite 'files' in the ruby options
AntoineInsa Aug 27, 2015
b77b3a0
Only add translations when key is present
AntoineInsa Aug 27, 2015
319d0d6
Add/refactor tests, simply run 'rake'
AntoineInsa Aug 27, 2015
3dcdb70
Add .travis.ci
AntoineInsa Aug 27, 2015
b8ddffe
Merge pull request #3 from waffle-iron/master
AntoineInsa Aug 27, 2015
50b0d91
Merge branch 'master' into v0.0.8
AntoineInsa Aug 27, 2015
b2ea22e
Change travis email notifications
AntoineInsa Aug 27, 2015
7e69492
Merge pull request #2 from AntoineInsa/v0.0.8
AntoineInsa Aug 27, 2015
90d74aa
Update README.md
AntoineInsa Aug 27, 2015
850a036
Add code climate test coverage
AntoineInsa Aug 27, 2015
9cd01b1
Rename to .travis.yml
AntoineInsa Aug 27, 2015
0ab05ec
Raise error when no 'key' header
AntoineInsa Aug 27, 2015
17f6776
Insert translation only if a key is present
AntoineInsa Aug 27, 2015
43776b3
Ensure no inconsistent key nesting
AntoineInsa Aug 27, 2015
c988291
Correct special char typo
AntoineInsa Aug 27, 2015
46a4372
Define google_drive_credentials as regular option
AntoineInsa Aug 27, 2015
7e8299f
Specify fallback chain
AntoineInsa Aug 27, 2015
7bb4953
Add testing for special characters import
AntoineInsa Aug 27, 2015
1a9bac9
Update gem loading examples
AntoineInsa Aug 28, 2015
022fbf8
Flatten options for more consistency
AntoineInsa Aug 28, 2015
e739eb0
Update various README typos/text
AntoineInsa Aug 28, 2015
0634ef7
Merge pull request #10 from AntoineInsa/fix/special-characters
AntoineInsa Jul 21, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -6,4 +6,5 @@ pkg/*
.rake_tasks~
doc/yardoc
.yardoc
tmp/*
tmp/*
.ruby-version
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--format documentation
--color
10 changes: 10 additions & 0 deletions .travis.yml
@@ -0,0 +1,10 @@
language: ruby
rvm:
- 2.0.0
notifications:
email:
on_success: never
on_failure: change
addons:
code_climate:
repo_token: bceabf3e60db5e6d9ef6c6485eb0f58d6fc5222a780c9b5d07115158ad702976
4 changes: 3 additions & 1 deletion Gemfile
@@ -1,4 +1,6 @@
source "https://rubygems.org"

# Specify your gem's dependencies in localch.gemspec
# Specify your gem's dependencies in i18n-docs.gemspec
gemspec

gem "codeclimate-test-reporter", group: :test, require: nil
301 changes: 256 additions & 45 deletions README.md
@@ -1,33 +1,213 @@
# i18n-docs

**i18n-docs** is a ruby gem that helps you to keep translations stored in Google Docs. This makes it easier for translators and project members to coordinate changes and updates. In addition, the standard features of Google Docs are super handy for this process: revision tracking, authorization, publishing, etc.
[![Gem Version](https://badge.fury.io/rb/i18n-docs.svg)](http://badge.fury.io/rb/i18n-docs)
[![Build Status](https://travis-ci.org/AntoineInsa/i18n-docs.svg?branch=master)](https://travis-ci.org/AntoineInsa/i18n-docs)
[![Test Coverage](https://codeclimate.com/github/AntoineInsa/i18n-docs/badges/coverage.svg)](https://codeclimate.com/github/AntoineInsa/i18n-docs/coverage)
[![Code Climate](https://codeclimate.com/github/AntoineInsa/i18n-docs/badges/gpa.svg)](https://codeclimate.com/github/AntoineInsa/i18n-docs)
[![Stories in Ready](https://badge.waffle.io/AntoineInsa/i18n-docs.png?label=ready&title=Ready)](https://waffle.io/AntoineInsa/i18n-docs)

Although we use it with Google Docs, it could be used with any CSV file.
# i18n-docs

*this gem is currently in use and tested with Rails 3.1. It probably works with other 3.x versions, but probably not 2.x at the moment.*
**i18n-docs** is a ruby gem that helps you to maintain your translations files, using a Google Docs Spreadsheet, or any other csv file. This makes it easier for translators and project members to coordinate changes and updates. In addition, the standard features of Google Docs are super handy for this process: revision tracking, authorization, etc.

This gem works both in Rails and non-Rails environment. See [3. Usage](#3-usage) for more details.

## 1. Features

- import translations from Google Spreadsheet/Worksheet or csv files to locales yml files (one per locale)
- export translations from local locales yml files to Google Spreadsheet/Worksheet or csv files

### 1.1 Spreadsheet structure

The spreadsheet can either be a Google Drive spreadsheet worksheet or a csv file.

eg: **Google Drive**
Spreadsheet name: `my-i18n`
Spreadsheet key: `1GsK...Cjwfw` (see the url `https://docs.google.com/spreadsheets/d/{spreadsheet_key}/edit`)
Worksheet name: `numbers`
```
| key | en | fr | de |
|---------------|-------|-------|------|
| numbers.one | one | un | eins |
| numbers.two | two | deux | zwei |
| numbers.three | three | trois | drei |
```

eg: **csv spreadsheet**
File path: `config/locales/activities.yml`
```
| key | en | fr | de |
|-------------|------|---------|--------|
| places.home | Home | Maison | Heim |
| places.work | Work | Travail | Arbeit |
| places.park | Park | Parc | Park |
```


### 1.2 Locales structure

This is the assumed file structure of the project locales:
```
locales
├── en
│   ├── numbers.yml
│   └── activities.yml
├── de
│   ├── numbers.yml
│   └── activities.yml
└── fr
├── numbers.yml
└── activities.yml
```

And this is an example of one of the translated file `en/numbers.yml`:
```
---
en:
numbers:
one: one
two: two
three: three
```

## 2. Configuration

Create a configuration file called `i18n-docs.yml` as the following:

```
files:
numbers.yml: "google-drive|1GsK...Cjwfw|numbers"
activities.yml : "config/locales/activities.csv"
... etc ...

google_drive_credentials:
oauth:
client_id: "YOUR_ID_GOES_HERE"
client_secret: "YOUR_SECRET_GOES_HERE"


default_locale: 'en' # The main locale, does only count for export
locales: ['en','fr','de'] # Subset of locales to play with
google_drive_credentials: {} # Google Drive credentials (see `i18n-docs.yml`)
files: {key => value} # Files to use (see `i18n-docs.yml`)
files_only: ['activities.yml'] # Subset of filename to use
cleanup: false # Remove temporary files
tmp_dir: 'tmp' # Temporary upload/download directory, default: './tmp/i18n-docs/'
locales_dir: 'i18n' # Final locales directory, default './_i18n' or './locales'
single_locale_file: true # Deal only with one file per locale (no locale subdirectories)
include_locale_key: true # Include the locale as a key at the root of the locale file
force_fallback: true # Force all translations to have a value, default: false
# Fallback chain 1.locale, 2.default locale, 3.humanized key

# Future work
logger_level: 0 # Verbose?
format: 'yml' # .yml or .json ? default: .yml
```

### 2.1 Files

In this section, locale files are tied with translation spreadsheets.

Google Doc spreadsheet is identified by its spreadsheet key and worksheet name with the following format:

```
google_drive|{spreadsheet_key}|{worksheet_name}
```

You can pull information from some local files by giving directly an url from where the gem is run (as `activities.yml` in the example).

### 2.2 Google Drive Credentials

To get Google OAuth client_id and secret, you can follow this link: https://console.developers.google.com/project

The first time you'll run i18n-docs, it will open a webpage with Google asking you to authorize the application. After having accepted, you'll get a authorization key.
Copy and paste the key in the prompt. The key will be saved for future calls in `.i18n-docs-access-token`.

### 2.3 Options

There's actually different ways of calling these options. You can define them:
- when calling a method in ruby, by passing options as a hash (eg: `I18nDocs::TranslationsManager.import_translations({'locales_dir' => '_i18n'})`)
- when running a rake task, by setting env variables (eg: `locales_dir=_i18n rake i18n:export_translations`)
- or simply by writing them in `i18n-docs.yml`

The only options that is not in the config file is the directory where to find the config file!
You can specify it as a ruby or an env option with the key `i18n-docs-path`.
By default, it will look in the current directory, or in the `config` directory for a Rails app.

With `single_locale_file=true` (and with only one file specified), you'll get the following file structure:
```
locales
├── en.yml
├── fr.yml
└── de.yml
```

With `include_locale_key=false`, you'll get the following locale files eg `en/numbers.yml`:
```
---
numbers:
one: one
two: two
three: three
```
Note that `en:` has disappeared from the top.

With `force_locale=true`, all blank values will be force to be present:
```
| key | en (default) | fr | de |
|-------------|--------------|--------|------|
| places.home | Home | Maison | Heim |
| places.work | Workplace | | |
| places.park | | | |
```
becomes
```
| key | en (default) | fr | de |
|--------------------|--------------|-------------|-------------|
| places.home | Home | Maison | Heim |
| places.work | Workplace | Workplace | Workplace |
| places.castle_park | Castle park | Castle park | Castle park |
```
Note that locales are defaulting to the default locale, and then to the humanized key.


## 3. Usage

### 3.1 Specifying gem

The official i18n-docs gem lives here https://rubygems.org/gems/i18n-docs.
You can refer to it via:

group :development do
gem 'i18n-docs'
gem 'i18n-docs', '~> 0.0.7'
end

## Features:
But because a lot of development had happened lately
which hasn't been merged into local-ch/i18n-docs yet,
you may want to reference a commit directly on github,
following http://bundler.io/git.html.

* download translations from multiple Google spreadsheets and store to YAML files
* import/export YAML files with a Google Spreadsheet
* check YAML files for missing translations (`rake i18n:find_missing_keys`)
group :development do
gem 'i18n-docs', :github => 'AntoineInsa/i18n-docs',
gem 'i18n-docs', :github => 'AntoineInsa/i18n-docs', :branch => 'master'
gem 'i18n-docs', :github => 'AntoineInsa/i18n-docs', :tag => 'v0.0.8'
gem 'i18n-docs', :github => 'AntoineInsa/i18n-docs', :ref => 'b2ea22e18b3'
end

## Usage
You can as well clone/download the project, and use it locally:

### Configuration
group :development do
gem 'i18n-docs', :path => '~/Sites/i18n-docs'
end

Add the GEM to your Rails project:
### 3.2 For Rails user

gem 'i18n-docs'
Add the gem to your Rails project:

Create a configuration file in `config/translations.yml`:
gem 'i18n-docs', :github => 'AntoineInsa/i18n-docs'

files:
navigation.yml: "https://docs.google.com/spreadsheet/pub?key=ab43...34f3&single=true&gid=0&output=csv"
forms.yml: "https://docs.google.com/spreadsheet/pub?key=0Ap...XveWc&single=true&gid=0&output=csv"
... etc ...

Finally, let Rails know what locales you will be using. Add this to `config/application.rb`:
Let Rails know what locales you will be using. Add this to `config/application.rb`:

module Web
class Application < Rails::Application
Expand All @@ -38,55 +218,86 @@ Finally, let Rails know what locales you will be using. Add this to `config/appl
end
end

This defines which languages and translation files to import from a Google Spreadsheet. The content of the Spreadsheet URL is stored to a file called e.g. `example1.yml` within folders `config/locales/en` and all other detected locales.

### Rake Tasks
This defines which languages and translation files to import from a Google Spreadsheet. The content of the spreadsheets will be stored under `config/locales/`.

Following Rake tasks are added by the GEM to your Rails project:
The following rake tasks are added by the gem to your Rails project:

* `rake i18n:export_translations`
Export all language files to CSV files (ENV 'locale' have to set for non-'en' locales else only files contained in 'en' folder are considered)
Export all translations files to spreadsheets (Google Drive and/or local files) (the defined default locale is the reference for files to export)
* `rake i18n:import_translations`
Download translations from Google Spreadsheet and save them to YAML files.
* `rake i18n:missing_keys`
Find and list translation keys that do not exist in all locales
Download translations from spreadsheets (Google Drive and/or local files) and save them into YAML files.

*This gem is currently in use and tested with Rails 3.1. It probably works with other 3.x versions, but probably not 2.x at the moment.*

### 3.3 For non-Rails users

Pick-up any directory, add a `i18n-docs.yml` file (see 2.).

Add a `Gemfile`:

source "https://rubygems.org"
gem 'i18n-docs', :github => 'AntoineInsa/i18n-docs'


Add a `i18n-docs.rb`:

## Setup at Google Docs
#!/usr/bin/env ruby

* At Google Docs, create a new spreadsheet for your translations. The first column is the translation key, the other columns are the languages:
![screenshot](http://dl.dropbox.com/u/385855/Screenshots/bamr.png)
* use option `File -> Publish To the Web`
![screenshot](http://dl.dropbox.com/u/385855/Screenshots/2-lp.png)
* use **get a link** to generate a unique url that points to the CSV spreadsheet
![screenshot](http://dl.dropbox.com/u/385855/Screenshots/oom_.png)
* From now on you should only update translations in Google Docs and run `rake i18n:import_translations` in the application to get changes. You can also export your
require 'rubygems'
require 'i18n-docs'

## Todo
# To import translations
I18nDocs::TranslationsManager.import_translations()

*Pull requests welcome!*
# To export translations
# I18nDocs::TranslationsManager.export_translations()

* create mocked tests that do not have to download our sample CSV files
* automate the creation of files in config/locales
* automate the detection of which languages are supported
* validate use with rails 2.x, other 3.x versions

## CHANGELOG
You may have to install:
- Rbenv (https://github.com/sstephenson/rbenv)
- RubyGems (https://rubygems.org/pages/download)
- Bundler (http://bundler.io/)

And simply execute the file by running `bundle install` the first time, and `bundle exec ruby i18n-docs.rb`! Et voilà!


## 4. CHANGELOG

### 0.0.8

Make it work outside of a Rails environment

### 0.0.7-2

Use i18n-docs.yml (old translations.yml) configuration file for Google OAuth credentials.

### 0.0.7-1

Upgrade to OAuth before Google deprecation deadline.

### 0.0.4

Update docs, license. Push to Rubygems.org.

### 0.0.3

* Open sourced: changed name and description.
Open sourced: changed name and description.

### 0.0.2

* removed loading of `awesome_print` from the rake task. It was breaking
download
Removed loading of `awesome_print` from the rake task. It was breaking download.

## 5. Contributing

1. Fork it ( https://github.com/AntoineInsa/i18n-docs/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Run the tests (`bundle exec rspec spec` or `rake`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create a new Pull Request

### Credits/License
## 6. Credits/License

This gem is sponsored by [local.ch](http://www.local.ch/). It is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License). If you're a ruby developer and want to work with us in Switzerland, please check out our [jobs page](http://local-ch.github.com/).

This gem has been improved by [Attendease](http://attendease.com) as well, from Vancouver, BC, Canada.