summaryrefslogtreecommitdiff
path: root/i18npool/source/languagetag/languagetag.cxx
blob: ad8b9c138dd8887837f427a1e2df035e85a54a92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "i18npool/languagetag.hxx"
#include "i18npool/mslangid.hxx"
#include <rtl/ustrbuf.hxx>
#include <rtl/bootstrap.hxx>
#include <osl/file.hxx>
#include <rtl/instance.hxx>

//#define erDEBUG

#if defined(ENABLE_LIBLANGTAG)
#include <liblangtag/langtag.h>
#else
/* Replacement code for LGPL phobic and Android systems.
 * For iOS we could probably use NSLocale instead, that should have more or
 * less required functionality. If it is good enough, it could be used for Mac
 * OS X, too.
 */
#include "simple-langtag.cxx"
#endif

using rtl::OUString;
using rtl::OString;
using rtl::OUStringBuffer;
using namespace com::sun::star;

// The actual pointer type of mpImplLangtag that is declared void* to not
// pollute the entire code base with liblangtag.
#define LANGTAGCAST(p) (reinterpret_cast<lt_tag_t*>(p))
#define MPLANGTAG LANGTAGCAST(mpImplLangtag)

/** Convention to signal presence of BCP 47 language tag in a Locale's Variant
    field. The Locale's Language field then will contain this ISO 639-2
    reserved for local use code. */
#define ISO639_LANGUAGE_TAG "qlt"


// "statics" to be returned as const reference to an empty locale and string.
namespace {
struct theEmptyLocale : public rtl::Static< lang::Locale, theEmptyLocale > {};
struct theEmptyBcp47 : public rtl::Static< OUString, theEmptyBcp47 > {};
}


/** A reference holder for liblangtag data de/initialization, one static
    instance. Currently implemented such that the first "ref" inits and dtor
    (our library deinitialized) tears down.
*/
class LiblantagDataRef
{
public:
    LiblantagDataRef();
    ~LiblantagDataRef();
    inline void incRef()
    {
        if (mnRef != SAL_MAX_UINT32 && !mnRef++)
            setup();
    }
    inline void decRef()
    {
        if (mnRef != SAL_MAX_UINT32 && mnRef && !--mnRef)
            teardown();
    }
    void presetDataPath( const rtl::OUString& rPath );
private:
    rtl::OString maDataPath;   // path to liblangtag data, "|" if system
    sal_uInt32   mnRef;

    void setupDataPath();
    void setup();
    void teardown();
};

namespace {
struct theDataRef : public rtl::Static< LiblantagDataRef, theDataRef > {};
}

LiblantagDataRef::LiblantagDataRef()
    :
        mnRef(0)
{
}

LiblantagDataRef::~LiblantagDataRef()
{
    // When destructed we're tearing down unconditionally.
    if (mnRef)
        mnRef = 1;
    decRef();
}

void LiblantagDataRef::setup()
{
    if (maDataPath.isEmpty())
        setupDataPath();
    lt_db_initialize();
    // Hold ref eternally.
    mnRef = SAL_MAX_UINT32;
}

void LiblantagDataRef::teardown()
{
    lt_db_finalize();
}

void LiblantagDataRef::presetDataPath( const rtl::OUString& rPath )
{
    if (maDataPath.isEmpty())
    {
        maDataPath = OUStringToOString( rPath, RTL_TEXTENCODING_UTF8);
        lt_db_set_datadir( maDataPath.getStr());
    }
}

void LiblantagDataRef::setupDataPath()
{
    // maDataPath is assumed to be empty here.
    OUString aURL;

    if (!rtl::Bootstrap::get("BRAND_BASE_DIR", aURL) && !rtl::Bootstrap::get("LIBLANGTAG_SHARE", aURL))
        OSL_FAIL( "LiblantagDataRef: can't get BRAND_BASE_DIR");
    else
    {
        // Check if data is in our own installation, else assume system
        // installation.
        aURL += "/share/liblangtag";
        OUString aData( aURL);
        aData += "/language-subtag-registry.xml";
        osl::DirectoryItem aDirItem;
        if (osl::DirectoryItem::get( aData, aDirItem) == osl::DirectoryItem::E_None)
        {
            OUString aPath;
            if (osl::FileBase::getSystemPathFromFileURL( aURL, aPath) == osl::FileBase::E_None)
                maDataPath = OUStringToOString( aPath, RTL_TEXTENCODING_UTF8);
        }
    }
    if (maDataPath.isEmpty())
        maDataPath = "|";   // assume system
    else
        lt_db_set_datadir( maDataPath.getStr());
}

