-
Notifications
You must be signed in to change notification settings - Fork 462
Territory languages #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Territory languages #315
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7dcd86b
scripts: add territory-language import from CLDR
mxsasha f68373c
Add public API for territory language data
akx 27ee02b
core: documented all valid keys for get_global()
mxsasha 07dc2e4
get_global: format key documentation, add warning
akx 935a0be
Add documentation for `babel.languages`
akx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # -- encoding: UTF-8 -- | ||
| from babel.core import get_global | ||
|
|
||
|
|
||
| def get_official_languages(territory, regional=False, de_facto=False): | ||
| """ | ||
| Get the official language(s) for the given territory. | ||
|
|
||
| The language codes, if any are known, are returned in order of descending popularity. | ||
|
|
||
| If the `regional` flag is set, then languages which are regionally official are also returned. | ||
|
|
||
| If the `de_facto` flag is set, then languages which are "de facto" official are also returned. | ||
|
|
||
| .. warning:: Note that the data is as up to date as the current version of the CLDR used | ||
| by Babel. If you need scientifically accurate information, use another source! | ||
|
|
||
| :param territory: Territory code | ||
| :type territory: str | ||
| :param regional: Whether to return regionally official languages too | ||
| :type regional: bool | ||
| :param de_facto: Whether to return de-facto official languages too | ||
| :type de_facto: bool | ||
| :return: Tuple of language codes | ||
| :rtype: tuple[str] | ||
| """ | ||
|
|
||
| territory = str(territory).upper() | ||
| allowed_stati = set(("official",)) | ||
| if regional: | ||
| allowed_stati.add("official_regional") | ||
| if de_facto: | ||
| allowed_stati.add("de_facto_official") | ||
|
|
||
| languages = get_global("territory_languages").get(territory, {}) | ||
| pairs = [ | ||
| (info['population_percent'], language) | ||
| for language, info in languages.items() | ||
| if info.get('official_status') in allowed_stati | ||
| ] | ||
| pairs.sort(reverse=True) | ||
| return tuple(lang for _, lang in pairs) | ||
|
|
||
|
|
||
|
|
||
| def get_territory_language_info(territory): | ||
| """ | ||
| Get a dictionary of language information for a territory. | ||
|
|
||
| The dictionary is keyed by language code; the values are dicts with more information. | ||
|
|
||
| The following keys are currently known for the values: | ||
|
|
||
| * `population_percent`: The percentage of the territory's population speaking the | ||
| language. | ||
| * `official_status`: An optional string describing the officiality status of the language. | ||
| Known values are "official", "official_regional" and "de_facto_official". | ||
|
|
||
| .. warning:: Note that the data is as up to date as the current version of the CLDR used | ||
| by Babel. If you need scientifically accurate information, use another source! | ||
|
|
||
| .. note:: Note that the format of the dict returned may change between Babel versions. | ||
|
|
||
| See http://www.unicode.org/cldr/charts/latest/supplemental/territory_language_information.html | ||
|
|
||
| :param territory: Territory code | ||
| :type territory: str | ||
| :return: Language information dictionary | ||
| :rtype: dict[str, dict] | ||
| """ | ||
| territory = str(territory).upper() | ||
| return get_global("territory_languages").get(territory, {}).copy() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ public API of Babel. | |
| core | ||
| dates | ||
| lists | ||
| languages | ||
| messages/index | ||
| numbers | ||
| plural | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| Languages | ||
| ========= | ||
|
|
||
| .. module:: babel.languages | ||
|
|
||
| The languages module provides functionality to access data about | ||
| languages that is not bound to a given locale. | ||
|
|
||
| Official Languages | ||
| ------------------ | ||
|
|
||
| .. autofunction:: get_official_languages | ||
|
|
||
| .. autofunction:: get_territory_language_info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # -- encoding: UTF-8 -- | ||
| from babel.languages import get_official_languages, get_territory_language_info | ||
|
|
||
|
|
||
| def test_official_languages(): | ||
| assert get_official_languages("FI") == ("fi", "sv") | ||
| assert get_official_languages("SE") == ("sv",) | ||
| assert get_official_languages("CH") == ("de", "fr", "it") | ||
| assert get_official_languages("CH", de_facto=True) == ("de", "gsw", "fr", "it") | ||
| assert get_official_languages("CH", regional=True) == ("de", "fr", "it", "rm") | ||
|
|
||
|
|
||
| def test_get_language_info(): | ||
| assert set(get_territory_language_info("HU").keys()) == set(("hu", "en", "de", "ro", "hr", "sk", "sl")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be inclined to write:
It should be a little faster and a bit more Pythonic to make more extensive use of comprehensions.