Load all i18n strings


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

Thank you!


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

http://gohugo.io/extras/datafiles/ ?

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.

Thank you, @moorereason and @bep.

Yes, I mean the language files inside /i18n. I was thinking on use something like getJSON or getCSV. A getYAML function could do the job:

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

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 …

Thank you, @bep.

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

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

Yeah, it’s weird.

So, the getYAML already exists? This function is a good companion to the other ones.

Thank you.

Reporting back.

To duplicate the file doesn’t work!

$: 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:

  - string

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

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" }}',

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

You can do this :slight_smile:

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

var i18n = {
  {{ range $strings }}
    "{{ . }}": "{{ i18n . }}",
  {{ end }}
1 Like

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 }}