Class: TranslationIO::API::Segments

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_io/api/segments.rb

Overview

Client for segment endpoints

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ Segments

Returns a new instance of Segments.



7
8
9
10
# File 'lib/translation_io/api/segments.rb', line 7

def initialize(api_key:)
  @api_key = api_key
  @base_url = "https://translation.io/api/v1/segments"
end

Instance Method Details

#add_tag(segment_id, name:) ⇒ TranslationIO::API::Response

Note:

a unique color will be attributed to the tag. If this tag already exists in the project, the same color will be reused.

Note:

if this tag is already added to the segment, nothing will change and no error will be triggered.

Add a tag to a segment

Parameters:

  • segment_id (String, Integer)

    The ID of the segment

  • name (String)

    The name of the tag to add

Returns:



73
74
75
# File 'lib/translation_io/api/segments.rb', line 73

def add_tag(segment_id, name:)
  request.post(@base_url + "/#{segment_id}/add_tag", { name: name })
end

#create(target_language, type:, key:, source:) ⇒ TranslationIO::API::Response

Create a segment

Parameters:

  • target_language (String)

    Target language code (from translation.io/docs/languages). Example: “es”

  • type (String)

    Segment type. Should be “key” or “source”.

  • key (String)

    Key associated with the text to be translated. Only for “key” type. Example: “home.call_to_action.bottom”

  • source (String)

    Source text. Example: “Click here to subscribe”

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'lib/translation_io/api/segments.rb', line 49

def create(target_language, type:, key:, source:)
  request.post(
    @base_url, {
      target_language: target_language,
      type: type,
      key: key,
      source: source
    }
  )
end

#list(target_language, options = {}) ⇒ TranslationIO::API::Response

List segments for target language

Parameters:

  • target_language (String)

    Target language code (from translation.io/docs/languages). Example: “es”

  • options (Hash) (defaults to: {})

    optional params to pass with the call to list

Options Hash (options):

  • :query (String)

    Query segments to returns only the ones that contain query value in their key, source, target (with plurals), context, comment or references properties. Note: that query is case-insensitive.

  • :type (String) — default: "all"

    Filter by type of segments. Should be key, source or all.

  • :state (String) — default: "all"

    Filter by state of segments. Should be translated, untranslated or all.

  • :tag (String)

    Filter by tag. Only returns segments that are tagged with tag value.

Returns:



30
31
32
33
34
35
# File 'lib/translation_io/api/segments.rb', line 30

def list(target_language, options = {})
  request.get(
    @base_url,
    options.merge("target_language": target_language)
  )
end

#remove_tag(segment_id, name:) ⇒ TranslationIO::API::Response

Note:

if this tag doesn’t exist or is not linked to this segment, nothing will change and no error will be triggered.

Remove a tag from a segment

Parameters:

  • segment_id (String, Integer)

    The ID of the segment

  • name (String)

    The name of the tag to remove

Returns:



88
89
90
# File 'lib/translation_io/api/segments.rb', line 88

def remove_tag(segment_id, name:)
  request.post(@base_url + "/#{segment_id}/remove_tag", { name: name })
end