Commit graph

22 commits

Author SHA1 Message Date
Amir Sarabadani
92abd6cd1b Language: Mark constructor of LanguageCode unstable for now
Since the branch cut is now, let's make this unstable for now and then
change it once we are confident it'll be stable.

Bug: T376565
Change-Id: Id7dd735abc5a97a0dfbd50484a8ae989b6c1738f
2024-10-22 01:39:37 +02:00
Amir Sarabadani
c65e133f18 Language: Introduce getContentLanguageCode() service
And use that as much as possible. This makes bootstraping services much
faster since Language object is extremely heavy (specially to
initialize) and that's why in LanguageFactory it's behind a cache (see
LanguageFactory::getRawLanguage)

At the same time, most uses of Language object only need the language
code (after normalization and mapping and validation) and this allows
such change to happen fast and way more decoupled from Language object
that has more than 100 methods.

Bug: T376565
Change-Id: I1c3d94454896842939dfaad8cbf742e5a3ae5438
2024-10-16 11:02:01 +02:00
James D. Forrester
bc662aec9b Move Language and friends into Language namespace
Bug: T353458
Change-Id: Id3202c0c4f4a2043bf97b7caee081acab684155c
2024-08-10 13:36:30 +02:00
C. Scott Ananian
87534b827a [doc] Update filename in LanguageCode::getDeprecatedCodeMapping() comment
Followup-To: 7fe63e48c5
Change-Id: I2de451c07451bc5c8aa5b93b361d5bd2a3577fe5
2024-04-11 14:26:07 +00:00
Nikki
f572f4c159 Make isWellFormedLanguageTag() actually case insensitive
BCP 47 language tags are case insensitive. At the moment,
isWellFormedLanguageTag() lowercases the language tag to be checked, but
the regex contains uppercase characters, which means it will incorrectly
return false for a language tag like "en-GB-oed".

This changes the regex to use lowercase letters throughout, for
consistency, since it doesn't use capital letters in other places where
they would normally be used, but also makes it do a case-insensitive
match instead of trying to make sure the case for the regex and language
tag are the same. This should reduce the chance of it breaking if
someone later re-adds capital letters to the regex.

Change-Id: Iacf87d37c7ac515c350399f99e05e3f5bace9b90
2023-11-15 15:27:32 +00:00
Reedy
40f7d7632a Language: minor cleanup
* Remove unnecessary duplicate documentation
* Code tweaks

Change-Id: I9c8c190bee58195eace08cba18a51e4572896ef7
2023-10-02 13:34:11 +00:00
jenkins-bot
a1f4fb418a Merge "Allow Bcp47Code as parameter to LanguageCode::bcp47ToInternal()" 2023-09-29 21:27:27 +00:00
C. Scott Ananian
f47de6ec61 Allow Bcp47Code as parameter to LanguageCode::bcp47ToInternal()
This nominally takes a string-valued language code conforming to the
BCP-47 standard, but this is often generated from a Bcp47Code object.
Since the MediaWiki Language code implements Bcp47Code, we may have
the case where we have a Language object in hand (but typed as a
Bcp47Code not Language) and call Language::toBcp47Code() only to pass
it to LanguageCode::bcp47ToInternal to convert it back to a
mediawiki-internal code.

We can save steps and be more efficient if allow the parameter to be a
Bcp47Code object, and write a fast path for the special case where
that Bcp47Code happens to be a Language object and we can simply call
Language::getCode() to obtain the internal code.

Change-Id: I24932449b8c40e3a5072748d87667184f4befa67
2023-09-29 15:10:29 -04:00
Amir E. Aharoni
390445b343 Languages: Add language crh-ro
Only Romanian is defined as fallback. See:
https://translatewiki.net/w/i.php?title=User_talk:Amire80&oldid=11617782#Add_Fallback_language

Bug: T336919
Change-Id: Ibe5a8227f30e78b04d4ed3a1476c06fc9e725422
2023-05-27 09:21:29 +00:00
C. Scott Ananian
5ad8dea80a Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.

Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.

This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be.  Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method.  The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).

Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.

Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2023-03-13 13:25:09 -04:00
jenkins-bot
d8c48e19c4 Merge "Tighten up function signature for LanguageCode::bcp47ToInternal()" 2023-01-31 21:42:00 +00:00
C. Scott Ananian
a7f19491f0 Tighten up function signature for LanguageCode::bcp47ToInternal()
This was newly-added in 1.40 and it should have a modern tight
signature.

