# Anyone have a ref shortcode that auto-generates a link with multilingual Title support?

**URL:** <https://discourse.gohugo.io/t/anyone-have-a-ref-shortcode-that-auto-generates-a-link-with-multilingual-title-support/37800>\
**Category:** support\
**Created:** [March 22, 2022, 7:28am UTC](https://discourse.gohugo.io/t/anyone-have-a-ref-shortcode-that-auto-generates-a-link-with-multilingual-title-support/37800 "2022-03-22T07:28:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![techmagus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/techmagus/32/16997_2.png) [@techmagus](https://discourse.gohugo.io/u/techmagus)\
**Post date:** [March 22, 2022, 7:28am UTC](https://discourse.gohugo.io/t/anyone-have-a-ref-shortcode-that-auto-generates-a-link-with-multilingual-title-support/37800/1 "2022-03-22T07:28:40Z")

</div>

I am looking for a way to use ref/relref shortcode to auto-generate as a link with the correct Title automatically pulled.

(Test repo: [Test repo](https://github.com/techmagus/hugo-test-site))

Currently we have to do this manually:

```auto
[title]({{< ref "somefile.md" >}})

```

or in a multilingual site:

```auto
[title]({{< ref path="somefile.md" lang="ja" >}})

```

The more cross-referencing you have, the harder it is to track, and edit (if needed). So, I’m thinking of creating a new shortcode for just this purpose.

However, there is no title params for the `ref` function, there are only `path`, `lang`, and `outputFormat`.

I also tried:

```auto
{{ with .Site.GetPage "somefile" }}{{ .Title }}{{ end }}

```

But it only pulls information from the exact same language the viewed page is set to. So if the said page is in “ko”, then `.Site.GetPage` will only search ‘ko’ related files.

For example, this does not work:

```auto
{{ with .Site.GetPage "/ja/kb/webdev/test-internal.md" }}{{ .Title }}{{ end }}

```

But without the lang folder, it works but will pull the same language as the current page:

```auto
{{ with .Site.GetPage "/kb/webdev/test-internal.md" }}{{ .Title }}{{ end }}

```

Sample code in .md files

```auto
* {{< reftitle path="reftitle-test.md" lang="en-ph" >}}
* {{< reftitle path="reftitle-test.md" lang="ja" >}}
* {{< reftitle path="reftitle-test.md" lang="ko" >}}

```

I currently have the following in my `reftitle.html` shortcode

```auto
{{- $lang := .Get "lang" -}}
{{- $path := .Get "path" -}}
{{- $ref := relref . .Params | urlize -}}
<a href="{{ $ref }}">{{ with .Site.GetPage $ref }}{{ .Title }}{{ end }}</a>

```

- The links are correct since it is using the `ref`.
- However, I can not pull the appropriate link title.
  - In the example code above, it is empty because it can not find the referenced file because `$ref` value is `/ja/kb/webdev/test-internal.md`

Test repo: [Test repo](https://github.com/techmagus/hugo-test-site)

Or, I’m approaching this incorrectly. ^^;;

Thank you, shalom!

---

<div class="post-metadata">

**Author:** ![frjo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frjo/32/18626_2.png) [@frjo](https://discourse.gohugo.io/u/frjo)\
**Post date:** [March 22, 2022, 9:00am UTC](https://discourse.gohugo.io/t/anyone-have-a-ref-shortcode-that-auto-generates-a-link-with-multilingual-title-support/37800/2 "2022-03-22T09:00:35Z")

</div>

Shortcode, I named it “reflink.html”

```auto
{{ with site.GetPage (.Get 0) }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}

```

Use it like this:

`{{< reflink "somefile.md" >}}`

And it will output:

`<a href="/path/to/somefile/">Some files title</a>`

Read more at [.GetPage | Hugo](https://gohugo.io/functions/getpage/).

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [March 22, 2022, 9:19am UTC](https://discourse.gohugo.io/t/anyone-have-a-ref-shortcode-that-auto-generates-a-link-with-multilingual-title-support/37800/3 "2022-03-22T09:19:07Z")

</div>

> [@techmagus](#):
>
> I am looking for a way to use ref/relref shortcode to auto-generate as a link with the correct Title automatically pulled.

I happen to have written about ~~that exact~~ a similar topic recently – the `titleref` shortcode that I describe accepts the page title, and it figures out the associated `RelPermalink` internally 😃

> **[titleref: Referencing Hugo posts by their titles](https://scripter.co/titleref-referencing-hugo-posts-by-their-titles/)**
>
> A custom Hugo shortcode to create cross-references to other posts
> using their title strings.

See if that helps you. I don’t show the `lang` support. But you should be able to build upon the idea I present there.

---

<div class="post-metadata">

**Author:** ![techmagus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/techmagus/32/16997_2.png) [@techmagus](https://discourse.gohugo.io/u/techmagus)\
**Post date:** [March 22, 2022, 10:23am UTC](https://discourse.gohugo.io/t/anyone-have-a-ref-shortcode-that-auto-generates-a-link-with-multilingual-title-support/37800/4 "2022-03-22T10:23:22Z")

</div>

Ooh, found a way to do it!

I was looking into @kaushalmodi’s titleref and it made me remember `.Translations`

Here’s the new code for the shortcode file:

```auto
{{- $path := .Get "path" -}}
{{- $ref := relref . .Params | urlize -}}
{{ with .Site.GetPage $path }}
  {{ range .AllTranslations }}
    {{ if eq $ref .RelPermalink }}<a href="{{ .RelPermalink }}">{{ .Title }}</a>{{ break }}{{ end }}
  {{ end }}
{{ end }}

```

Having the following in your .md file:

```auto
* {{< reflangtitle path="reftitle-test.md" lang="en-ph" >}}
* {{< reflangtitle path="reftitle-test.md" lang="ja" >}}
* {{< reflangtitle path="reftitle-test.md" lang="ko" >}}

```

Will now generate the proper multilingual title and automatic linking.

I also updated the test site source code: [Test source](https://github.com/techmagus/hugo-test-site).

With this shortcode, it is now possible:

1. use `ref` with an automatic link generation
2. the generated link pulls the multilingual title regardless of the current page’s `lang`

Useful when referencing a post written only in one language from a page/post written in a different language.

Example: if you have a Chinese article and you want to link to another article which is only available in English, you can use the shortcode to autogenerate the link and the title (you don’t have to open that article and copy-paste the title into your new content).

By the way, thank you for the `{{ break }}` feature, without it there are extra lines generated, very useful.

Thank you all. Shalom!
