Pull merge.
[yaffs-website] / web / core / modules / language / src / LanguageNegotiatorInterface.php
1 <?php
2
3 namespace Drupal\language;
4
5 use Drupal\Core\Session\AccountInterface;
6
7 /**
8  * Common interface for language negotiation services.
9  *
10  * The language negotiation API is based on two major concepts:
11  * - Language types: types of translatable data (the types of data that a user
12  *   can view or request).
13  * - Language negotiation methods: responsible for determining which language to
14  *   use to present a particular piece of data to the user.
15  * Both language types and language negotiation methods are customizable.
16  *
17  * Drupal defines three built-in language types:
18  * - Interface language: The page's main language, used to present translated
19  *   user interface elements such as titles, labels, help text, and messages.
20  * - Content language: The language used to present content that is available
21  *   in more than one language.
22  * - URL language: The language associated with URLs. When generating a URL,
23  *   this value will be used for URL's as a default if no explicit preference is
24  *   provided.
25  * Modules can define additional language types through
26  * hook_language_types_info(), and alter existing language type definitions
27  * through hook_language_types_info_alter().
28  *
29  * Language types may be configurable or fixed. The language negotiation
30  * methods associated with a configurable language type can be explicitly
31  * set through the user interface. A fixed language type has predetermined
32  * (module-defined) language negotiation settings and, thus, does not appear in
33  * the configuration page. Here is a code snippet that makes the content
34  * language (which by default inherits the interface language's values)
35  * configurable:
36  * @code
37  * function mymodule_language_types_info_alter(&$language_types) {
38  *   unset($language_types[LanguageInterface::TYPE_CONTENT]['fixed']);
39  * }
40  * @endcode
41  *
42  * The locked configuration property prevents one language type from being
43  * switched from customized to not customized, and vice versa.
44  * @see \Drupal\language\LanguageNegotiator::updateConfiguration()
45  *
46  * Every language type can have a different set of language negotiation methods
47  * assigned to it. Different language types often share the same language
48  * negotiation settings, but they can have independent settings if needed. If
49  * two language types are configured the same way, their language switcher
50  * configuration will be functionally identical and the same settings will act
51  * on both language types.
52  *
53  * Drupal defines the following built-in language negotiation methods:
54  * - URL: Determine the language from the URL (path prefix or domain).
55  * - Session: Determine the language from a request/session parameter.
56  * - User: Follow the user's language preference.
57  * - User admin language: Identify admin language from the user preferences.
58  * - Browser: Determine the language from the browser's language settings.
59  * - Selected language: Use the default site language.
60  * Language negotiation methods are simple plugin classes that implement a
61  * particular logic to return a language code. For instance, the URL method
62  * searches for a valid path prefix or domain name in the current request URL.
63  * If a language negotiation method does not return a valid language code, the
64  * next method associated with the language type (based on method weight) is
65  * invoked.
66  *
67  * Modules can define additional language negotiation methods by simply provide
68  * the related plugins, and alter existing methods through
69  * hook_language_negotiation_info_alter(). Here is an example snippet that lets
70  * path prefixes be ignored for administrative paths:
71  * @code
72  * function mymodule_language_negotiation_info_alter(&$negotiation_info) {
73  *   // Replace the original plugin with our own implementation.
74  *   $method_id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::METHOD_ID;
75  *   $negotiation_info[$method_id]['class'] = 'Drupal\my_module\Plugin\LanguageNegotiation\MyLanguageNegotiationUrl';
76  * }
77  *
78  * class MyLanguageNegotiationUrl extends LanguageNegotiationUrl {
79  *   public function getCurrentLanguage(Request $request = NULL) {
80  *     if ($request) {
81  *       // Use the original URL language negotiation method to get a valid
82  *       // language code.
83  *       $langcode = parent::getCurrentLanguage($request);
84  *
85  *       // If we are on an administrative path, override with the default
86  *       language.
87  *       if ($request->query->has('q') && strtok($request->query->get('q'), '/') == 'admin') {
88  *         return $this->languageManager->getDefaultLanguage()->getId();
89  *       }
90  *       return $langcode;
91  *     }
92  *   }
93  * }
94  * @endcode
95  *
96  * For more information, see
97  * @link https://www.drupal.org/node/1497272 Language Negotiation API @endlink
98  */
99 interface LanguageNegotiatorInterface {
100
101   /**
102    * The language negotiation method id for the language negotiator itself.
103    */
104   const METHOD_ID = 'language-default';
105
106   /**
107    * Resets the negotiated languages and the method instances.
108    */
109   public function reset();
110
111   /**
112    * Sets the current active user and resets all language types.
113    *
114    * @param \Drupal\Core\Session\AccountInterface $current_user
115    *   The current active user.
116    */
117   public function setCurrentUser(AccountInterface $current_user);
118
119   /**
120    * Initializes the specified language type.
121    *
122    * @param string $type
123    *   The language type to be initialized.
124    *
125    * @return \Drupal\Core\Language\LanguageInterface[]
126    *   Returns an array containing a single language keyed by the language
127    *   negotiation method ID used to determine the language of the specified
128    *   type. If negotiation is not possible the default language is returned.
129    */
130   public function initializeType($type);
131
132   /**
133    * Returns the language negotiation methods enabled for a language type.
134    *
135    * @param string $type
136    *   (optional) The language type. If no type is specified all the method
137    *   definitions are returned.
138    *
139    * @return array[]
140    *   An array of language negotiation method definitions keyed by method id.
141    */
142   public function getNegotiationMethods($type = NULL);
143
144   /**
145    * Returns an instance of the specified language negotiation method.
146    *
147    * @param string $method_id
148    *   The method identifier.
149    *
150    * @return \Drupal\language\LanguageNegotiationMethodInterface
151    */
152   public function getNegotiationMethodInstance($method_id);
153
154   /**
155    * Returns the ID of the language type's primary language negotiation method.
156    *
157    * @param string $type
158    *   The language type.
159    *
160    * @return string
161    *   The identifier of the primary language negotiation method for the given
162    *   language type, or the default method if none exists.
163    */
164   public function getPrimaryNegotiationMethod($type);
165
166   /**
167    * Checks whether a language negotiation method is enabled for a language type.
168    *
169    * @param string $method_id
170    *   The language negotiation method ID.
171    * @param string $type
172    *   (optional) The language type. If none is passed, all the configurable
173    *   language types will be inspected.
174    *
175    * @return bool
176    *   TRUE if the method is enabled for at least one of the given language
177    *   types, or FALSE otherwise.
178    */
179   public function isNegotiationMethodEnabled($method_id, $type = NULL);
180
181   /**
182    * Saves a list of language negotiation methods for a language type.
183    *
184    * @param string $type
185    *   The language type.
186    * @param int[] $enabled_methods
187    *   An array of language negotiation method weights keyed by method ID.
188    */
189   public function saveConfiguration($type, $enabled_methods);
190
191   /**
192    * Resave the configuration to purge missing negotiation methods.
193    */
194   public function purgeConfiguration();
195
196   /**
197    * Updates the configuration based on the given language types.
198    *
199    * Stores the list of the language types along with information about their
200    * configurable state. Stores the default settings if the language type is
201    * not configurable.
202    *
203    * @param string[] $types
204    *   An array of configurable language types.
205    */
206   public function updateConfiguration(array $types);
207
208 }