> For the complete documentation index, see [llms.txt](https://developers.weglot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.weglot.com/cms/javascript/link-hooks.md).

# Language link Hooks

Using Weglot links hooks to create your own language switcher

By default, Weglot will show a language switcher on your page. Either on the bottom-right side or where it has been set through the `switchers` options. In some cases, you might want to not use Weglot's switcher at all and use menu entries from the CMS you're integrating in. You might also want to create your own switcher and not use Weglot's CSS.

When Weglot gets initialized, every link that matches one of these two CSS selectors will be "hooked" to a `Weglot.switchTo` action automatically:

* `a[href='#Weglot-xx']`
* `a[href$='change-language.weglot.com/xx']`

... where `xx` is the ISO-639-1 code of the target language

When the links are "hooked", the original `href` attribute also gets removed, and the class `weglot-link` is added to them. The current language also has the class `weglot-link--active`

### Example - Anchor

I want to use the native menu feature of my CMS. Let's say my original language is English, and I want to translate to French and Spanish.

My menu would have 3 entries, as follows:

| **Text** | **Link**   |
| -------- | ---------- |
| English  | #Weglot-en |
| Français | #Weglot-fr |
| Español  | #Weglot-es |

I have now a "native" language switcher that works for all these languages. If I need to apply specific styling to the current language, I can target the `weglot-link--active`class.

### Example - Fake link

Sometimes, some CMS you will be using won't allow for anchor links in the menu (e.g. Wix). In this case, you can use fake links that will be replaced when Weglot initializes:

| **Text** | **Link**                               |
| -------- | -------------------------------------- |
| English  | <http://change-language.weglot.com/en> |
| Français | <http://change-language.weglot.com/fr> |

## Example: create your own switcher

```javascript
function() {
    // CHANGE THIS SELECTOR to the element you want to add your custom switcher to.
    var myDiv = document.getElementById("myDiv");
    
    if (!Weglot) {
        return;
    }

    //Create array of options to be added
    var availableLanguages = Weglot.options.languages
        .map(function(language) {
            return language.language_to;
        })
        .concat(Weglot.options.language_from);
    
    //Create and append select list
    var selectList = document.createElement("select");
    myDiv.appendChild(selectList);
    
    var currentLang = Weglot.getCurrentLang();
    
    //Create and append the options
    for (var i = 0; i < availableLanguages.length; i++) {
        var lang = availableLanguages[i];
        var option = document.createElement("option");
        option.value = lang;
        option.text = Weglot.getLanguageName(lang);
        if (lang === currentLang) {
            option.selected = "selected";        
        }
        selectList.appendChild(option);
    }
    
    selectList.onchange = function(){
        Weglot.switchTo(this.value);
    };
    
    Weglot.on("languageChanged", function(lang) {
        selectList.value = lang;
    });
}()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.weglot.com/cms/javascript/link-hooks.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
