• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

lib/H19-Feb-2018-4,6483,714

src/H19-Feb-2018-7560

test/H19-Feb-2018-3,1602,533

.formatter.exsH A D19-Feb-2018126 54

.gitignoreH A D19-Feb-201878 98

.travis.ymlH A D19-Feb-2018238 1713

CHANGELOG.mdH A D19-Feb-20185.8 KiB12896

README.mdH A D19-Feb-20184.8 KiB12986

mix.exsH A D19-Feb-20181.1 KiB5546

mix.lockH A D19-Feb-2018298 54

README.md

1# Gettext
2
3[![Build Status](https://travis-ci.org/elixir-lang/gettext.svg)](https://travis-ci.org/elixir-lang/gettext)
4
5Gettext is an **internationalization** (i18n) and **localization** (l10n) system commonly used for writing multilingual programs. Gettext is a standard for i18n in different communities, meaning there is a great set of tooling for developers and translators. This project is an implementation of the Gettext system in Elixir.
6
7## Installation
8
9  1. Add `:gettext` to your list of dependencies in `mix.exs` (use `$ mix hex.info gettext` to find the latest version):
10
11     ```elixir
12     def deps do
13       [{:gettext, ">= 0.0.0"}]
14     end
15     ```
16
17  2. Optionally add the `:gettext` application to your `:applications` key if you're not using `:extra_applications`.
18
19  3. Optionally add the `:gettext` compiler to your Mix compilers so your backends are recompiled when `.po` files change:
20
21     ```elixir
22     def project do
23       [compilers: [:gettext] ++ Mix.compilers()]
24     end
25     ```
26
27[Documentation for `Gettext` is available on Hex][docs-gettext].
28
29## Usage
30
31To use Gettext, you must define a Gettext module:
32
33```elixir
34defmodule MyApp.Gettext do
35  use Gettext, otp_app: :my_app
36end
37```
38
39And invoke the Gettext API, which consists of the `*gettext` macros:
40
41```elixir
42import MyApp.Gettext
43
44# Simple translation
45gettext("Here is one string to translate")
46
47# Plural translation
48number_of_apples = 4
49ngettext("The apple is ripe", "The apples are ripe", number_of_apples)
50
51# Domain-based translation
52dgettext("errors", "Here is an error message to translate")
53```
54
55Translations in Gettext are stored in Portable Object files (`.po`). Such files must be placed at `priv/gettext/LOCALE/LC_MESSAGES/DOMAIN.po`, where `LOCALE` is the locale and `DOMAIN` is the domain (the default domain is called `default`).
56
57For example, the translation to `pt_BR` of the first two `*gettext` calls in the snippet above must be placed in the `priv/gettext/pt_BR/LC_MESSAGES/default.po` file with contents:
58
59```pot
60msgid "Here is one string to translate"
61msgstr "Aqui está um texto para traduzir"
62
63msgid "Here is the string to translate"
64msgid_plural "Here are the strings to translate"
65msgstr[0] "Aqui está o texto para traduzir"
66msgstr[1] "Aqui estão os textos para traduzir"
67```
68
69`.po` are text-based files and can be edited directly by translators. Some may even use existing tools for managing them, such as [Poedit][poedit] or [poeditor.com][poeditor.com].
70
71Finally, because translations are based on strings, your source code does not lose readability as you still see literal strings, like `gettext "here is an example"`, instead of paths like `translate "some.path.convention"`.
72
73Read the [documentation for the `Gettext` module][docs-gettext-module] for more information on locales, interpolation, pluralization, and other features.
74
75## Workflow
76
77Gettext is able to automatically extract translations from your source code, alleviating developers and translators from the repetitive and error-prone work of maintaining translation files.
78
79When extracted from source, translations are placed into `.pot` files, which are template files. Those templates files can then be merged into translation files for each specific locale your application is being currently translated to.
80
81In other words, the typical workflow looks like this:
82
83  1. Add `gettext` calls to your source code. No need to touch translation files
84     at this point as Gettext will return the given string if no translation is
85     available:
86
87     ```elixir
88     gettext("Welcome back!")
89     ```
90
91  2. Once changes to the source are complete, automatically sync all existing entries to `.pot` (template files) in `priv/gettext` by running:
92
93     ```bash
94     mix gettext.extract
95     ```
96
97  3. `.pot` files can then be merged into locale-specific `.po` files:
98
99     ```bash
100     # Merge .pot into all locales
101     mix gettext.merge priv/gettext
102
103     # Merge .pot into one specific locale
104     mix gettext.merge priv/gettext --locale en
105     ```
106
107It is also possible to both extract and merge translations in one step with `mix gettext.extract --merge`.
108
109## License
110
111Copyright 2015 Plataformatec
112
113  Licensed under the Apache License, Version 2.0 (the "License");
114  you may not use this file except in compliance with the License.
115  You may obtain a copy of the License at
116
117      http://www.apache.org/licenses/LICENSE-2.0
118
119  Unless required by applicable law or agreed to in writing, software
120  distributed under the License is distributed on an "AS IS" BASIS,
121  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122  See the License for the specific language governing permissions and
123  limitations under the License.
124
125[docs-gettext]: http://hexdocs.pm/gettext
126[docs-gettext-module]: http://hexdocs.pm/gettext/Gettext.html
127[poedit]: http://poedit.net/
128[poeditor.com]: https://poeditor.com
129