LanguageTag::LanguageTag( const rtl::OUString & rBcp47LanguageTag, bool bCanonicalize )
    :
        maBcp47( rBcp47LanguageTag),
        mpImplLangtag( NULL),
        mnLangID( LANGUAGE_DONTKNOW),
        meIsValid( DECISION_DONTKNOW),
        meIsIsoLocale( DECISION_DONTKNOW),
        meIsIsoODF( DECISION_DONTKNOW),
        mbSystemLocale( rBcp47LanguageTag.isEmpty()),
        mbInitializedBcp47( !mbSystemLocale),
        mbInitializedLocale( false),
        mbInitializedLangID( false),
        mbCachedLanguage( false),
        mbCachedScript( false),
        mbCachedCountry( false)
{
    theDataRef::get().incRef();

    if (bCanonicalize)
        canonicalize();
}


LanguageTag::LanguageTag( const com::sun::star::lang::Locale & rLocale )
    :
        maLocale( rLocale),
        mpImplLangtag( NULL),
        mnLangID( LANGUAGE_DONTKNOW),
        meIsValid( DECISION_DONTKNOW),
        meIsIsoLocale( DECISION_DONTKNOW),
        meIsIsoODF( DECISION_DONTKNOW),
        mbSystemLocale( rLocale.Language.isEmpty()),
        mbInitializedBcp47( false),
        mbInitializedLocale( !mbSystemLocale),
        mbInitializedLangID( false),
        mbCachedLanguage( false),
        mbCachedScript( false),
        mbCachedCountry( false)
{
    theDataRef::get().incRef();
}


LanguageTag::LanguageTag( LanguageType nLanguage )
    :
        mpImplLangtag( NULL),
        mnLangID( nLanguage),
        meIsValid( DECISION_DONTKNOW),
        meIsIsoLocale( DECISION_DONTKNOW),
        meIsIsoODF( DECISION_DONTKNOW),
        mbSystemLocale( nLanguage == LANGUAGE_SYSTEM),
        mbInitializedBcp47( false),
        mbInitializedLocale( false),
        mbInitializedLangID( !mbSystemLocale),
        mbCachedLanguage( false),
        mbCachedScript( false),
        mbCachedCountry( false)
{
    theDataRef::get().incRef();
}


LanguageTag::LanguageTag( const rtl::OUString& rLanguage, const rtl::OUString& rCountry )
    :
        maLocale( rLanguage, rCountry, ""),
        mpImplLangtag( NULL),
        mnLangID( LANGUAGE_DONTKNOW),
        meIsValid( DECISION_DONTKNOW),
        meIsIsoLocale( DECISION_DONTKNOW),
        meIsIsoODF( DECISION_DONTKNOW),
        mbSystemLocale( rLanguage.isEmpty()),
        mbInitializedBcp47( false),
        mbInitializedLocale( !mbSystemLocale),
        mbInitializedLangID( false),
        mbCachedLanguage( false),
        mbCachedScript( false),
        mbCachedCountry( false)
{
    theDataRef::get().incRef();
}


LanguageTag::LanguageTag( const LanguageTag & rLanguageTag )
    :
        maLocale( rLanguageTag.maLocale),
        maBcp47( rLanguageTag.maBcp47),
        maCachedLanguage( rLanguageTag.maCachedLanguage),
        maCachedScript( rLanguageTag.maCachedScript),
        maCachedCountry( rLanguageTag.maCachedCountry),
        mpImplLangtag( rLanguageTag.mpImplLangtag ?
                lt_tag_copy( LANGTAGCAST( rLanguageTag.mpImplLangtag)) : NULL),
        mnLangID( rLanguageTag.mnLangID),
        meIsValid( rLanguageTag.meIsValid),
        meIsIsoLocale( rLanguageTag.meIsIsoLocale),
        meIsIsoODF( rLanguageTag.meIsIsoODF),
        mbSystemLocale( rLanguageTag.mbSystemLocale),
        mbInitializedBcp47( rLanguageTag.mbInitializedBcp47),
        mbInitializedLocale( rLanguageTag.mbInitializedLocale),
        mbInitializedLangID( rLanguageTag.mbInitializedLangID),
        mbCachedLanguage( rLanguageTag.mbCachedLanguage),
        mbCachedScript( rLanguageTag.mbCachedScript),
        mbCachedCountry( rLanguageTag.mbCachedCountry)
{
    theDataRef::get().incRef();
}


