Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions babel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,40 @@ def ordinal_form(self):
"""
return self._data.get('ordinal_form', _default_plural_rule)

@property
def measurement_systems(self):
"""Localized names for various measurement systems.

>>> Locale('fr', 'FR').measurement_systems['US']
u'am\\xe9ricain'
>>> Locale('en', 'US').measurement_systems['US']
u'US'

"""
return self._data['measurement_systems']

@property
def character_order(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have been a great use of a Python 3.4 Enum. Just an observation, no need to do anything here. :)

"""The text direction for the language.

>>> Locale('de', 'DE').character_order
'left-to-right'
>>> Locale('ar', 'SA').character_order
'right-to-left'
"""
return self._data['character_order']

@property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels pretty redundant with the method above. I understand the value of providing the CSS-specific value in this package (commonly used), but I don't think it belongs in the Locale class.

We could create helper modules that provide methods like this that transform CLDR values into ones accepted by other systems. Off hand, I can't think of any other ones that might be relevant for CSS (maybe language code?), but, for instance, converting date format strings to JS compatible ones might be useful to someone.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is that much of an evil. After all, ltr and rtl are fairly common terms... However, now that you do mention it, this should really be called text_direction. I shall rename it post-haste.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, good catch. :) What do you think about transforming "left-to-right" to "ltr" (yah, I'm still trying to get rid of one of the two methods :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, character_order (well, characterOrder, but we're snakes, not camels) is the CLDR name, so I'd like to keep that as-is. Likewise, direction is the CSS property (though I think it's better spelled as text_direction in Babel, just to be explicit).

def text_direction(self):
"""The text direction for the language in CSS short-hand form.

>>> Locale('de', 'DE').text_direction
'ltr'
>>> Locale('ar', 'SA').text_direction
'rtl'
"""
return ''.join(word[0] for word in self.character_order.split('-'))


def default_locale(category=None, aliases=LOCALE_ALIASES):
"""Returns the system default locale for a given category, based on
Expand Down
15 changes: 15 additions & 0 deletions scripts/import_cldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ def _process_local_datas(sup, srcdir, destdir, force=False, dump_json=False):
parse_currency_names(data, tree)
parse_unit_patterns(data, tree)
parse_date_fields(data, tree)
parse_character_order(data, tree)
parse_measurement_systems(data, tree)

write_datafile(data_filename, data, dump_json=dump_json)

Expand Down Expand Up @@ -839,6 +841,19 @@ def parse_day_period_rules(tree):
return day_periods


def parse_character_order(data, tree):
for elem in tree.findall('.//layout/orientation/characterOrder'):
data['character_order'] = elem.text


def parse_measurement_systems(data, tree):
measurement_systems = data.setdefault('measurement_systems', {})
for measurement_system in tree.findall('.//measurementSystemNames/measurementSystemName'):
type = measurement_system.attrib['type']
if not _should_skip_elem(measurement_system, type=type, dest=measurement_systems):
_import_type_text(measurement_systems, measurement_system, type=type)



if __name__ == '__main__':
main()