Skip to content

Commit

Permalink
Merge pull request #93 from ddnexus/i18n
Browse files Browse the repository at this point in the history
I18n refactoring
  • Loading branch information
ddnexus authored Oct 2, 2018
2 parents 05afcdb + f8017c3 commit bab3bf8
Show file tree
Hide file tree
Showing 32 changed files with 248 additions and 261 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Besides the classic pagination `nav`, Pagy offers a few ready to use alternative

## Please Star and Share!

Pagy is young and needs to be known, and **you** can really help, even with just a click on the star, or sharing a tweet with friends and collegues. A big thank you for your help!
Pagy is young and needs to be known, and **you** can really help, even with just a click on the star, or sharing a tweet with friends and collegues. A big thank you for your support!

## Help Wanted

Expand All @@ -148,7 +148,7 @@ Pagy is a fresh project and your help would be great. If you like it, you have a
- Create an issue if anything should be improved/fixed
- Submit a pull request to improve Pagy
- Submit some cool extra
- Submit your translation if your language is missing from the [dictionary file](https://github.com/ddnexus/pagy/blob/master/lib/locales/pagy.yml)
- Submit your translation if your language is missing from the [dictionary files](https://github.com/ddnexus/pagy/blob/master/lib/locales)
- Write a Tutorial or a "How To" topic

## Branches and Pull Requests
Expand Down
27 changes: 15 additions & 12 deletions docs/api/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,35 @@ This method is similar to the `I18n.t` and its equivalent rails `t` helper. It i

## I18n

**IMPORTANT**: if you are using pagy with some language missing from the dictionary file, please, submit your translation!
**IMPORTANT**: if you are using pagy with some language missing from the [dictionary files](https://github.com/ddnexus/pagy/blob/master/lib/locales), please, submit your translation!

Pagy is I18n ready. That means that all its strings are stored in a [dictionary file](https://github.com/ddnexus/pagy/blob/master/lib/locales/pagy.yml), ready to be customized and/or translated/pluralized and used with or without the `I18n` gem.
Pagy is I18n ready. That means that all its strings are stored in a dictionary file of one of its [languages](https://github.com/ddnexus/pagy/blob/master/lib/locales), ready to be customized and/or translated/pluralized and used with or without the `I18n` gem.

The Pagy dictionary is a YAML file containing a few entries used in the the UI by helpers and templates through the [pagy_t method](#pagy_tpath-vars) (eqivalent to the `I18n.t` or rails `t` helper). The file follows the same structure of the standard locale files for `i18n`.
A Pagy dictionary file is a YAML file containing a few entries used in the the UI by helpers and templates through the [pagy_t method](#pagy_tpath-vars) (eqivalent to the `I18n.t` or rails `t` helper). The file follows the same structure of the standard locale files for `i18n`.

### Multi-language apps

For multi-language apps you need the dynamic translation provided by the [i18n extra](../extras/i18n.md), which delegates the handling of the pagy strings to the `I18n` gem. In that case you need only 2 steps:
For multi-language apps you need the dynamic translation provided by the [i18n extra](../extras/i18n.md), which delegates the handling of the pagy strings to the `I18n` gem. In that case you need only to require the I18n extra in the initializer file.

1. require the I18n extra in the initializer file
2. add a copy of the locale file(s) to the usual I18n dir(s), and/or add the pagy entries to the existing files

**Notice**: For simplicity, you could also use the previous 2 steps for single-language apps, but if you want more performance, please follow the specific documentation below.
**Notice**: For simplicity, you could also use the `i18n` extra for single-language apps, but if you want more performance, please follow the specific documentation below.

### Single-language apps

Single-language apps (i.e. only `fr` or only `en` or only ...) don't need to switch between languages, so they don't need the `i18n` extra/`I18n` gem (although you could choose to use it).

By default, Pagy handles its own dictionary file directly, providing pluralization and interpolation (without dynamic translation) _5x faster_ and using _3.5x less memory_ than the standard `I18n` gem.

If you need to use your own language and/or customize the Pagy strings in this scenario, you need the following steps:
If you are fine with the locales provided with pagy, you just need to load the dictionary file of your language by adding this line the initializer file. For example with `zh-cn`:

```ruby
Pagy::Frontend::I18N.load(file: Pagy.root.join('locales', 'zh-cn.yml'), language:'zh-cn')
```

If you need to use your own translation file and/or customize the Pagy strings in this scenario, you may need the following steps:

1. copy and edit the [dictionary file](https://github.com/ddnexus/pagy/blob/master/lib/locales/pagy.yml)
2. see [Adding the model translations](#adding-the-model-translations) below
3. load the dictionary file in the initializer file (e.g. `Pagy::Frontend::I18N.load(file:..., language:'en')`
1. copy and edit one of the [dictionary files](https://github.com/ddnexus/pagy/blob/master/lib/locales)
2. load it in the initializer file (e.g. `Pagy::Frontend::I18N.load(file:..., language:'tr')`
3. see [Adding the model translations](#adding-the-model-translations) below
4. check if you need to configure some of the following variables in the [pagy.rb](https://github.com/ddnexus/pagy/blob/master/lib/config/pagy.rb) initializer.

#### Pagy::Frontend::I18N Constant
Expand Down
2 changes: 1 addition & 1 deletion docs/extras/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ This helper provides an items selector UI, which allows the user to select any a

<span>Show <input type="number" min="1" max="100" value="20" style="padding: 0; text-align: center; width: 3rem;"> items per page</span>

You can change/translate its text by editing the [dictionaray YAML file](https://github.com/ddnexus/pagy/blob/master/lib/locales/pagy.yml) at the `pagy.items` path.
You can change/translate its text by editing the `pagy.items` value in the [dictionaray files](https://github.com/ddnexus/pagy/blob/master/lib/locales).

When the items number is changed with the selector, pagy will reload the pagination UI using the selected items per page. It will also request the _right_ page number calculated in order to contain the first item of the previously displayed page. That way the new displayed page will roughly show the same items in the collection before the items change.
2 changes: 1 addition & 1 deletion docs/migration-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ If the app uses the bootstrap pagination, the same CSSs should work seamlessly w

### I18n

If the app uses `I18n` you should copy and paste the entries in the [pagy.yml dictionary file](https://github.com/ddnexus/pagy/blob/master/lib/locales/pagy.yml) to the dictionaries of your app, and translate them accordingly.
If the app uses `I18n` you should follow the [I18n doc](api/frontend.md#i18n)

See also [I18n](api/frontend.md#i18n).
5 changes: 3 additions & 2 deletions lib/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@
# I18n: faster internal pagy implementation (does not use the I18n gem)
# Use only for single language apps that don't need dynamic translation between multiple languages
# See https://ddnexus.github.io/pagy/api/frontend#i18n
# Notice: Do not use the following 2 lines if you use the i18n extra below
# Pagy::Frontend::I18N.load(file:'path/to/dictionary.yml', language:'en') # load a custom file
# Notice: Do not use any of the following lines if you use the i18n extra below
# Pagy::Frontend::I18N.load(file: Pagy.root.join('locale', 'es.yml'), language:'es') # load 'es' pagy language file
# Pagy::Frontend::I18N.load(file:'path/to/dictionary.yml', language:'en') # load a custom 'en' file
# Pagy::Frontend::I18N[:plural] = -> (count) {(['zero', 'one'][count] || 'other')} # default

# I18n: Use the `I18n` gem instead of the pagy implementation
Expand Down
20 changes: 20 additions & 0 deletions lib/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
en:
pagy:
nav:
prev: "&lsaquo;&nbsp;Prev"
next: "Next&nbsp;&rsaquo;"
gap: "&hellip;"
info:
single_page:
zero: "No %{item_name} found"
one: "Displaying <b>1</b> %{item_name}"
other: "Displaying <b>all %{count}</b> %{item_name}"
multiple_pages: "Displaying %{item_name} <b>%{from}-%{to}</b> of <b>%{count}</b> in total"
item_name:
zero: "items"
one: "item"
other: "items"
compact: "Page %{page_input} of %{pages}"
items:
one: "Show %{items_input} item per page"
other: "Show %{items_input} items per page"
20 changes: 20 additions & 0 deletions lib/locales/es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
es:
pagy:
nav:
prev: "&lsaquo;&nbsp;Prev"
next: "Siguiente&nbsp;&rsaquo;"
gap: "&hellip;"
info:
single_page:
zero: "Sin resultados"
one: "Mostrando 1 %{item_name}"
other: "Mostrando %{count} %{item_name}"
multiple_pages: "Mostrando %{item_name} %{from}-%{to} de %{count} en total"
item_name:
zero: "ítems"
one: "ítem"
other: "ítems"
compact: "Página %{page_input} de %{pages}"
items:
one: "Mostrar %{items_input} ítem por página"
other: "Mostrar %{items_input} ítems por página"
20 changes: 20 additions & 0 deletions lib/locales/id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
id:
pagy:
nav:
prev: "&lsaquo;&nbsp;Balik"
next: "Lanjut&nbsp;&rsaquo;"
gap: "&hellip;"
info:
single_page:
zero: "Tidak ditemukan %{item_name}"
one: "Menampilkan <b>1</b> %{item_name}"
other: "Menampilkan <b>semua %{count}</b> %{item_name}"
multiple_pages: "Menampilkan %{item_name} <b>%{from}-%{to}</b> dari total <b>%{count}</b>"
item_name:
zero: "item"
one: "item"
other: "item"
compact: "Halaman %{page_input} dari %{pages}"
items:
one: "Tampilkan %{items_input} item per halaman"
other: "Tampilkan %{items_input} item per halaman"
20 changes: 20 additions & 0 deletions lib/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ja:
pagy:
nav:
prev: "&lsaquo;&nbsp;前へ"
next: "次へ&nbsp;&rsaquo;"
gap: "&hellip;"
info:
single_page:
zero: "見つかりませんでした"
one: "<b>1</b> 件の%{item_name}"
other: "<b>%{count}</b> 件の%{item_name}"
multiple_pages: "<b>%{count}</b> 件中 <b>%{from}-%{to}</b> 件目の%{item_name}を表示中"
item_name:
zero: "結果"
one: "結果"
other: "結果"
compact: "%{page_input} / %{pages} ページ"
items:
one: "%{items_input} 件ずつ表示"
other: "%{items_input} 件ずつ表示"
188 changes: 0 additions & 188 deletions lib/locales/pagy.yml

This file was deleted.

Loading

0 comments on commit bab3bf8

Please sign in to comment.