LanguageTag& LanguageTag::operator=( const LanguageTag & rLanguageTag )
{
    maLocale            = rLanguageTag.maLocale;
    maBcp47             = rLanguageTag.maBcp47;
    maCachedLanguage    = rLanguageTag.maCachedLanguage;
    maCachedScript      = rLanguageTag.maCachedScript;
    maCachedCountry     = rLanguageTag.maCachedCountry;
    mpImplLangtag       = rLanguageTag.mpImplLangtag;
    mpImplLangtag       = rLanguageTag.mpImplLangtag ?
                            lt_tag_copy( LANGTAGCAST( rLanguageTag.mpImplLangtag)) : NULL;
    mnLangID            = rLanguageTag.mnLangID;
    meIsValid           = rLanguageTag.meIsValid;
    meIsIsoLocale       = rLanguageTag.meIsIsoLocale;
    meIsIsoODF          = rLanguageTag.meIsIsoODF;
    mbSystemLocale      = rLanguageTag.mbSystemLocale;
    mbInitializedBcp47  = rLanguageTag.mbInitializedBcp47;
    mbInitializedLocale = rLanguageTag.mbInitializedLocale;
    mbInitializedLangID = rLanguageTag.mbInitializedLangID;
    mbCachedLanguage    = rLanguageTag.mbCachedLanguage;
    mbCachedScript      = rLanguageTag.mbCachedScript;
    mbCachedCountry     = rLanguageTag.mbCachedCountry;
    return *this;
}


LanguageTag::~LanguageTag()
{
    lt_tag_unref( MPLANGTAG);

    theDataRef::get().decRef();
}


bool LanguageTag::canonicalize() const
{
#ifdef erDEBUG
    // dump once
    struct dumper
    {
        void** mpp;
        dumper( void** pp ) : mpp( *pp ? NULL : pp) {}
        ~dumper() { if (mpp && *mpp) lt_tag_dump( LANGTAGCAST( *mpp)); }
    };
    dumper aDumper( &mpImplLangtag);
#endif

    getBcp47( true );   // side effect: have maBcp47 in any case, resolved system
    if (!mpImplLangtag)
        mpImplLangtag = lt_tag_new();

    // ensure error is free'd
    struct myerror
    {
        lt_error_t* p;
        myerror() : p(NULL) {}
        ~myerror() { if (p) lt_error_unref( p); }
    } aError;

    if (lt_tag_parse( MPLANGTAG, OUStringToOString( maBcp47, RTL_TEXTENCODING_UTF8).getStr(), &aError.p))
    {
        char* pTag = lt_tag_canonicalize( MPLANGTAG, &aError.p);
        SAL_WARN_IF( !pTag, "i18npool.langtag", "LanguageTag::canonicalize: could not canonicalize " << maBcp47);
        if (pTag)
        {
            OUString aOld( maBcp47);
            maBcp47 = OUString::createFromAscii( pTag);
            // Make the lt_tag_t follow the new string if different, which
            // removes default script and such.
            if (maBcp47 != aOld)
            {
                if (!lt_tag_parse( MPLANGTAG, pTag, &aError.p))
                {
                    SAL_WARN( "i18npool.langtag", "LanguageTag::canonicalize: could not reparse " << maBcp47);
                    free( pTag);
                    meIsValid = DECISION_NO;
                    return false;
                }
            }
            free( pTag);
            meIsValid = DECISION_YES;
            return true;
        }
    }
    else
    {
        SAL_INFO( "i18npool.langtag", "LanguageTag::canonicalize: could not parse " << maBcp47);
    }
    meIsValid = DECISION_NO;
    return false;
}