Change-Id: I34a058c1e4b141342cd1ea543e4d35ed141251e0
2023-01-30 17:56:39 -05:00
Winston Sung
99bc537548 Minor adjustments for Names.php and LanguageCode.php
kk has suppress-script: Cyrl in BCP 47, so kk-cyrl-kz won't be added,
 non-Cyrl kk-* would be converted kk-*-* to distinguish with
 kk(-Cyrl)-*.

Bug: T321639
Change-Id: I6e58628f912222f73f8e94c01d5a3b43c2f15c30
2023-01-29 16:46:48 +00:00
C. Scott Ananian
e185a570ae Add efficient LanguageCode::bcp47ToInternal() helper
LanguageFactory::getLanguage() will accept *lowercased* BCP-47 codes, so
this method is equivalent to
  LanguageFactory::getLanguage(strtolower($code))->getCode()
but should be much more efficient in practice.

Change-Id: I180765604d08ed57f655b69dfb32686f0b2a0dce
2022-10-20 10:11:14 -04:00
Fomafix
233677a9a5 Move isWellFormedLanguageTag from Language to LanguageCode
The static function isWellFormedLanguageTag is related to BCP 47
language codes not to the internal language codes or language names.

The new function LanguageCode::isWellFormedLanguageTag uses type hints.
THe explicit type cast (bool) is not necessary anymore.

The old function Language::isWellFormedLanguageTag is now deprecated.

Change-Id: I6431dbd82ed6dfcc2a7c3495eca025506551db05
2022-08-30 11:39:26 +00:00
Umherirrender
3f08cd753a Replace deprecated Language::fetchLanguageNames
Change-Id: Iecd5481edb4dae6c7911157e24c32a51f4438ab2
2021-12-05 21:11:49 +01:00
Timo Tijhof
f5895c2c82 language: Clean up file headers and class-level docs
This follows-up d83fcce5cb, which did something similar for
includes/profiler/.

* Ensure presence of license header.

* Merge any file-level descriptions with the class block,
  where it gets seen in generated docs about that class.

* Add any missing `@ingroup` tags to class blocks.

* Remove remaining `@ingroup` from file blocks.
  These clutter the Doxygen pages with duplicate entries.

* Fix some misspelled words from 61e0908fa2 and f136c2953c.

Change-Id: I5d21ec159766b799ba519da951d4f0716bae5f9f
2020-02-12 02:15:44 +00:00
Max Semenik
8a98dd9d59 Convert some private static arrays to constants
Remove @since for some private ones as we don't guarantee anything
about private class members.

Change-Id: Ifb898353c02082e9ef69d67f69339345c6cd154d
2019-10-16 01:30:54 +00:00
Aryeh Gregor
6d80b6c082 Split some Language methods to LanguageNameUtils
These are static methods that have to do with processing language names
and codes. I didn't include fallback behavior, because that would mean a
circular dependency with LocalisationCache.

In the new class, I renamed AS_AUTONYMS to AUTONYMS, and added a class
constant DEFINED for 'mw' to match the existing SUPPORTED and ALL. I
also renamed fetchLanguageName(s) to getLanguageName(s).

There is 100% test coverage for the code in the new class.

This was previously committed as 2e52f48c2e and reverted because it
depended on e4468a1d6b, which had to be reverted for performance
issues. There should be no changes other than rebasing.

Bug: T201405
Change-Id: Ifa346c8a92bf1eb57dc5e79458b32b7b26f1ee8a
2019-10-07 15:20:52 -07:00
Amir Sarabadani
308e6427ae Revert "Make LocalisationCache a service"
This reverts commits:
 - 76a940350d
 - b78b8804d0
 - 2e52f48c2e
 - e4468a1d6b

Bug: T231200
Bug: T231198
Change-Id: I1a7e46a979ae5c9c8130dd3927f6663a216ba753
2019-08-26 18:28:26 +02:00
Aryeh Gregor
2e52f48c2e Split some Language methods to LanguageNameUtils
These are static methods that have to do with processing language names
and codes. I didn't include fallback behavior, because that would mean a
circular dependency with LocalisationCache.

In the new class, I renamed AS_AUTONYMS to AUTONYMS, and added a class
constant DEFINED for 'mw' to match the existing SUPPORTED and ALL. I
also renamed fetchLanguageName(s) to getLanguageName(s).

There is 100% test coverage for the code in the new class.

Change-Id: I245ae94bfc1f62b6af75ea57525139adf2539fe6
2019-08-23 12:52:35 +03:00
Timo Tijhof
f557b2be4f language: Move some language-related classes to includes/language/
Bug: T225756
Change-Id: Ica351483d83a3a912d58ebb194b30828541fbcf3
2019-06-18 21:38:44 +00:00
Renamed from languages/LanguageCode.php (Browse further)