# Load all i18n strings

**URL:** <https://discourse.gohugo.io/t/load-all-i18n-strings/4810>\
**Category:** support\
**Created:** [December 13, 2016, 2:55pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810 "2016-12-13T14:55:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [December 13, 2016, 2:55pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/1 "2016-12-13T14:55:55Z")

</div>

Hi!

Is it possible to load all i18n strings on a template to use them on a JS also?

Thank you!

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 13, 2016, 7:46pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/2 "2016-12-13T19:46:42Z")

</div>

No…

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [December 13, 2016, 7:54pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/3 "2016-12-13T19:54:51Z")

</div>

And I can’t read a YAML file, either? Only CSV and JSON? Or is there a undocumented function for it?

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [December 13, 2016, 8:33pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/4 "2016-12-13T20:33:39Z")

</div>

[http://gohugo.io/extras/datafiles/](http://gohugo.io/extras/datafiles/) ?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 13, 2016, 9:35pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/5 "2016-12-13T21:35:44Z")

</div>

> [@robsonsobral](#):
>
> And I can’t read a YAML file, either? Only CSV and JSON? Or is there a undocumented function for it?

I assume you mean the language files in /i18n. There are no undocumented function for it.

You can of course duplicate the files into /data.

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [December 13, 2016, 10:20pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/6 "2016-12-13T22:20:13Z")

</div>

Thank you, @moorereason and @bep.

Yes, I mean the language files inside `/i18n`. I was thinking on use something like [`getJSON` or `getCSV`](http://gohugo.io/extras/datadrivencontent/). A `getYAML` function could do the job:

```auto
{{ $i18nStrings := getYAML "i18n/en.yaml" }}

```

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [December 20, 2016, 5:27pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/7 "2016-12-20T17:27:42Z")

</div>

Can an `getYAML` function be a feature request?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 20, 2016, 5:42pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/8 "2016-12-20T17:42:21Z")

</div>

> [@robsonsobral](#):
>
> Can an getYAML function be a feature request?

No (or yes, sure it can) – but in this case it would be better to expose the data structure for i18n to the templates, i.e. `.Site.I18nData` or something …

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [December 20, 2016, 5:54pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/9 "2016-12-20T17:54:01Z")

</div>

Thank you, @bep.

Why not both? `.Site.I18n.Data` is a good idea, but not to have `getYAML` and (blérgh) `getTOML` is weird.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 20, 2016, 7:19pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/10 "2016-12-20T19:19:36Z")

</div>

The hack you wanted to use getYAML for is weird, not that the function does not exist.

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [December 20, 2016, 7:53pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/11 "2016-12-20T19:53:35Z")

</div>

Yeah, it’s weird.

So, the `getYAML` already exists? This function is a good companion to the [other ones](http://gohugo.io/extras/datadrivencontent/).

Thank you.

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [January 24, 2017, 2:40pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/12 "2017-01-24T14:40:29Z")

</div>

Reporting back.

To duplicate the file **doesn’t work**!

```auto
$: hugo server
Started building sites ...
Error: Error building site: Failed to read data from translations/pt.yaml/pt.yaml: yaml: unmarshal errors:
  line 1: cannot unmarshal !!seq into map[string]interface {}

```

It looks like the `$.Site.Data` doesn’t expect the format used on i18n. To make it work, I need to put all translation keys inside an array. Something like:

```
strings:
  - string
    translation

```

I was considering use Gulp to copy the file, but this bug made everything worst.

---

<div class="post-metadata">

**Author:** ![robsonsobral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robsonsobral/32/1628_2.png) [@robsonsobral](https://discourse.gohugo.io/u/robsonsobral)\
**Post date:** [January 24, 2017, 3:56pm UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/13 "2017-01-24T15:56:30Z")

</div>

My solution for now is to load the required strings one by one:

```
    <script type="text/javascript">
      var i18n = {
          'error': '{{ i18n "error" }}',
          'forcePosition': '{{ i18n "forcePosition" }}',
          'forcePositionSubmited': '{{ i18n "forcePositionSubmited" }}',
          'groupsLoadFailed': '{{ i18n "groupsLoadFailed" }}',
          'groupTracksUpdateFailed': '{{ i18n "groupTracksUpdateFailed" }}',
        };
    </script>

```

It opens the door to another issues, but… It’s the ~~less worst~~ best I can do for now.

---

<div class="post-metadata">

**Author:** ![nop33](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nop33/32/6177_2.png) [@nop33](https://discourse.gohugo.io/u/nop33)\
**Post date:** [December 20, 2018, 11:01am UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/14 "2018-12-20T11:01:41Z")

</div>

You can do this 🙂

```
<script type="text/javascript">
{{ $strings := slice "error" "forcePosition" "forcePositionSubmited" }}

var i18n = {
  {{ range $strings }}
    "{{ . }}": "{{ i18n . }}",
  {{ end }}
};
</script>
```

---

<div class="post-metadata">

**Author:** ![nop33](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nop33/32/6177_2.png) [@nop33](https://discourse.gohugo.io/u/nop33)\
**Post date:** [December 20, 2018, 11:45am UTC](https://discourse.gohugo.io/t/load-all-i18n-strings/4810/15 "2018-12-20T11:45:06Z")

</div>

You can also rename/format the i18n files to json and do the following:

```
{{ $path := printf "i18n/%s.json" .Site.Language.Lang }}
{{ $strings := getJSON $path }}
var i18n = {
  {{ range $key, $value := $strings }}
    "{{ $key }}": "{{ T $key }}",
  {{ end }}
}
```