void LanguageTag::convertLocaleToBcp47()
{
    if (mbSystemLocale && !mbInitializedLocale)
        convertLangToLocale();

    if (maLocale.Language == ISO639_LANGUAGE_TAG)
    {
        maBcp47 = maLocale.Variant;
        meIsIsoLocale = DECISION_NO;
    }
    else
    {
        /* XXX NOTE: most legacy code never evaluated the Variant field, so for
         * now just concatenate language and country. In case we stumbled over
         * variant aware code we'd have to take care of that. */
        if (maLocale.Country.isEmpty())
            maBcp47 = maLocale.Language;
        else
        {
            OUStringBuffer aBuf( maLocale.Language.getLength() + 1 + maLocale.Country.getLength());
            aBuf.append( maLocale.Language).append( '-').append( maLocale.Country);
            maBcp47 = aBuf.makeStringAndClear();
        }
    }
    mbInitializedBcp47 = true;
}


void LanguageTag::convertLocaleToLang()
{
    if (mbSystemLocale)
    {
        mnLangID = MsLangId::getRealLanguage( LANGUAGE_SYSTEM);
    }
    else
    {
        /* FIXME: this is temporary until code base is converted to not use
         * MsLangId::convert...() anymore. After that, proper new method has to
         * be implemented to allow ISO639_LANGUAGE_TAG and sript tag and such. */
        mnLangID = MsLangId::convertLocaleToLanguage( maLocale);
    }
    mbInitializedLangID = true;
}


void LanguageTag::convertBcp47ToLocale()
{
    bool bIso = isIsoLocale();
    if (bIso)
    {
        maLocale.Language = getLanguageFromLangtag();
        maLocale.Country = getRegionFromLangtag();
        maLocale.Variant = OUString();
    }
    else
    {
        maLocale.Language = ISO639_LANGUAGE_TAG;
        maLocale.Country = getCountry();
        maLocale.Variant = maBcp47;
    }
    mbInitializedLocale = true;
}


void LanguageTag::convertBcp47ToLang()
{
    if (mbSystemLocale)
    {
        mnLangID = MsLangId::getRealLanguage( LANGUAGE_SYSTEM);
    }
    else
    {
        /* FIXME: this is temporary. If we support locales that consist not
         * only of language and country, e.g. added script, this probably needs
         * to be adapted. */
        if (!mbInitializedLocale)
            convertBcp47ToLocale();
        convertLocaleToLang();
    }
    mbInitializedLangID = true;
}


void LanguageTag::convertLangToLocale()
{
    if (mbSystemLocale && !mbInitializedLangID)
    {
        mnLangID = MsLangId::getRealLanguage( LANGUAGE_SYSTEM);
        mbInitializedLangID = true;
    }
    /* FIXME: this is temporary until code base is converted to not use
     * MsLangId::convert...() anymore. After that, proper new method has to be
     * implemented to allow ISO639_LANGUAGE_TAG and script tag and such. */
    // Resolve system here!
    maLocale = MsLangId::convertLanguageToLocale( mnLangID, true);
    mbInitializedLocale = true;
}


void LanguageTag::convertLangToBcp47()
{
    /* FIXME: this is temporary. If we support locales that consist not only of
     * language and country, e.g. added script, this probably needs to be
     * adapted. */
    if (!mbInitializedLocale)
        convertLangToLocale();
    convertLocaleToBcp47();
    mbInitializedBcp47 = true;
}


const rtl::OUString & LanguageTag::getBcp47( bool bResolveSystem ) const
{
    if (!bResolveSystem && mbSystemLocale)
        return theEmptyBcp47::get();
    if (!mbInitializedBcp47)
    {
        if (mbInitializedLocale)
            const_cast<LanguageTag*>(this)->convertLocaleToBcp47();
        else
            const_cast<LanguageTag*>(this)->convertLangToBcp47();
    }
    return maBcp47;
}


rtl::OUString LanguageTag::getLanguageFromLangtag() const
{
    rtl::OUString aLanguage;
    if (!mpImplLangtag)
        canonicalize();
    if (maBcp47.isEmpty())
        return aLanguage;
    const lt_lang_t* pLangT = lt_tag_get_language( MPLANGTAG);
    SAL_WARN_IF( !pLangT, "i18npool.langtag", "LanguageTag::getLanguageFromLangtag: pLangT==NULL");
    if (!pLangT)
        return aLanguage;
    const char* pLang = lt_lang_get_tag( pLangT);
    SAL_WARN_IF( !pLang, "i18npool.langtag", "LanguageTag::getLanguageFromLangtag: pLang==NULL");
    if (pLang)
        aLanguage = OUString::createFromAscii( pLang);
    return aLanguage;
}


