# How to dynamically change the visible captions?

When working with the captions / text-track options, you can dynamically change the currently selected language (or set a default language) using following methods.

This method assumes that you are using text-tracks that are loaded with the manifest. If you add the text-track separately, you can just use this API: [TextTrackDescription](https://optiview.dolby.com/docs/theoplayer/v10/api-reference/web/interfaces/TextTrackDescription)

## Step-by-step guide

### 1. Create a new function inside the browser

##### Web SDK

```js
function setLanguage(player, language) {
  // Disable all text tracks that are currently active
  player.textTracks
    .filter(function (x) {
      if (x.mode !== 'disabled') {
        return x;
      }
    })
    .forEach(function (x) {
      x.mode = 'disabled';
    });
  // Enable the text track for a specific language. Note: Here we are searching the label. You can also do x.language for the ISO 3 letter language code.
  player.textTracks.filter(function (x) {
    if (x.label == language) {
      return x;
    }
  })[0].mode = 'showing';
}
```

##### iOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x)

```swift
private func setLanguage(player: THEOplayer, language: String) {
    let list: TextTrackList = player.textTracks
    for i in 0..<list.count {
        var textTrack: TextTrack = list[i]
        if textTrack.label == language {
            textTrack.mode = .showing
        }
    }
}
```

##### Roku SDK

In the following example we are iterating through all available text tracks. Once we found a track with a desired language, we simply set the mode to "showing". After that, we have to simply assign textTracks object to a proper THEOplayer attribute. This is all you have to do to make a desired text track visible. Of course, we can modify a visible text track during the video playback.

```brightscript

function setCaptionsLanguage(language as String)
  textTracks = m.player.textTracks
  for i = textTracks.count() - 1 to 0 step -1
    if textTracks[i].kind = "captions" and textTracks[i].language = language then
      if m.fullScreen then
        textTracks[i].mode = "showing"
      else
        textTracks[i].mode = "hidden"
      end if
    else
      textTracks[i].mode = "disabled"
    end if
  end for
  'assignment of new roAssociativeArray is required because roku deep-copied roAssociativeArray through fields (pass-by-value)
  'read more : <https://developer.roku.com/en-gb/docs/developer-program/performance-guide/optimization-techniques.md#OptimizationTechniques-DataFlow>
  m.player.textTracks = textTracks
end function
```

### 2. Call the function using the language label (or 3-letter-language code if you change x.label to x.language)

## Related articles

* [How to dynamically change the visible captions?](https://optiview.dolby.com/docs/theoplayer/v10/how-to-guides/texttrack/how-to-dynamically-change-the-visible-captions.md)
* [How to add CSS or JavaScript files to an Android/iOS project](https://optiview.dolby.com/docs/theoplayer/v4/faq/how-to-add-css-or-javascript-files-to-android-ios)
* [Web SDK](https://optiview.dolby.com/docs/theoplayer/v10/getting-started/sdks/web/getting-started.md)
* [Android Legacy (4.12.x) SDK](https://optiview.dolby.com/docs/theoplayer/v4/getting-started/sdks/android/getting-started)
* [Chromecast Receiver SDK](https://optiview.dolby.com/docs/theoplayer/v10/getting-started/sdks/chromecast/getting-started.md)
