Sha256: c802be4212a5f5556e05a90787cab42bc72223e8257727bee2d69a7cea7144c5

Contents?: true

Size: 1.53 KB

Versions: 43

Compression:

Stored size: 1.53 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:

```ruby
# 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.

```ruby
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.

```yaml
# /lib/locales/zh-CN.yml
hipster:
    - "屌丝"
```

```ruby
# 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:

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

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
faker-3.2.0 lib/locales/README.md
faker-3.1.1 lib/locales/README.md
faker-3.1.0 lib/locales/README.md
faker-3.0.0 lib/locales/README.md
faker-2.23.0 lib/locales/README.md
faker-2.22.0 lib/locales/README.md
faker-2.21.0 lib/locales/README.md
faker-2.20.0 lib/locales/README.md
faker-2.19.0 lib/locales/README.md
logstash-output-scalyr-0.1.7 vendor/bundle/jruby/2.5.0/bundler/gems/faker-e1bd4a5a5777/lib/locales/README.md
faker-2.18.0 lib/locales/README.md
faker-2.17.0 lib/locales/README.md
faker-2.16.0 lib/locales/README.md
faker-2.15.1 lib/locales/README.md
faker-2.15.0 lib/locales/README.md
faker-2.14.0 lib/locales/README.md
faker-2.13.0 lib/locales/README.md
faker-2.12.0 lib/locales/README.md
faker-2.11.0 lib/locales/README.md
faker-2.10.2 lib/locales/README.md