rtl::OUString LanguageTag::getScriptFromLangtag() const
{
    rtl::OUString aScript;
    if (!mpImplLangtag)
        canonicalize();
    if (maBcp47.isEmpty())
        return aScript;
    const lt_script_t* pScriptT = lt_tag_get_script( MPLANGTAG);
    // pScriptT==NULL is valid for default scripts
    if (!pScriptT)
        return aScript;
    const char* pScript = lt_script_get_tag( pScriptT);
    SAL_WARN_IF( !pScript, "i18npool.langtag", "LanguageTag::getScriptFromLangtag: pScript==NULL");
    if (pScript)
        aScript = OUString::createFromAscii( pScript);
    return aScript;
}


rtl::OUString LanguageTag::getRegionFromLangtag() const
{
    rtl::OUString aRegion;
    if (!mpImplLangtag)
        canonicalize();
    if (maBcp47.isEmpty())
        return aRegion;
    const lt_region_t* pRegionT = lt_tag_get_region( MPLANGTAG);
    SAL_WARN_IF( !pRegionT, "i18npool.langtag", "LanguageTag::getRegionFromLangtag: pRegionT==NULL");
    if (!pRegionT)
        return aRegion;
    const char* pRegion = lt_region_get_tag( pRegionT);
    SAL_WARN_IF( !pRegion, "i18npool.langtag", "LanguageTag::getRegionFromLangtag: pRegion==NULL");
    if (pRegion)
        aRegion = OUString::createFromAscii( pRegion);
    return aRegion;
}


const com::sun::star::lang::Locale & LanguageTag::getLocale( bool bResolveSystem ) const
{
    if (!bResolveSystem && mbSystemLocale)
        return theEmptyLocale::get();
    if (!mbInitializedLocale)
    {
        if (mbInitializedBcp47)
            const_cast<LanguageTag*>(this)->convertBcp47ToLocale();
        else
            const_cast<LanguageTag*>(this)->convertLangToLocale();
    }
    return maLocale;
}


LanguageType LanguageTag::getLanguageType( bool bResolveSystem ) const
{
    if (!bResolveSystem && mbSystemLocale)
        return LANGUAGE_SYSTEM;
    if (!mbInitializedLangID)
    {
        if (mbInitializedBcp47)
            const_cast<LanguageTag*>(this)->convertBcp47ToLang();
        else
            const_cast<LanguageTag*>(this)->convertLocaleToLang();
    }
    return mnLangID;
}


namespace
{

bool isLowerAscii( sal_Unicode c )
{
    return 'a' <= c && c <= 'z';
}

bool isUpperAscii( sal_Unicode c )
{
    return 'A' <= c && c <= 'Z';
}

}


// static
bool LanguageTag::isIsoLanguage( const rtl::OUString& rLanguage )
{
    /* TODO: ignore case? For now let's see where rubbish is used. */
    bool b2chars;
    if (((b2chars = (rLanguage.getLength() == 2)) || rLanguage.getLength() == 3) &&
            isLowerAscii( rLanguage[0]) && isLowerAscii( rLanguage[1]) &&
            (b2chars || isLowerAscii( rLanguage[2])))
        return true;
    SAL_WARN_IF( ((rLanguage.getLength() == 2 || rLanguage.getLength() == 3) &&
                (isUpperAscii( rLanguage[0]) || isUpperAscii( rLanguage[1]))) ||
            (rLanguage.getLength() == 3 && isUpperAscii( rLanguage[2])), "i18npool.langtag",
            "LanguageTag::isIsoLanguage: rejecting upper case");
    return false;
}


// static
bool LanguageTag::isIsoCountry( const rtl::OUString& rRegion )
{
    /* TODO: ignore case? For now let's see where rubbish is used. */
    if (rRegion.isEmpty() ||
            (rRegion.getLength() == 2 && isUpperAscii( rRegion[0]) && isUpperAscii( rRegion[1])))
        return true;
    SAL_WARN_IF( rRegion.getLength() == 2 && (isLowerAscii( rRegion[0]) || isLowerAscii( rRegion[1])),
            "i18npool.langtag", "LanguageTag::isIsoCountry: rejecting lower case");
    return false;
}


