summaryrefslogtreecommitdiffstats
path: root/testing/marionette/puppeteer/firefox/firefox_puppeteer/api/l10n.py
blob: f7f52918c356a39728543ecd8a45611423d20eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

# -----------------
# DEPRECATED module
# -----------------
# Replace its use in tests when Firefox 45 ESR support ends with
# marionette_driver.localization.L10n

import copy

from marionette_driver.errors import (
    NoSuchElementException,
    UnknownCommandException,
)
from marionette_driver.localization import L10n as L10nMarionette

from firefox_puppeteer.base import BaseLib


class L10n(BaseLib):
    """An API which allows Marionette to handle localized content.

    .. deprecated:: 52.2.0
       Use the localization module from :py:mod:`marionette_driver` instead.

    The `localization`_ of UI elements in Gecko based applications is done via
    entities and properties. For static values entities are used, which are located
    in .dtd files. Whereby for dynamically updated content the values come from
    .property files. Both types of elements can be identifed via a unique id,
    and the translated content retrieved.

    .. _localization: https://mzl.la/2eUMjyF
    """

    def __init__(self, marionette):
        super(L10n, self).__init__(marionette)

        self._l10nMarionette = L10nMarionette(self.marionette)

    def localize_entity(self, dtd_urls, entity_id):
        """Returns the localized string for the specified DTD entity id.

        To find the entity all given DTD files will be searched for the id.

        :param dtd_urls: A list of dtd files to search.
        :param entity_id: The id to retrieve the value from.

        :returns: The localized string for the requested entity.

        :raises NoSuchElementException: When entity id is not found in dtd_urls.
        """
        # Add xhtml11.dtd to prevent missing entity errors with XHTML files
        try:
            return self._l10nMarionette.localize_entity(dtd_urls, entity_id)
        except UnknownCommandException:
            dtds = copy.copy(dtd_urls)
            dtds.append("resource:///res/dtd/xhtml11.dtd")

            dtd_refs = ''
            for index, item in enumerate(dtds):
                dtd_id = 'dtd_%s' % index
                dtd_refs += '<!ENTITY %% %s SYSTEM "%s">%%%s;' % \
                    (dtd_id, item, dtd_id)

            contents = """<?xml version="1.0"?>
                <!DOCTYPE elem [%s]>

                <elem id="entity">&%s;</elem>""" % (dtd_refs, entity_id)

            with self.marionette.using_context('chrome'):
                value = self.marionette.execute_script("""
                    var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
                                 .createInstance(Components.interfaces.nsIDOMParser);
                    var doc = parser.parseFromString(arguments[0], "text/xml");
                    var node = doc.querySelector("elem[id='entity']");

                    return node ? node.textContent : null;
                """, script_args=[contents])

            if not value:
                raise NoSuchElementException('DTD Entity not found: %s' % entity_id)

            return value

    def localize_property(self, property_urls, property_id):
        """Returns the localized string for the specified property id.

        To find the property all given property files will be searched for
        the id.

        :param property_urls: A list of property files to search.
        :param property_id: The id to retrieve the value from.

        :returns: The localized string for the requested entity.

        :raises NoSuchElementException: When property id is not found in
            property_urls.
        """
        try:
            return self._l10nMarionette.localize_property(property_urls, property_id)
        except UnknownCommandException:
            with self.marionette.using_context('chrome'):
                value = self.marionette.execute_script("""
                    let property = null;
                    let property_id = arguments[1];

                    arguments[0].some(aUrl => {
                      let bundle = Services.strings.createBundle(aUrl);

                      try {
                        property = bundle.GetStringFromName(property_id);
                        return true;
                      }
                      catch (ex) { }
                    });

                    return property;
                """, script_args=[property_urls, property_id])

            if not value:
                raise NoSuchElementException('Property not found: %s' % property_id)

            return value