translate.nth()

The translate.nth() function translates element at $n index in $list with a global dictionary and/or $dictionary.

Global dictionary is in use on $dictionary-global set to true, or by setting $global argument to true.

// Sass.
@use 'sass:list';

// Functions.
@use '../translator.dictionary.function';
@use 'translate.string.function';

// Modules.
@use '../dictionary';

// The `translator.translate-nth()` or `translate.nth()` function.
@function nth($list, $n, $key: null, $dictionary: (), $global: null) {
  @if type-of($list) == list {
    $dictionary: translator.dictionary($key, $dictionary, $global);
    $nth: list.nth($list, $n);
    $translation: dictionary.translation($nth, $dictionary);
    @if type-of($nth) == list {
      @if $translation {
        @return list.set-nth($list, $n, $translation);
      } @else {
        @for $i from 1 through list.length($nth) {
          $translation: dictionary.translation(list.nth($nth, $i), $dictionary);
          @if $translation {
            $nth: list.set-nth($nth, $i, $translation);
          }
        }
        @return list.set-nth($list, $n, $nth);
      }
    }
    @return if($translation, list.set-nth($list, $n, $translation), $list);
  }
  @return $list;
}

Parameters

$list

A list in which element at given $n index is translated.

$n

An index of $list to translate.

$key: null

A key of the dictionary retrieved from a global and/or given $dictionary.

$dictionary: ()

The dictionary that is used to translate $n element in $list.

$global: null

A bool value indicates whether to use a global dictionary. Default, null, then $dictionary-global is checked.

Return

The return value is the list with a translated $n element of $list.

Examples

// Use.
@use '@angular-package/sass/translator';

// Examples.
$-dictionary-example: dictionary.merge(null, (
  (extra large, 'extra large', extra-large): xl,
  (extra small, 'extra small', extra-small): xs,
  border: b,
  class: (prefix: class-prefix, separator: class-separator, suffix: class-suffix, calendars: (calendar: cal), labels: (label: lab)),
  color: c,
  general: (word: słowo, (wrapper, wrap): owijka, (technology, technologia): tech, color: c),
  large: lg,
  medium: md,
  outline: o,
  prefix: spectre,
  small: sm,
  suffix: end,
));

// String with space.
@debug translator.translate-nth(border width 'extra small', 3); // border width xs

// String with delimiter.
@debug translator.translate-nth(border width extra-large, 3); // border width xl

// Translate list as phrase.
@debug translator.translate-nth(border width (extra small), 3); // border width xs

// Translate list as string.
@debug translator.translate-nth(border width (extra-small, 'extra large'), 3); // border width (xs, xl)

// `$global` dictionary true
@debug translator.translate-nth((prefix, suffix), 2, class); // prefix, class-suffix
@debug translator.translate-nth((prefix, name, suffix), 1, class); // class-prefix, name, suffix

// `$global` dictionary false
@debug translator.translate-nth((prefix, suffix), 1, class, $global: false); // prefix, suffix

// List.
@debug translator.translate-nth(((prefix, suffix), suffix), 1, class); // (class-prefix, class-suffix), suffix
@debug translator.translate-nth(((prefix, suffix, (prefix, suffix)), suffix), 1, class); // (class-prefix, class-suffix, (prefix, suffix)), suffix

// Custom `$dictionary`.
@debug translator.translate-nth((prefix, suffix), 1, null, $-dictionary-example class); // class-prefix, suffix
@debug translator.translate-nth((prefix, suffix), 1, class, (test: (class: (prefix: spectre-prefix))) test, false); // spectre-prefix, suffix
@debug translator.translate-nth((wrapper, label, ), 1, null, $-dictionary-example general); // owijka, label
@debug translator.translate-nth((wrapper, label, color), 3, general, $-dictionary-example); // wrapper, label, c
@debug translator.translate-nth((wrapper, label, color), 3, $dictionary: $-dictionary-example general); // wrapper, label, c

Last updated