// static
bool LanguageTag::isIsoScript( const rtl::OUString& rScript )
{
    /* TODO: ignore case? For now let's see where rubbish is used. */
    if (rScript.isEmpty() ||
            (rScript.getLength() == 4 &&
             isUpperAscii( rScript[0]) && isLowerAscii( rScript[1]) &&
             isLowerAscii( rScript[2]) && isLowerAscii( rScript[3])))
        return true;
    SAL_WARN_IF( rScript.getLength() == 4 &&
            (isLowerAscii( rScript[0]) || isUpperAscii( rScript[1]) ||
             isUpperAscii( rScript[2]) || isUpperAscii( rScript[3])),
            "i18npool.langtag", "LanguageTag::isIsoScript: rejecting case mismatch");
    return false;
}


rtl::OUString LanguageTag::getLanguage() const
{
    if (!mbCachedLanguage)
    {
        maCachedLanguage = getLanguageFromLangtag();
        mbCachedLanguage = true;
    }
    return maCachedLanguage;
}


rtl::OUString LanguageTag::getScript() const
{
    if (!mbCachedScript)
    {
        maCachedScript = getScriptFromLangtag();
        mbCachedScript = true;
    }
    return maCachedScript;
}


rtl::OUString LanguageTag::getLanguageAndScript() const
{
    OUString aLanguageScript( getLanguage());
    OUString aScript( getScript());
    if (!aScript.isEmpty())
    {
        OUStringBuffer aBuf( aLanguageScript.getLength() + 1 + aScript.getLength());
        aBuf.append( aLanguageScript).append( '-').append( aScript);
        aLanguageScript = aBuf.makeStringAndClear();
    }
    return aLanguageScript;
}


rtl::OUString LanguageTag::getCountry() const
{
    if (!mbCachedCountry)
    {
        maCachedCountry = getRegionFromLangtag();
        if (!isIsoCountry( maCachedCountry))
            maCachedCountry = OUString();
        mbCachedCountry = true;
    }
    return maCachedCountry;
}


rtl::OUString LanguageTag::getRegion() const
{
    return getRegionFromLangtag();
}


bool LanguageTag::isIsoLocale() const
{
    if (meIsIsoLocale == DECISION_DONTKNOW)
    {
        if (!mpImplLangtag)
            canonicalize();
        // It must be at most ll-CC or lll-CC
        // Do not use getCountry() here, use getRegion() instead.
        meIsIsoLocale = ((maBcp47.isEmpty() ||
                    (maBcp47.getLength() <= 6 && isIsoLanguage( getLanguage()) && isIsoCountry( getRegion()))) ?
                DECISION_YES : DECISION_NO);
    }
    return meIsIsoLocale == DECISION_YES;
}


bool LanguageTag::isIsoODF() const
{
    if (meIsIsoODF == DECISION_DONTKNOW)
    {
        if (!mpImplLangtag)
            canonicalize();
        if (!isIsoScript( getScript()))
            return ((meIsIsoODF = DECISION_NO) == DECISION_YES);
        // The usual case is lll-CC so simply check that first.
        if (isIsoLocale())
            return ((meIsIsoODF = DECISION_YES) == DECISION_YES);
        // If this is not ISO locale for which script must not exist it can
        // still be ISO locale plus ISO script lll-Ssss-CC
        meIsIsoODF = ((maBcp47.getLength() <= 11 &&
                    isIsoLanguage( getLanguage()) && isIsoCountry( getRegion()) && isIsoScript( getScript())) ?
                DECISION_YES : DECISION_NO);
    }
    return meIsIsoODF == DECISION_YES;
}


bool LanguageTag::isValidBcp47() const
{
    if (meIsValid == DECISION_DONTKNOW)
    {
        if (!mpImplLangtag)
           canonicalize();
        SAL_WARN_IF( meIsValid == DECISION_DONTKNOW, "i18npool.langtag",
                "LanguageTag::isValidBcp47: canonicalize() didn't set meIsValid");
    }
    return meIsValid == DECISION_YES;
}


bool LanguageTag::isSystemLocale() const
{
    return mbSystemLocale;
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */