Sha256: 425e0b818fffef6779e76c16f834b647d281f7643bdf516cefe7bd821ca0bb9f

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

# How Locales Work

The default locale is English. You can see how it is implemented in the "translate" method in [Faker.rb](/lib/faker.rb).

Here's how to set it:

```
# Sets the locale to "Simplified Chinese":

Faker::Config.locale = 'zh-CN'
```

It works so that once the Faker locale is set to a different location, the translate method will check that .yml file for an equivalent and use that data. If it doesn't exist, it defaults back to English. It uses the "i18n" gem to do this.

Using Chinese as an example, when the locale is set to Chinese and you attempt to call for hipster ipsem (which doesn't exist at the time of this writing), you will get English back. It checks the "zh-CH.yml" file, does not find "hipster" and then checks the "en.yml" file and returns a word from that array.

```
Faker::Config.locale = 'zh-CN'

Faker::Hipster.word #=> "kogi"
```

In order to update a locale with more translation features, simply add a new field to the .yml file that corresponds to an existing piece of functionality in the "en.yml" file. In this example, that would mean providing Chinese hipster words.

```
# /lib/locales/zh-CN.yml

hipster: ["屌丝"]

# Now this should work:

Faker::Hipster.word #=> "屌丝"
```

After you've done that, find or create a test file for the locale you've updated and test the functionality for that language.

In our hypothetical example here, one would add something like this to the "test-zh-locale.rb" file in the "test_ch_methods" method:

```
assert Faker::Hipster.word.is_a? String
```

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
faker-1.9.1 lib/locales/README.md
faker-1.8.7 lib/locales/README.md
faker-1.8.6 lib/locales/README.md
faker-1.8.5 lib/locales/README.md
faker-1.8.4 lib/locales/README.md
faker-1.8.3 lib/locales/README.md