| 28 | | use I18N::LangTags qw(is_language_tag same_language_tag |
| 29 | | extract_language_tags super_languages |
| 30 | | similarity_language_tag is_dialect_of |
| 31 | | locale2language_tag alternate_language_tags |
| 32 | | encode_language_tag panic_languages |
| 33 | | ); |
| 34 | | |
| 35 | | ...or whatever of those functions you want to import. Those are |
| 36 | | all the exportable functions -- you're free to import only some, |
| 37 | | or none at all. By default, none are imported. If you say: |
| | 33 | use I18N::LangTags(); |
| | 34 | |
| | 35 | ...or specify whichever of those functions you want to import, like so: |
| | 36 | |
| | 37 | use I18N::LangTags qw(implicate_supers similarity_language_tag); |
| | 38 | |
| | 39 | All the exportable functions are listed below -- you're free to import |
| | 40 | only some, or none at all. By default, none are imported. If you |
| | 41 | say: |
| | 745 | #--------------------------------------------------------------------------- |
| | 746 | #--------------------------------------------------------------------------- |
| | 747 | |
| | 748 | =item * the function implicate_supers( ...languages... ) |
| | 749 | |
| | 750 | This takes a list of strings (which are presumed to be language-tags; |
| | 751 | strings that aren't, are ignored); and after each one, this function |
| | 752 | inserts super-ordinate forms that don't already appear in the list. |
| | 753 | The original list, plus these insertions, is returned. |
| | 754 | |
| | 755 | In other words, it takes this: |
| | 756 | |
| | 757 | pt-br de-DE en-US fr pt-br-janeiro |
| | 758 | |
| | 759 | and returns this: |
| | 760 | |
| | 761 | pt-br pt de-DE de en-US en fr pt-br-janeiro |
| | 762 | |
| | 763 | This function is most useful in the idiom |
| | 764 | |
| | 765 | implicate_supers( I18N::LangTags::Detect::detect() ); |
| | 766 | |
| | 767 | (See L<I18N::LangTags::Detect>.) |
| | 768 | |
| | 769 | |
| | 770 | =item * the function implicate_supers_strictly( ...languages... ) |
| | 771 | |
| | 772 | This works like C<implicate_supers> except that the implicated |
| | 773 | forms are added to the end of the return list. |
| | 774 | |
| | 775 | In other words, implicate_supers_strictly takes a list of strings |
| | 776 | (which are presumed to be language-tags; strings that aren't, are |
| | 777 | ignored) and after the whole given list, it inserts the super-ordinate forms |
| | 778 | of all given tags, minus any tags that already appear in the input list. |
| | 779 | |
| | 780 | In other words, it takes this: |
| | 781 | |
| | 782 | pt-br de-DE en-US fr pt-br-janeiro |
| | 783 | |
| | 784 | and returns this: |
| | 785 | |
| | 786 | pt-br de-DE en-US fr pt-br-janeiro pt de en |
| | 787 | |
| | 788 | The reason this function has "_strictly" in its name is that when |
| | 789 | you're processing an Accept-Language list according to the RFCs, if |
| | 790 | you interpret the RFCs quite strictly, then you would use |
| | 791 | implicate_supers_strictly, but for normal use (i.e., common-sense use, |
| | 792 | as far as I'm concerned) you'd use implicate_supers. |
| | 793 | |
| | 794 | =cut |
| | 795 | |
| | 796 | sub implicate_supers { |
| | 797 | my @languages = grep is_language_tag($_), @_; |
| | 798 | my %seen_encoded; |
| | 799 | foreach my $lang (@languages) { |
| | 800 | $seen_encoded{ I18N::LangTags::encode_language_tag($lang) } = 1 |
| | 801 | } |
| | 802 | |
| | 803 | my(@output_languages); |
| | 804 | foreach my $lang (@languages) { |
| | 805 | push @output_languages, $lang; |
| | 806 | foreach my $s ( I18N::LangTags::super_languages($lang) ) { |
| | 807 | # Note that super_languages returns the longest first. |
| | 808 | last if $seen_encoded{ I18N::LangTags::encode_language_tag($s) }; |
| | 809 | push @output_languages, $s; |
| | 810 | } |
| | 811 | } |
| | 812 | return uniq( @output_languages ); |
| | 813 | |
| | 814 | } |
| | 815 | |
| | 816 | sub implicate_supers_strictly { |
| | 817 | my @tags = grep is_language_tag($_), @_; |
| | 818 | return uniq( @_, map super_languages($_), @_ ); |
| | 819 | } |
| | 820 | |
| | 821 | |
| | 822 | |