File: klocale_kde.cpp

package info (click to toggle)
kde4libs 4%3A4.14.26-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 83,084 kB
  • ctags: 99,791
  • sloc: cpp: 763,588; xml: 12,007; ansic: 5,224; java: 4,060; perl: 2,938; yacc: 2,484; python: 1,219; sh: 1,174; ruby: 337; lex: 278; makefile: 32
file content (3322 lines) | stat: -rw-r--r-- 110,210 bytes parent folder | download | duplicates (4)
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
/* This file is part of the KDE libraries
   Copyright (c) 1997,2001 Stephan Kulow <coolo@kde.org>
   Copyright (c) 1999 Preston Brown <pbrown@kde.org>
   Copyright (c) 1999-2002 Hans Petter Bieker <bieker@kde.org>
   Copyright (c) 2002 Lukas Tinkl <lukas@kde.org>
   Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
   Copyright (C) 2009, 2010 John Layt <john@layt.net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "klocale_p.h"

#include "config-localization.h"

#include <math.h>
#include <locale.h>

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#if HAVE_LANGINFO_H
#include <langinfo.h>
#endif

#include <QtCore/QTextCodec>
#include <QtCore/QFile>
#include <QtGui/QPrinter>
#include <QtCore/QFileInfo>
#include <QtCore/QRegExp>
#include <QtCore/QLocale>
#include <QtCore/QHash>
#include <QtCore/QMutexLocker>
#include <QtCore/QStringList>

#include "kcatalog_p.h"
#include "kglobal.h"
#include "kstandarddirs.h"
#include "kconfig.h"
#include "kcomponentdata.h"
#include "kdebug.h"
#include "kdatetime.h"
#include "kcalendarsystem.h"
#include "kcurrencycode.h"
#include "klocalizedstring.h"
#include "kconfiggroup.h"
#include "kcatalogname_p.h"
#include "common_helpers_p.h"
#include "kdayperiod_p.h"

class KLocaleStaticData
{
public:

    KLocaleStaticData();

    QString maincatalog;

    // FIXME: Temporary until full language-sensitivity implemented.
    QHash<KLocale::DigitSet, QStringList> languagesUsingDigitSet;
};

KLocaleStaticData::KLocaleStaticData()
{
    // Languages using non-Western Arabic digit sets.
    // FIXME: Temporary until full language-sensitivity implemented.
    languagesUsingDigitSet.insert(KLocale::ArabicIndicDigits, QStringList() << QString::fromLatin1("ar") << QString::fromLatin1("ps"));
    languagesUsingDigitSet.insert(KLocale::BengaliDigits, QStringList() << QString::fromLatin1("bn") << QString::fromLatin1("as") );
    languagesUsingDigitSet.insert(KLocale::DevenagariDigits, QStringList() << QString::fromLatin1("hi") << QString::fromLatin1("ne"));
    languagesUsingDigitSet.insert(KLocale::EasternArabicIndicDigits, QStringList() << QString::fromLatin1("fa") << QString::fromLatin1("ur"));
    languagesUsingDigitSet.insert(KLocale::GujaratiDigits, QStringList() << QString::fromLatin1("gu") );
    languagesUsingDigitSet.insert(KLocale::GurmukhiDigits, QStringList() << QString::fromLatin1("pa") );
    languagesUsingDigitSet.insert(KLocale::KannadaDigits, QStringList() << QString::fromLatin1("kn") );
    languagesUsingDigitSet.insert(KLocale::KhmerDigits, QStringList() << QString::fromLatin1("km") );
    languagesUsingDigitSet.insert(KLocale::MalayalamDigits, QStringList() << QString::fromLatin1("ml") );
    languagesUsingDigitSet.insert(KLocale::OriyaDigits, QStringList() << QString::fromLatin1("or") );
    languagesUsingDigitSet.insert(KLocale::TamilDigits, QStringList() << QString::fromLatin1("ta") );
    languagesUsingDigitSet.insert(KLocale::TeluguDigits, QStringList() << QString::fromLatin1("te") );
    languagesUsingDigitSet.insert(KLocale::ThaiDigits, QStringList() << QString::fromLatin1("th"));
}

K_GLOBAL_STATIC(KLocaleStaticData, staticData)


QDebug operator<<(QDebug debug, const KCatalogName &cn)
{
    return debug << cn.name << cn.loadCount;
}

KLocalePrivate::KLocalePrivate(KLocale *q_ptr)
               : q(q_ptr),
                 m_config(KSharedConfig::Ptr()),
                 m_country(QString()),
                 m_language(QString()),
                 m_languages(0),
                 m_catalogName(QString()),
                 m_calendar(0),
                 m_currency(0),
                 m_codecForEncoding(0)
{
}

KLocalePrivate::KLocalePrivate(const KLocalePrivate &rhs)
{
    copy(rhs);
}

KLocalePrivate &KLocalePrivate::operator=(const KLocalePrivate &rhs)
{
    copy(rhs);
    return *this;
}

KConfig *KLocalePrivate::config()
{
    if (m_config != KSharedConfig::Ptr()) {
        return m_config.data();
    } else {
        return KGlobal::config().data();
    }
}

void KLocalePrivate::copy(const KLocalePrivate &rhs)
{
    // Parent KLocale
    q = 0;

    // Config
    m_config = rhs.m_config;

    // Country settings
    m_country = rhs.m_country;
    m_countryDivisionCode = rhs.m_countryDivisionCode;

    // Language settings
    m_language = rhs.m_language;
    m_languages = 0;
    m_languageList = rhs.m_languageList;
    m_languageSensitiveDigits = rhs.m_languageSensitiveDigits;
    m_nounDeclension = rhs.m_nounDeclension;

    // Catalog settings
    m_catalogName = rhs.m_catalogName;
    m_catalogNames = rhs.m_catalogNames;
    m_catalogs = rhs.m_catalogs;
    m_numberOfSysCatalogs = rhs.m_numberOfSysCatalogs;
    m_useTranscript = rhs.m_useTranscript;

    // Calendar settings
    m_calendarSystem = rhs.m_calendarSystem;
    m_calendar = 0;
    m_weekStartDay = rhs.m_weekStartDay;
    m_workingWeekStartDay = rhs.m_workingWeekStartDay;
    m_workingWeekEndDay = rhs.m_workingWeekEndDay;
    m_weekDayOfPray = rhs.m_weekDayOfPray;

    // Date/Time settings
    m_dateFormat = rhs.m_dateFormat;
    m_dateFormatShort = rhs.m_dateFormatShort;
    m_timeFormat = rhs.m_timeFormat;
    m_dateTimeDigitSet = rhs.m_dateTimeDigitSet;
    m_dateMonthNamePossessive = rhs.m_dateMonthNamePossessive;
    m_dayPeriods = rhs.m_dayPeriods;
    m_weekNumberSystem = rhs.m_weekNumberSystem;

    // Number settings
    m_decimalPlaces = rhs.m_decimalPlaces;
    m_decimalSymbol = rhs.m_decimalSymbol;
    m_thousandsSeparator = rhs.m_thousandsSeparator;
    m_numericDigitGrouping = rhs.m_numericDigitGrouping;
    m_positiveSign = rhs.m_positiveSign;
    m_negativeSign = rhs.m_negativeSign;
    m_digitSet = rhs.m_digitSet;

    // Currency settings
    m_currencyCode = rhs.m_currencyCode;
    m_currency = 0;
    m_currencyCodeList = rhs.m_currencyCodeList;

    // Money settings
    m_currencySymbol = rhs.m_currencySymbol;
    m_monetaryDecimalSymbol = rhs.m_monetaryDecimalSymbol;
    m_monetaryThousandsSeparator = rhs.m_monetaryThousandsSeparator;
    m_monetaryDigitGrouping = rhs.m_monetaryDigitGrouping;
    m_monetaryDecimalPlaces = rhs.m_monetaryDecimalPlaces;
    m_positiveMonetarySignPosition = rhs.m_positiveMonetarySignPosition;
    m_negativeMonetarySignPosition = rhs.m_negativeMonetarySignPosition;
    m_positivePrefixCurrencySymbol = rhs.m_positivePrefixCurrencySymbol;
    m_negativePrefixCurrencySymbol = rhs.m_negativePrefixCurrencySymbol;
    m_monetaryDigitSet = rhs.m_monetaryDigitSet;

    // Units settings
    m_binaryUnitDialect = rhs.m_binaryUnitDialect;
    m_byteSizeFmt = rhs.m_byteSizeFmt;
    m_pageSize = rhs.m_pageSize;
    m_measureSystem = rhs.m_measureSystem;

    // Encoding settings
    m_encoding = rhs.m_encoding;
    m_codecForEncoding = rhs.m_codecForEncoding;
    m_utf8FileEncoding = rhs.m_utf8FileEncoding;
}

KLocalePrivate::~KLocalePrivate()
{
    delete m_currency;
    delete m_calendar;
    delete m_languages;
}

// init only called from platform specific constructor, so set everything up
// Will be given a persistantConfig or a tempConfig or neither, but never both
void KLocalePrivate::init(const QString& catalogName, const QString &language, const QString &country,
                          KSharedConfig::Ptr persistantConfig, KConfig *tempConfig)
{
    m_catalogName = catalogName;

    // Only keep the persistant config if it is not the global
    if (persistantConfig != KSharedConfig::Ptr() && persistantConfig != KGlobal::config()) {
        m_config = persistantConfig;
    }

    KConfigGroup cg;
    bool useEnvironmentVariables;

    // We can't read the formats from the config until we know what locale to read in, but we need
    // to read the config to find out the locale.  The Country and Language settings should never
    // be localized in the config, so we can read a temp copy of them to get us started.

    // If no config given, use the global config and include envvars, otherwise use only the config.
    if (m_config != KSharedConfig::Ptr()) {
        cg = m_config->group(QLatin1String("Locale"));
        useEnvironmentVariables = false;
    } else if (tempConfig == 0 || tempConfig == KGlobal::config().data()) {
        cg = KGlobal::config()->group(QLatin1String("Locale"));
        useEnvironmentVariables = true;
    } else {
        cg = tempConfig->group(QLatin1String("Locale"));
        useEnvironmentVariables = false;
    }

    initEncoding();
    initFileNameEncoding();
    initCountry(country, cg.readEntry(QLatin1String("Country")));
    initLanguageList(language, cg.readEntry(QLatin1String("Language")), useEnvironmentVariables);
    // Now that we have a language, we can set up the config which uses it to setLocale()
    initConfig(tempConfig);
    initMainCatalogs();
    initFormat();
}

// Init the config, this is called during construction and by later setCountry/setLanguage calls.
// You _must_ have the m_language set to a valid language or en_US before calling this so a
// setLocale can be applied to the config
void KLocalePrivate::initConfig(KConfig *config)
{
    // * If we were constructed with a KSharedConfig it means the user gave it to us
    //   to use for the life of the KLocale, so just keep using it after a setLocale
    // * If passed in KConfig is null or the global config then use the global, but
    //   do the setLocale first.
    // * If we have a KConfig we need to use that, but due to keeping old behaviour
    //   of not requiring access to it for life we can't keep a reference so instead
    //   take a copy and use that, but do setLocale first.

    if (m_config != KSharedConfig::Ptr()) {
        m_config->setLocale(m_language);
    } else {
        // If no config given then use the global
        if (config == 0 || config == KGlobal::config().data()) {
            KGlobal::config()->setLocale(m_language);
        } else {
            config->setLocale(m_language);
            m_config = KSharedConfig::openConfig();
            config->copyTo(QString(), m_config.data());
            m_config->markAsClean();
        }
    }
}

void KLocalePrivate::initMainCatalogs()
{
    KLocaleStaticData *s = staticData;
    QMutexLocker lock(kLocaleMutex());

    if (!s->maincatalog.isEmpty()) {
        // If setMainCatalog was called, then we use that
        // (e.g. korgac calls setMainCatalog("korganizer") to use korganizer.po)
        m_catalogName = s->maincatalog;
    }

    if (m_catalogName.isEmpty()) {
        kDebug(173) << "KLocale instance created called without valid "
                    << "catalog! Give an argument or call setMainCatalog "
                    << "before init" << endl;
    } else {
        // do not use insertCatalog here, that would already trigger updateCatalogs
        m_catalogNames.append(KCatalogName(m_catalogName));   // application catalog

        // catalogs from which each application can draw translations
        const int numberOfCatalogs = m_catalogNames.size();
        m_catalogNames.append(KCatalogName(QString::fromLatin1("libphonon")));
        m_catalogNames.append(KCatalogName(QString::fromLatin1("kio4")));
        m_catalogNames.append(KCatalogName(QString::fromLatin1("kdelibs4")));
        m_catalogNames.append(KCatalogName(QString::fromLatin1("kdeqt")));
        m_catalogNames.append(KCatalogName(QString::fromLatin1("solid_qt")));
        m_catalogNames.append(KCatalogName(QString::fromLatin1("kdecalendarsystems")));
        m_numberOfSysCatalogs = m_catalogNames.size() - numberOfCatalogs;

        updateCatalogs(); // evaluate this for all languages
    }
}

void KLocalePrivate::getLanguagesFromVariable(QStringList &list, const char *variable, bool isLanguageList)
{
    QByteArray var(qgetenv(variable));
    if (!var.isEmpty()) {
        QString value = QFile::decodeName(var);
        if (isLanguageList) {
            list += value.split(QLatin1Char(':'));
        } else {
            // Process the value to create possible combinations.
            QString lang, ctry, modf, cset;
            KLocale::splitLocale(value, lang, ctry, modf, cset);

            if (!ctry.isEmpty() && !modf.isEmpty()) {
                list += lang + QLatin1Char('_') + ctry + QLatin1Char('@') + modf;
            }
            // NOTE: The priority is tricky in case both ctry and modf are present.
            // Should really lang@modf be of higher priority than lang_ctry?
            // For at least one case (Serbian language), it is better this way.
            if (!modf.isEmpty()) {
                list += lang + QLatin1Char('@') + modf;
            }
            if (!ctry.isEmpty()) {
                list += lang + QLatin1Char('_') + ctry;
            }
            list += lang;
        }
    }
}

// init the country at construction only, will ensure we always have a country set
void KLocalePrivate::initCountry(const QString &country, const QString &configCountry)
{
    // Cache the valid countries list and add the default C as it is valid to use
    QStringList validCountries = allCountriesList();
    validCountries.append( defaultCountry() );

    // First check if the constructor passed in a value and if so if it is valid
    QString putativeCountry = country;

    if ( putativeCountry.isEmpty() || !validCountries.contains( putativeCountry, Qt::CaseInsensitive ) ) {

        // If the requested country is not valid, try the country as set in the config:
        putativeCountry = configCountry;

        if ( putativeCountry.isEmpty() || !validCountries.contains( putativeCountry, Qt::CaseInsensitive ) ) {

            // If the config country is not valid try the current host system country
            putativeCountry = systemCountry();

            if ( putativeCountry.isEmpty() || !validCountries.contains( putativeCountry, Qt::CaseInsensitive ) ) {
                // Only if no other option, resort to the default C
                putativeCountry = defaultCountry();
            }
        }
    }

    // Always save as lowercase, unless it's C when we want it uppercase
    if ( putativeCountry.toLower() == defaultCountry().toLower() ) {
        m_country = defaultCountry();
    } else {
        m_country = putativeCountry.toLower();
    }
}

QString KLocalePrivate::systemCountry() const
{
    // Use QLocale for now as it supposedly provides a sensible default most times,
    // e.g. if locale is only "de" it is assumed to mean country of "DE"
    QString systemCountry, s1, s2, s3;
    splitLocale( QLocale::system().name(), s1, systemCountry, s2, s3 );
    return systemCountry.toLower();
}

void KLocalePrivate::initLanguageList(const QString &language, const QString &configLanguages,
                                      bool useEnvironmentVariables)
{
    m_language = language;

    // Collect possible languages by decreasing priority.
    // The priority is as follows:
    // - the internally set language, if any
    // - KDE_LANG environment variable (can be a list)
    // - KDE configuration (can be a list)
    // - environment variables considered by gettext(3)
    // The environment variables are not considered if useEnvironmentVariables is false.
    QStringList list;
    if (!m_language.isEmpty()) {
        list += m_language;
    }

    // If the Locale object was created with a specific config file, then do not use the
    // environmental variables.  If the locale object was created with the global config, then
    // do use the environmental variables.
    if (useEnvironmentVariables) {
        // KDE_LANG contains list of language codes, not locale string.
        getLanguagesFromVariable(list, "KDE_LANG", true);
    }

    if (!configLanguages.isEmpty()) {
        list += configLanguages.split(QLatin1Char(':'));
    }

    if (useEnvironmentVariables) {
        // Collect languages by same order of priority as for gettext(3).
        // LANGUAGE contains list of language codes, not locale string.
        getLanguagesFromVariable(list, "LANGUAGE", true);
        getLanguagesFromVariable(list, "LC_ALL");
        getLanguagesFromVariable(list, "LC_MESSAGES");
        getLanguagesFromVariable(list, "LANG");
    }

    // fall back to the system language
    list += systemLanguageList();

    // Send the list to filter for really present languages on the system.
    setLanguage(list);
}

QStringList KLocalePrivate::systemLanguageList() const
{
    return QStringList();
}

void KLocalePrivate::initFormat()
{
    KConfigGroup cg(config(), "Locale");

    KConfig entryFile(KStandardDirs::locate("locale", QString::fromLatin1("l10n/%1/entry.desktop").arg(m_country)));
    entryFile.setLocale(m_language);
    KConfigGroup entry(&entryFile, "KCM Locale");

    //One-time conversion in 4.4 from FracDigits to DecimalPlaces and MonetaryDecimalPlaces
    //If user has personal setting for FracDigits then use it for both Decimal Places
    //TODO: Possible to do with kconf_update
    if (cg.hasKey("FracDigits")) {
        QString fracDigits = cg.readEntry("FracDigits", "");
        if (!fracDigits.isEmpty()) {
            cg.writeEntry("DecimalPlaces", fracDigits);
            cg.writeEntry("MonetaryDecimalPlaces", fracDigits);
        }
        cg.deleteEntry("FracDigits");
        cg.config()->sync();
    }

    // Numeric
#define readConfigEntry(key, default, save) \
        save = entry.readEntry(key, default); \
        save = cg.readEntry(key, save);

#define readConfigNumEntry(key, default, save, type) \
        save = (type)entry.readEntry(key, int(default)); \
        save = (type)cg.readEntry(key, int(save));

    // Country settings
    readConfigEntry("CountryDivisionCode", QString(), m_countryDivisionCode);

    // Numeric formats
    readConfigNumEntry("DecimalPlaces", 2, m_decimalPlaces, int);

    readConfigEntry("DecimalSymbol", ".", m_decimalSymbol);
    readConfigEntry("ThousandsSeparator", ",", m_thousandsSeparator);
    m_thousandsSeparator.remove(QString::fromLatin1("$0"));
    QString digitGroupFormat;
    readConfigEntry("DigitGroupFormat", "3", digitGroupFormat);
    m_numericDigitGrouping = digitGroupFormatToList(digitGroupFormat);

    readConfigEntry("PositiveSign", "", m_positiveSign);
    readConfigEntry("NegativeSign", "-", m_negativeSign);

    readConfigNumEntry("DigitSet", KLocale::ArabicDigits, m_digitSet, KLocale::DigitSet);
    // FIXME: Temporary until full language-sensitivity implemented.
    readConfigEntry("LanguageSensitiveDigits", true, m_languageSensitiveDigits);

    // Currency
    readConfigEntry("CurrencyCode", "USD", m_currencyCode);
    initCurrency();
    readConfigEntry("CurrencySymbol", m_currency->defaultSymbol(), m_currencySymbol);
    readConfigEntry("CurrencyCodesInUse", QStringList(m_currencyCode), m_currencyCodeList);

    // Monetary formats
    readConfigNumEntry("MonetaryDecimalPlaces", m_currency->decimalPlaces(), m_monetaryDecimalPlaces, int);

    readConfigEntry("MonetaryDecimalSymbol", ".", m_monetaryDecimalSymbol);
    readConfigEntry("MonetaryThousandsSeparator", ",", m_monetaryThousandsSeparator);
    m_monetaryThousandsSeparator.remove(QString::fromLatin1("$0"));
    readConfigEntry("MonetaryDigitGroupFormat", "3", digitGroupFormat);
    m_monetaryDigitGrouping = digitGroupFormatToList(digitGroupFormat);

    readConfigEntry("PositivePrefixCurrencySymbol", true, m_positivePrefixCurrencySymbol);
    readConfigEntry("NegativePrefixCurrencySymbol", true, m_negativePrefixCurrencySymbol);
    readConfigNumEntry("PositiveMonetarySignPosition", KLocale::BeforeQuantityMoney,
                       m_positiveMonetarySignPosition, KLocale::SignPosition);
    readConfigNumEntry("NegativeMonetarySignPosition", KLocale::ParensAround,
                       m_negativeMonetarySignPosition, KLocale::SignPosition);

    readConfigNumEntry("MonetaryDigitSet", KLocale::ArabicDigits,
                       m_monetaryDigitSet, KLocale::DigitSet);
    readConfigNumEntry("BinaryUnitDialect", KLocale::IECBinaryDialect,
                       m_binaryUnitDialect, KLocale::BinaryUnitDialect);

    // Date and time
    readConfigEntry("TimeFormat", "%H:%M:%S", m_timeFormat);
    readConfigEntry("DateFormat", "%A %d %B %Y", m_dateFormat);
    readConfigEntry("DateFormatShort", "%Y-%m-%d", m_dateFormatShort);
    readConfigNumEntry("WeekStartDay", 1, m_weekStartDay, int);                //default to Monday
    readConfigNumEntry("WorkingWeekStartDay", 1, m_workingWeekStartDay, int);  //default to Monday
    readConfigNumEntry("WorkingWeekEndDay", 5, m_workingWeekEndDay, int);      //default to Friday
    readConfigNumEntry("WeekDayOfPray", 7, m_weekDayOfPray, int);              //default to Sunday
    readConfigNumEntry("DateTimeDigitSet", KLocale::ArabicDigits,
                       m_dateTimeDigitSet, KLocale::DigitSet);
    readConfigNumEntry("WeekNumberSystem", KLocale::IsoWeekNumber,
                       m_weekNumberSystem, KLocale::WeekNumberSystem);

    // other
#ifndef QT_NO_PRINTER
    readConfigNumEntry("PageSize", QPrinter::A4, m_pageSize, QPrinter::PageSize);
#endif
    readConfigNumEntry("MeasureSystem", KLocale::Metric, m_measureSystem, KLocale::MeasureSystem);
    QString calendarType;
    readConfigEntry("CalendarSystem", "gregorian", calendarType);
    setCalendar(calendarType);

    readConfigEntry("Transcript", true, m_useTranscript);

    //Grammatical
    //Precedence here is l10n / i18n / config file
    KConfig langCfg(KStandardDirs::locate("locale", QString::fromLatin1("%1/entry.desktop").arg(m_language)));
    KConfigGroup lang(&langCfg, "KCM Locale");
#define read3ConfigBoolEntry(key, default, save) \
        save = entry.readEntry(key, default); \
        save = lang.readEntry(key, save); \
        save = cg.readEntry(key, save);

    read3ConfigBoolEntry("NounDeclension", false, m_nounDeclension);
    read3ConfigBoolEntry("DateMonthNamePossessive", false, m_dateMonthNamePossessive);

    initDayPeriods(cg);
}

void KLocalePrivate::initDayPeriods(const KConfigGroup &cg)
{
    // Prefer any l10n file value for country/language,
    // otherwise default to language only value which will be filled in later when i18n available

    //Day Period are stored in config as one QStringList entry per Day Period
    //PeriodCode,LongName,ShortName,NarrowName,StartTime,EndTime,Offset,OffsetIfZero
    //where start and end time are in the format HH:MM:SS.MMM

    m_dayPeriods.clear();
    QString periodKey = QString::fromLatin1("DayPeriod1");
    int i = 1;
    while (cg.hasKey(periodKey)) {
        QStringList period = cg.readEntry(periodKey, QStringList());
        if (period.count() == 8) {
            m_dayPeriods.append(KDayPeriod(period[0], period[1], period[2], period[3],
                                           QTime::fromString(period[4], QString::fromLatin1("HH:mm:ss.zzz")),
                                           QTime::fromString(period[5], QString::fromLatin1("HH:mm:ss.zzz")),
                                           period[6].toInt(), period[7].toInt()));
        }
        i = i + 1;
        periodKey = QString::fromLatin1("DayPeriod%1").arg(i);
    }
}

bool KLocalePrivate::setCountry(const QString &country, KConfig *newConfig)
{
    // Cache the valid countries list and add the default C as it is valid to use
    QStringList validCountries = allCountriesList();
    validCountries.append(defaultCountry());

    QString putativeCountry = country;

    if (putativeCountry.isEmpty()) {
        // An empty string means to use the system country
        putativeCountry = systemCountry();
        if (putativeCountry.isEmpty() || !validCountries.contains(putativeCountry, Qt::CaseInsensitive)) {
            // If the system country is not valid, use the default
            putativeCountry = defaultCountry();
        }
    } else if (!validCountries.contains(putativeCountry, Qt::CaseInsensitive)) {
        return false;
    }

    // Always save as lowercase, unless it's C when we want it uppercase
    if (putativeCountry.toLower() == defaultCountry().toLower()) {
        m_country = defaultCountry();
    } else {
        m_country = putativeCountry.toLower();
    }

    // Get rid of the old config, start again with the new
    m_config = KSharedConfig::Ptr();
    initConfig(newConfig);

    // Init all the settings
    initFormat();

    return true;
}

bool KLocalePrivate::setCountryDivisionCode(const QString &countryDivisionCode)
{
    m_countryDivisionCode = countryDivisionCode;
    return true;
}

bool KLocalePrivate::setLanguage(const QString &language, KConfig *config)
{
    QMutexLocker lock(kLocaleMutex());
    m_languageList.removeAll(language);
    m_languageList.prepend(language);   // let us consider this language to be the most important one

    m_language = language; // remember main language for shortcut evaluation

    // important when called from the outside and harmless when called before
    // populating the catalog name list
    updateCatalogs();

    // Get rid of the old config, start again with the new
    m_config = KSharedConfig::Ptr();
    initConfig(config);

    // Init the new format settings
    initFormat();

    // Maybe the mo-files for this language are empty, but in principle we can speak all languages
    return true;
}

// KDE5 Unlike the other setLanguage call this does not reparse the config so the localized config
// settings for the new primary language will _not_ be loaded.  In KDE5 always keep the original
// config so this can be reparsed when required.
bool KLocalePrivate::setLanguage(const QStringList &languages)
{
    QMutexLocker lock(kLocaleMutex());
    // This list might contain
    // 1) some empty strings that we have to eliminate
    // 2) duplicate entries like in de:fr:de, where we have to keep the first occurrence of a
    //    language in order to preserve the order of precenence of the user
    // 3) languages into which the application is not translated. For those languages we should not
    //    even load kdelibs.mo or kio.po. these languages have to be dropped. Otherwise we get
    //    strange side effects, e.g. with Hebrew: the right/left switch for languages that write
    //    from right to left (like Hebrew or Arabic) is set in kdelibs.mo. If you only have
    //    kdelibs.mo but nothing from appname.mo, you get a mostly English app with layout from
    //    right to left. That was considered to be a bug by the Hebrew translators.
    QStringList list;
    foreach(const QString &language, languages) {
        if (!language.isEmpty() && !list.contains(language) && isApplicationTranslatedInto(language)) {
            list.append(language);
        }
    }

    if (!list.contains(KLocale::defaultLanguage())) {
        // English should always be added as final possibility; this is important
        // for proper initialization of message text post-processors which are
        // needed for English too, like semantic to visual formatting, etc.
        list.append(KLocale::defaultLanguage());
    }

    m_language = list.first(); // keep this for shortcut evaluations

    m_languageList = list; // keep this new list of languages to use

    // important when called from the outside and harmless when called before populating the
    // catalog name list
    updateCatalogs();

    return true; // we found something. Maybe it's only English, but we found something
}

void KLocalePrivate::initCurrency()
{
    if (m_currencyCode.isEmpty() || !KCurrencyCode::isValid(m_currencyCode)) {
        m_currencyCode = KLocale::defaultCurrencyCode();
    }

    if (!m_currency || m_currencyCode != m_currency->isoCurrencyCode() || !m_currency->isValid()) {
        delete m_currency;
        m_currency = new KCurrencyCode(m_currencyCode, m_language);
    }
}

void KLocalePrivate::setCurrencyCode(const QString &newCurrencyCode)
{
    if (!newCurrencyCode.isEmpty() && newCurrencyCode != m_currency->isoCurrencyCode() &&
        KCurrencyCode::isValid(newCurrencyCode)) {
        m_currencyCode = newCurrencyCode;
        initCurrency();
    }
}

bool KLocalePrivate::isApplicationTranslatedInto(const QString &lang)
{
    if (lang.isEmpty()) {
        return false;
    }

    if (lang == KLocale::defaultLanguage()) {
        // default language is always "installed"
        return true;
    }

    if (m_catalogName.isEmpty()) {
        kDebug() << "no appName!";
        return false;
    }

    if (!KCatalog::catalogLocaleDir(m_catalogName, lang).isEmpty()) {
        return true;
    }
    return false;
}

void KLocalePrivate::splitLocale(const QString &aLocale, QString &language, QString &country,
                                 QString &modifier, QString &charset)
{
    QString locale = aLocale;

    language.clear();
    country.clear();
    modifier.clear();
    charset.clear();

    // In case there are several concatenated locale specifications,
    // truncate all but first.
    int f = locale.indexOf(QLatin1Char(':'));
    if (f >= 0) {
        locale.truncate(f);
    }

    f = locale.indexOf(QLatin1Char('.'));
    if (f >= 0) {
        charset = locale.mid(f + 1);
        locale.truncate(f);
    }

    f = locale.indexOf(QLatin1Char('@'));
    if (f >= 0) {
        modifier = locale.mid(f + 1);
        locale.truncate(f);
    }

    f = locale.indexOf(QLatin1Char('_'));
    if (f >= 0) {
        country = locale.mid(f + 1);
        locale.truncate(f);
    }

    language = locale;
}

QString KLocalePrivate::language() const
{
    return m_language;
}

QString KLocalePrivate::country() const
{
    return m_country;
}

QString KLocalePrivate::countryDivisionCode() const
{
    if (m_countryDivisionCode.isEmpty()) {
        return country().toUpper();
    } else {
        return m_countryDivisionCode;
    }
}

KCurrencyCode *KLocalePrivate::currency()
{
    if (!m_currency) {
        initCurrency();
    }
    return m_currency;
}

QString KLocalePrivate::currencyCode() const
{
    return m_currencyCode;
}

void KLocalePrivate::insertCatalog(const QString &catalog)
{
    QMutexLocker lock(kLocaleMutex());
    int pos = m_catalogNames.indexOf(KCatalogName(catalog));
    if (pos != -1) {
        ++m_catalogNames[pos].loadCount;
        return;
    }

    // Insert new catalog just before system catalogs, to preserve the
    // lowest priority of system catalogs.
    m_catalogNames.insert(m_catalogNames.size() - m_numberOfSysCatalogs, KCatalogName(catalog));
    updateCatalogs(); // evaluate the changed list and generate the necessary KCatalog objects
}

void KLocalePrivate::updateCatalogs()
{
    // some changes have occurred. Maybe we have learned or forgotten some languages.
    // Maybe the language precedence has changed.
    // Maybe we have learned or forgotten some catalog names.

    QList<KCatalog> newCatalogs;

    // now iterate over all languages and all wanted catalog names and append or create them in the
    // right order the sequence must be e.g. nds/appname nds/kdelibs nds/kio de/appname de/kdelibs
    // de/kio etc. and not nds/appname de/appname nds/kdelibs de/kdelibs etc. Otherwise we would be
    // in trouble with a language sequende nds,<default>,de. In this case <default> must hide
    // everything after itself in the language list.
    foreach(const QString &lang, m_languageList) {
        if (lang == KLocale::defaultLanguage()) {
            // Default language has no catalogs (messages from the code),
            // so loading catalogs for languages below the default
            // would later confuse the fallback resolution.
            break;
        }
        foreach(const KCatalogName &name, m_catalogNames) {
            // create and add catalog for this name and language if it exists
            if (! KCatalog::catalogLocaleDir(name.name, lang).isEmpty()) {
                newCatalogs.append(KCatalog(name.name, lang));
                //kDebug(173) << "Catalog: " << name << ":" << lang;
            }
        }
    }

    // notify KLocalizedString of catalog update.
    m_catalogs = newCatalogs;
    KLocalizedString::notifyCatalogsUpdated(m_languageList, m_catalogNames);
}

void KLocalePrivate::removeCatalog(const QString &catalog)
{
    QMutexLocker lock(kLocaleMutex());
    int pos = m_catalogNames.indexOf(KCatalogName(catalog));
    if (pos == -1) {
        return;
    }
    if (--m_catalogNames[pos].loadCount > 0) {
        return;
    }
    m_catalogNames.removeAt(pos);
    if (KGlobal::hasMainComponent()) {
        // walk through the KCatalog instances and weed out everything we no longer need
        updateCatalogs();
    }
}

void KLocalePrivate::setActiveCatalog(const QString &catalog)
{
    QMutexLocker lock(kLocaleMutex());
    int pos = m_catalogNames.indexOf(KCatalogName(catalog));
    if (pos == -1) {
        return;
    }
    m_catalogNames.move(pos, 0);
    // walk through the KCatalog instances and adapt to the new order
    updateCatalogs();
}

void KLocalePrivate::translateRawFrom(const char *catname, const char *msgctxt, const char *msgid, const char *msgid_plural,
                                      unsigned long n, QString *language, QString *translation) const
{
    if (!msgid || !msgid[0]) {
        kDebug(173) << "KLocale: trying to look up \"\" in catalog. "
        << "Fix the program" << endl;
        language->clear();
        translation->clear();
        return;
    }
    if (msgctxt && !msgctxt[0]) {
        kDebug(173) << "KLocale: trying to use \"\" as context to message. "
        << "Fix the program" << endl;
    }
    if (msgid_plural && !msgid_plural[0]) {
        kDebug(173) << "KLocale: trying to use \"\" as plural message. "
        << "Fix the program" << endl;
    }

    QMutexLocker locker(kLocaleMutex());
    // determine the fallback string
    QString fallback;
    if (msgid_plural == NULL) {
        fallback = QString::fromUtf8(msgid);
    } else {
        if (n == 1) {
            fallback = QString::fromUtf8(msgid);
        } else {
            fallback = QString::fromUtf8(msgid_plural);
        }
    }
    if (language) {
        *language = KLocale::defaultLanguage();
    }
    if (translation) {
        *translation = fallback;
    }

    // shortcut evaluation if default language is main language: do not consult the catalogs
    if (useDefaultLanguage()) {
        return;
    }

    const QList<KCatalog> catalogList = m_catalogs;
    QString catNameDecoded;
    if (catname != NULL) {
        catNameDecoded = QString::fromUtf8(catname);
    }
    for (QList<KCatalog>::ConstIterator it = catalogList.constBegin(); it != catalogList.constEnd();
         ++it) {
        // shortcut evaluation: once we have arrived at default language, we cannot consult
        // the catalog as it will not have an assiciated mo-file. For this default language we can
        // immediately pick the fallback string.
        if ((*it).language() == KLocale::defaultLanguage()) {
            return;
        }

        if (catNameDecoded.isEmpty() || catNameDecoded == (*it).name()) {
            QString text;
            if (msgctxt != NULL && msgid_plural != NULL) {
                text = (*it).translateStrict(msgctxt, msgid, msgid_plural, n);
            } else if (msgid_plural != NULL) {
                text = (*it).translateStrict(msgid, msgid_plural, n);
            } else if (msgctxt != NULL) {
                text = (*it).translateStrict(msgctxt, msgid);
            } else {
                text = (*it).translateStrict(msgid);
            }

            if (!text.isEmpty()) {
                // we found it
                if (language) {
                    *language = (*it).language();
                }
                if (translation) {
                    *translation = text;
                }
                return;
            }
        }
    }
}

QString KLocalePrivate::translateQt(const char *context, const char *sourceText, const char *comment) const
{
    // Qt's context is normally the name of the class of the method which makes
    // the tr(sourceText) call. However, it can also be manually supplied via
    // translate(context, sourceText) call.
    //
    // Qt's sourceText is the actual message displayed to the user.
    //
    // Qt's comment is an optional argument of tr() and translate(), like
    // tr(sourceText, comment) and translate(context, sourceText, comment).
    //
    // We handle this in the following way:
    //
    // If the comment is given, then it is considered gettext's msgctxt, so a
    // context call is made.
    //
    // If the comment is not given, but context is given, then we treat it as
    // msgctxt only if it was manually supplied (the one in translate()) -- but
    // we don't know this, so we first try a context call, and if translation
    // is not found, we fallback to ordinary call.
    //
    // If neither comment nor context are given, it's just an ordinary call
    // on sourceText.

    if (!sourceText || !sourceText[0]) {
        kDebug(173) << "KLocale: trying to look up \"\" in catalog. "
        << "Fix the program" << endl;
        return QString();
    }

    if (useDefaultLanguage()) {
        return QString();
    }

    QString translation;
    QString language;

    // NOTE: Condition (language != defaultLanguage()) means that translation
    // was found, otherwise we got the original string back as translation.

    if (comment && comment[0]) {
        // Comment given, go for context call.
        translateRawFrom(0, comment, sourceText, 0, 0, &language, &translation);
    } else {
        // Comment not given, go for try-fallback with context.
        if (context && context[0]) {
            translateRawFrom(0, context, sourceText, 0, 0, &language, &translation);
        }
        if (language.isEmpty() || language == defaultLanguage()) {
            translateRawFrom(0, 0, sourceText, 0, 0, &language, &translation);
        }
    }

    if (language != defaultLanguage()) {
        return translation;
    }

    // No proper translation found, return empty according to Qt's expectation.
    return QString();
}

QList<KLocale::DigitSet> KLocalePrivate::allDigitSetsList() const
{
    QList<KLocale::DigitSet> digitSets;
    digitSets.append(KLocale::ArabicDigits);
    digitSets.append(KLocale::ArabicIndicDigits);
    digitSets.append(KLocale::BengaliDigits);
    digitSets.append(KLocale::DevenagariDigits);
    digitSets.append(KLocale::EasternArabicIndicDigits);
    digitSets.append(KLocale::GujaratiDigits);
    digitSets.append(KLocale::GurmukhiDigits);
    digitSets.append(KLocale::KannadaDigits);
    digitSets.append(KLocale::KhmerDigits);
    digitSets.append(KLocale::MalayalamDigits);
    digitSets.append(KLocale::OriyaDigits);
    digitSets.append(KLocale::TamilDigits);
    digitSets.append(KLocale::TeluguDigits);
    digitSets.append(KLocale::ThaiDigits);
    qSort(digitSets);
    return digitSets;
}

QString KLocalePrivate::digitSetString(KLocale::DigitSet digitSet)
{
    switch (digitSet) {
    case KLocale::ArabicIndicDigits:
        return QString::fromUtf8("٠١٢٣٤٥٦٧٨٩");
    case KLocale::BengaliDigits:
        return QString::fromUtf8("০১২৩৪৫৬৭৮৯");
    case KLocale::DevenagariDigits:
        return QString::fromUtf8("०१२३४५६७८९");
    case KLocale::EasternArabicIndicDigits:
        return QString::fromUtf8("۰۱۲۳۴۵۶۷۸۹");
    case KLocale::GujaratiDigits:
        return QString::fromUtf8("૦૧૨૩૪૫૬૭૮૯");
    case KLocale::GurmukhiDigits:
        return QString::fromUtf8("੦੧੨੩੪੫੬੭੮੯");
    case KLocale::KannadaDigits:
        return QString::fromUtf8("೦೧೨೩೪೫೬೭೮೯");
    case KLocale::KhmerDigits:
        return QString::fromUtf8("០១២៣៤៥៦៧៨៩");
    case KLocale::MalayalamDigits:
        return QString::fromUtf8("൦൧൨൩൪൫൬൭൮൯");
    case KLocale::OriyaDigits:
        return QString::fromUtf8("୦୧୨୩୪୫୬୭୮୯");
    case KLocale::TamilDigits:
        return QString::fromUtf8("௦௧௨௩௪௫௬௭௮");
    case KLocale::TeluguDigits:
        return QString::fromUtf8("౦౧౨౩౪౫౬౭౯");
    case KLocale::ThaiDigits:
        return QString::fromUtf8("๐๑๒๓๔๕๖๗๘๙");
    default:
        return QString::fromUtf8("0123456789");
    }
}

QString KLocalePrivate::digitSetToName(KLocale::DigitSet digitSet, bool withDigits) const
{
    QString name;
    switch (digitSet) {
    case KLocale::ArabicIndicDigits:
        name = i18nc("digit set", "Arabic-Indic");
        break;
    case KLocale::BengaliDigits:
        name = i18nc("digit set", "Bengali");
        break;
    case KLocale::DevenagariDigits:
        name = i18nc("digit set", "Devanagari");
        break;
    case KLocale::EasternArabicIndicDigits:
        name = i18nc("digit set", "Eastern Arabic-Indic");
        break;
    case KLocale::GujaratiDigits:
        name = i18nc("digit set", "Gujarati");
        break;
    case KLocale::GurmukhiDigits:
        name = i18nc("digit set", "Gurmukhi");
        break;
    case KLocale::KannadaDigits:
        name = i18nc("digit set", "Kannada");
        break;
    case KLocale::KhmerDigits:
        name = i18nc("digit set", "Khmer");
        break;
    case KLocale::MalayalamDigits:
        name = i18nc("digit set", "Malayalam");
        break;
    case KLocale::OriyaDigits:
        name = i18nc("digit set", "Oriya");
        break;
    case KLocale::TamilDigits:
        name = i18nc("digit set", "Tamil");
        break;
    case KLocale::TeluguDigits:
        name = i18nc("digit set", "Telugu");
        break;
    case KLocale::ThaiDigits:
        name = i18nc("digit set", "Thai");
        break;
    default:
        name = i18nc("digit set", "Arabic");
    }
    if (withDigits) {
        QString digits = digitSetString(digitSet);
        QString nameWithDigits = i18nc("name of digit set with digit string, "
                                       "e.g. 'Arabic (0123456789)'", "%1 (%2)", name, digits);
        return nameWithDigits;
    } else {
        return name;
    }
}

QString KLocalePrivate::convertDigits(const QString &str, KLocale::DigitSet digitSet, bool ignoreContext) const
{
    if (!ignoreContext) {
        // Fall back to Western Arabic digits if requested digit set
        // is not appropriate for current application language.
        // FIXME: Temporary until full language-sensitivity implemented.
        KLocaleStaticData *s = staticData;
        if (m_languageSensitiveDigits && !s->languagesUsingDigitSet[digitSet].contains(m_language)) {
            digitSet = KLocale::ArabicDigits;
        }
    }

    QString nstr;
    QString digitDraw = digitSetString(digitSet);
    foreach(const QChar &c, str) {
        if (c.isDigit()) {
            nstr += digitDraw[c.digitValue()];
        } else {
            nstr += c;
        }
    }
    return nstr;
}

QString KLocalePrivate::toArabicDigits(const QString &str)
{
    QString nstr;
    foreach(const QChar &c, str) {
        if (c.isDigit()) {
            nstr += QChar('0' + c.digitValue());
        } else {
            nstr += c;
        }
    }
    return nstr;
}

bool KLocalePrivate::nounDeclension() const
{
    return m_nounDeclension;
}

bool KLocalePrivate::dateMonthNamePossessive() const
{
    return m_dateMonthNamePossessive;
}

int KLocalePrivate::weekStartDay() const
{
    return m_weekStartDay;
}

int KLocalePrivate::workingWeekStartDay() const
{
    return m_workingWeekStartDay;
}

int KLocalePrivate::workingWeekEndDay() const
{
    return m_workingWeekEndDay;
}

int KLocalePrivate::weekDayOfPray() const
{
    return m_weekDayOfPray;
}

int KLocalePrivate::decimalPlaces() const
{
    return m_decimalPlaces;
}

QString KLocalePrivate::decimalSymbol() const
{
    return m_decimalSymbol;
}

QString KLocalePrivate::thousandsSeparator() const
{
    return m_thousandsSeparator;
}

QList<int> KLocalePrivate::numericDigitGrouping() const
{
    return m_numericDigitGrouping;
}

QString KLocalePrivate::currencySymbol() const
{
    return m_currencySymbol;
}

QString KLocalePrivate::monetaryDecimalSymbol() const
{
    return m_monetaryDecimalSymbol;
}

QString KLocalePrivate::monetaryThousandsSeparator() const
{
    return m_monetaryThousandsSeparator;
}

QList<int> KLocalePrivate::monetaryDigitGrouping() const
{
    return m_monetaryDigitGrouping;
}

QString KLocalePrivate::positiveSign() const
{
    return m_positiveSign;
}

QString KLocalePrivate::negativeSign() const
{
    return m_negativeSign;
}

/* Just copy to keep the diff looking clean, delete later
int KLocale::fracDigits() const
{
    return monetaryDecimalPlaces();
}
*/

int KLocalePrivate::monetaryDecimalPlaces() const
{
    return m_monetaryDecimalPlaces;
}

bool KLocalePrivate::positivePrefixCurrencySymbol() const
{
    return m_positivePrefixCurrencySymbol;
}

bool KLocalePrivate::negativePrefixCurrencySymbol() const
{
    return m_negativePrefixCurrencySymbol;
}

KLocale::SignPosition KLocalePrivate::positiveMonetarySignPosition() const
{
    return m_positiveMonetarySignPosition;
}

KLocale::SignPosition KLocalePrivate::negativeMonetarySignPosition() const
{
    return m_negativeMonetarySignPosition;
}

static inline void put_it_in(QChar *buffer, int &index, const QString &s)
{
    for (int l = 0; l < s.length(); l++) {
        buffer[index++] = s.at(l);
    }
}

static inline void put_it_in(QChar *buffer, int &index, int number)
{
    buffer[index++] = number / 10 + '0';
    buffer[index++] = number % 10 + '0';
}

// Convert POSIX Digit Group Format string into a Qlist<int>, e.g. "3;2" converts to (3,2)
QList<int> KLocalePrivate::digitGroupFormatToList(const QString &digitGroupFormat) const
{
    QList<int> groupList;
    QStringList stringList = digitGroupFormat.split(QLatin1Char(';'));
    foreach(const QString &size, stringList) {
        groupList.append(size.toInt());
    }
    return groupList;
}

// Inserts all required occurrences of the group separator into a number string.
QString KLocalePrivate::formatDigitGroup(const QString &number, const QString &groupSeparator, const QString &decimalSeperator, QList<int> groupList) const
{
    if (groupList.isEmpty() || groupSeparator.isEmpty()) {
        return number;
    }

    QString num = number;
    int groupCount = groupList.count();
    int groupAt = 0;
    int groupSize = groupList.at(groupAt);
    int pos = num.indexOf(decimalSeperator);
    if (pos == -1) {
        pos = num.length();
    }
    pos = pos - groupSize;

    while (pos > 0 && groupSize > 0) {
        num.insert(pos, groupSeparator);
        if (groupAt + 1 < groupCount) {
            ++groupAt;
            groupSize = groupList.at(groupAt);
        }
        pos = pos - groupSize;
    }

    return num;
}

// Strips all occurrences of the group separator from a number, returns ok if the separators were all in the valid positions
QString KLocalePrivate::parseDigitGroup(const QString &number, const QString &groupSeparator, const QString &decimalSeparator, QList<int> groupList, bool *ok) const
{
    QString num = number;
    bool valid = true;

    if (!groupSeparator.isEmpty()) {
        if (!groupList.isEmpty()) {
            int separatorSize = groupSeparator.length();
            int groupCount = groupList.count();
            int groupAt = 0;
            int groupSize = groupList.at(groupAt);
            int pos = number.indexOf(decimalSeparator);
            if (pos == -1) {
                pos = number.length();
            }
            pos = pos - groupSize - separatorSize;

            while (pos > 0 && valid && groupSize > 0) {
                if (num.mid(pos, separatorSize) == groupSeparator) {
                    num.remove(pos, separatorSize);
                    if (groupAt + 1 < groupCount) {
                        ++groupAt;
                        groupSize = groupList.at(groupAt);
                    }
                    pos = pos - groupSize - separatorSize;
                } else {
                    valid = false;
                }
            }
        }

        if (num.contains(groupSeparator)) {
            valid = false;
            num = num.remove(groupSeparator);
        }
    }

    if (ok) {
        *ok = valid;
    }

    return num;
}

QString KLocalePrivate::formatMoney(double num, const QString &symbol, int precision) const
{
    // some defaults
    QString currencyString = symbol;
    if (symbol.isNull()) {
        currencyString = currencySymbol();
    }
    if (precision < 0) {
        precision = monetaryDecimalPlaces();
    }

    // the number itself
    bool neg = num < 0;
    QString res = QString::number(neg ? -num : num, 'f', precision);

    // Replace dot with locale decimal separator
    res.replace(QLatin1Char('.'), monetaryDecimalSymbol());

    // Insert the thousand separators
    res = formatDigitGroup(res, monetaryThousandsSeparator(), monetaryDecimalSymbol(), monetaryDigitGrouping());

    // set some variables we need later
    int signpos = neg
                  ? negativeMonetarySignPosition()
                  : positiveMonetarySignPosition();
    QString sign = neg
                   ? negativeSign()
                   : positiveSign();

    switch (signpos) {
    case KLocale::ParensAround:
        res.prepend(QLatin1Char('('));
        res.append(QLatin1Char(')'));
        break;
    case KLocale::BeforeQuantityMoney:
        res.prepend(sign);
        break;
    case KLocale::AfterQuantityMoney:
        res.append(sign);
        break;
    case KLocale::BeforeMoney:
        currencyString.prepend(sign);
        break;
    case KLocale::AfterMoney:
        currencyString.append(sign);
        break;
    }

    if (neg ? negativePrefixCurrencySymbol() :
            positivePrefixCurrencySymbol()) {
        res.prepend(QLatin1Char(' '));
        res.prepend(currencyString);
    } else {
        res.append(QLatin1Char(' '));
        res.append(currencyString);
    }

    // Convert to target digit set.
    res = convertDigits(res, m_monetaryDigitSet);

    return res;
}


QString KLocalePrivate::formatNumber(double num, int precision) const
{
    if (precision < 0) {
        precision = decimalPlaces();
    }
    // no need to round since QString::number does this for us
    return formatNumber(QString::number(num, 'f', precision), false, 0);
}

QString KLocalePrivate::formatLong(long num) const
{
    return formatNumber((double)num, 0);
}

// increase the digit at 'position' by one
static void _inc_by_one(QString &str, int position)
{
    for (int i = position; i >= 0; i--) {
        char last_char = str[i].toLatin1();
        switch (last_char) {
        case '0':
            str[i] = '1';
            break;
        case '1':
            str[i] = '2';
            break;
        case '2':
            str[i] = '3';
            break;
        case '3':
            str[i] = '4';
            break;
        case '4':
            str[i] = '5';
            break;
        case '5':
            str[i] = '6';
            break;
        case '6':
            str[i] = '7';
            break;
        case '7':
            str[i] = '8';
            break;
        case '8':
            str[i] = '9';
            break;
        case '9':
            str[i] = '0';
            if (i == 0) str.prepend(QLatin1Char('1'));
            continue;
        case '.':
            continue;
        }
        break;
    }
}

// Cut off if more digits in fractional part than 'precision'
static void _round(QString &str, int precision)
{
    int decimalSymbolPos = str.indexOf(QLatin1Char('.'));

    if (decimalSymbolPos == -1) {
        if (precision == 0)  return;
        else if (precision > 0) { // add dot if missing (and needed)
            str.append(QLatin1Char('.'));
            decimalSymbolPos = str.length() - 1;
        }
    }
    // fill up with more than enough zeroes (in case fractional part too short)
    str.reserve(str.length() + precision);
    for (int i = 0; i < precision; ++i)
        str.append(QLatin1Char('0'));

    // Now decide whether to round up or down
    char last_char = str[decimalSymbolPos + precision + 1].toLatin1();
    switch (last_char) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
        // nothing to do, rounding down
        break;
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        _inc_by_one(str, decimalSymbolPos + precision);
        break;
    default:
        break;
    }

    decimalSymbolPos = str.indexOf(QLatin1Char('.'));
    str.truncate(decimalSymbolPos + precision + 1);

    // if precision == 0 delete also '.'
    if (precision == 0) {
        str = str.left(decimalSymbolPos);
    }

    str.squeeze();
}

QString KLocalePrivate::formatNumber(const QString &numStr, bool round, int precision) const
{
    QString tmpString = numStr;

    if (precision < 0) {
        precision = decimalPlaces();
    }

    // Skip the sign (for now)
    const bool neg = (tmpString[0] == QLatin1Char('-'));
    if (neg || tmpString[0] == QLatin1Char('+')) {
        tmpString.remove(0, 1);
    }

    //kDebug(173)<<"tmpString:"<<tmpString;

    // Split off exponential part (including 'e'-symbol)
    const int expPos = tmpString.indexOf(QLatin1Char('e')); // -1 if not found
    QString mantString = tmpString.left(expPos); // entire string if no 'e' found
    QString expString;
    if (expPos > -1) {
        expString = tmpString.mid(expPos); // includes the 'e', or empty if no 'e'
        if (expString.length() == 1) {
            expString.clear();
        }
    }

    //kDebug(173)<<"mantString:"<<mantString;
    //kDebug(173)<<"expString:"<<expString;
    if (mantString.isEmpty() || !mantString[0].isDigit()) {// invalid number
        mantString = QLatin1Char('0');
    }

    if (round) {
        _round(mantString, precision);
    }

    // Replace dot with locale decimal separator
    mantString.replace(QLatin1Char('.'), decimalSymbol());

    // Insert the thousand separators
    mantString = formatDigitGroup(mantString, thousandsSeparator(), decimalSymbol(), numericDigitGrouping());

    // How can we know where we should put the sign?
    mantString.prepend(neg ? negativeSign() : positiveSign());

    // Convert to target digit set.
    if (digitSet() != KLocale::ArabicDigits) {
        mantString = convertDigits(mantString, digitSet());
        expString = convertDigits(expString, digitSet());
    }

    return mantString + expString;
}

// Returns a list of already translated units to use later in formatByteSize
// and friends.  Account for every unit in KLocale::BinarySizeUnits
QList<QString> KLocalePrivate::dialectUnitsList(KLocale::BinaryUnitDialect dialect)
{
    QList<QString> binaryUnits;
    QString s; // Used in CACHE_BYTE_FMT macro defined shortly

    // Adds a given translation to the binaryUnits list.
#define CACHE_BYTE_FMT(ctxt_text) \
        translateRawFrom(0, ctxt_text, 0, 0, 0, &s); \
        binaryUnits.append(s);

    // Do not remove i18n: comments below, they are used by the
    // translators.

    // This prefix is shared by all current dialects.
    // i18n: Dumb message, avoid any markup or scripting.
    CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in bytes", "%1 B"));

    switch (dialect) {
    case KLocale::MetricBinaryDialect:
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 1000 bytes", "%1 kB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^6 bytes", "%1 MB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^9 bytes", "%1 GB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^12 bytes", "%1 TB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^15 bytes", "%1 PB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^18 bytes", "%1 EB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^21 bytes", "%1 ZB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 10^24 bytes", "%1 YB"));
        break;

    case KLocale::JEDECBinaryDialect:
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 1024 bytes", "%1 KB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^20 bytes", "%1 MB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^30 bytes", "%1 GB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^40 bytes", "%1 TB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^50 bytes", "%1 PB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^60 bytes", "%1 EB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^70 bytes", "%1 ZB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("memory size in 2^80 bytes", "%1 YB"));
        break;

    case KLocale::IECBinaryDialect:
    default:
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 1024 bytes", "%1 KiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^20 bytes", "%1 MiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^30 bytes", "%1 GiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^40 bytes", "%1 TiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^50 bytes", "%1 PiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^60 bytes", "%1 EiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^70 bytes", "%1 ZiB"));
        // i18n: Dumb message, avoid any markup or scripting.
        CACHE_BYTE_FMT(I18N_NOOP2_NOSTRIP("size in 2^80 bytes", "%1 YiB"));
        break;
    }

    return binaryUnits;
}

QString KLocalePrivate::formatByteSize(double size, int precision, KLocale::BinaryUnitDialect dialect,
                                       KLocale::BinarySizeUnits specificUnit)
{
    // Error checking
    if (dialect <= KLocale::DefaultBinaryDialect || dialect > KLocale::LastBinaryDialect) {
        dialect = m_binaryUnitDialect;
    }

    if (specificUnit < KLocale::DefaultBinaryUnits || specificUnit > KLocale::UnitLastUnit) {
        specificUnit = KLocale::DefaultBinaryUnits;
    }

    // Choose appropriate units.
    QList<QString> dialectUnits;
    if (dialect == m_binaryUnitDialect) {
        // Cache default units for speed
        if (m_byteSizeFmt.size() == 0) {
            QMutexLocker lock(kLocaleMutex());

            // We only cache the user's default dialect.
            m_byteSizeFmt = dialectUnitsList(m_binaryUnitDialect);
        }

        dialectUnits = m_byteSizeFmt;
    } else {
        dialectUnits = dialectUnitsList(dialect);
    }

    int unit = 0; // Selects what unit to use from cached list
    double multiplier = 1024.0;

    if (dialect == KLocale::MetricBinaryDialect) {
        multiplier = 1000.0;
    }

    // If a specific unit conversion is given, use it directly.  Otherwise
    // search until the result is in [0, multiplier) (or out of our range).
    if (specificUnit == KLocale::DefaultBinaryUnits) {
        while (qAbs(size) >= multiplier && unit < (int) KLocale::UnitYottaByte) {
            size /= multiplier;
            unit++;
        }
    } else {
        // A specific unit is in use
        unit = static_cast<int>(specificUnit);
        if (unit > 0) {
            size /= pow(multiplier, unit);
        }
    }

    if (unit == 0) {
        // Bytes, no rounding
        return dialectUnits[unit].arg(formatNumber(size, 0));
    }

    return dialectUnits[unit].arg(formatNumber(size, precision));
}

QString KLocalePrivate::formatByteSize(double size)
{
    return formatByteSize(size, 1);
}

KLocale::BinaryUnitDialect KLocalePrivate::binaryUnitDialect() const
{
    return m_binaryUnitDialect;
}

void KLocalePrivate::setBinaryUnitDialect(KLocale::BinaryUnitDialect newDialect)
{
    if (newDialect > KLocale::DefaultBinaryDialect && newDialect <= KLocale::LastBinaryDialect) {
        QMutexLocker lock(kLocaleMutex());
        m_binaryUnitDialect = newDialect;
        m_byteSizeFmt.clear(); // Reset cached translations.
    }
}

QString KLocalePrivate::formatDuration(unsigned long mSec) const
{
    if (mSec >= 24*3600000) {
        return i18nc("@item:intext %1 is a real number, e.g. 1.23 days", "%1 days",
                     formatNumber(mSec / (24 * 3600000.0), 2));
    } else if (mSec >= 3600000) {
        return i18nc("@item:intext %1 is a real number, e.g. 1.23 hours", "%1 hours",
                     formatNumber(mSec / 3600000.0, 2));
    } else if (mSec >= 60000) {
        return i18nc("@item:intext %1 is a real number, e.g. 1.23 minutes", "%1 minutes",
                     formatNumber(mSec / 60000.0, 2));
    } else if (mSec >= 1000) {
        return i18nc("@item:intext %1 is a real number, e.g. 1.23 seconds", "%1 seconds",
                     formatNumber(mSec / 1000.0, 2));
    }
    return i18ncp("@item:intext", "%1 millisecond", "%1 milliseconds", mSec);
}

QString KLocalePrivate::formatSingleDuration(KLocalePrivate::DurationType durationType, int n)
{
    switch (durationType) {
    case KLocalePrivate::DaysDurationType:
        return i18ncp("@item:intext", "1 day", "%1 days", n);
    case KLocalePrivate::HoursDurationType:
        return i18ncp("@item:intext", "1 hour", "%1 hours", n);
    case KLocalePrivate::MinutesDurationType:
        return i18ncp("@item:intext", "1 minute", "%1 minutes", n);
    case KLocalePrivate::SecondsDurationType:
        return i18ncp("@item:intext", "1 second", "%1 seconds", n);
    }
    return QString();
}

QString KLocalePrivate::prettyFormatDuration(unsigned long mSec) const
{
    unsigned long ms = mSec;
    int days = ms / (24 * 3600000);
    ms = ms % (24 * 3600000);
    int hours = ms / 3600000;
    ms = ms % 3600000;
    int minutes = ms / 60000;
    ms = ms % 60000;
    int seconds = qRound(ms / 1000.0);

    // Handle correctly problematic case #1 (look at KLocaleTest::prettyFormatDuration()
    // at klocaletest.cpp)
    if (seconds == 60) {
        return prettyFormatDuration(mSec - ms + 60000);
    }

    if (days && hours) {
        return i18nc("@item:intext days and hours. This uses the previous item:intext messages. If this does not fit the grammar of your language please contact the i18n team to solve the problem",
                     "%1 and %2", formatSingleDuration(KLocalePrivate::DaysDurationType, days),
                     formatSingleDuration(KLocalePrivate::HoursDurationType, hours));
    } else if (days) {
        return formatSingleDuration(KLocalePrivate::DaysDurationType, days);
    } else if (hours && minutes) {
        return i18nc("@item:intext hours and minutes. This uses the previous item:intext messages. If this does not fit the grammar of your language please contact the i18n team to solve the problem",
                     "%1 and %2",
                     formatSingleDuration(KLocalePrivate::HoursDurationType, hours),
                     formatSingleDuration(KLocalePrivate::MinutesDurationType, minutes));
    } else if (hours) {
        return formatSingleDuration(KLocalePrivate::HoursDurationType, hours);
    } else if (minutes && seconds) {
        return i18nc("@item:intext minutes and seconds. This uses the previous item:intext messages. If this does not fit the grammar of your language please contact the i18n team to solve the problem",
                     "%1 and %2",
                     formatSingleDuration(KLocalePrivate::MinutesDurationType, minutes),
                     formatSingleDuration(KLocalePrivate::SecondsDurationType, seconds));
    } else if (minutes) {
        return formatSingleDuration(KLocalePrivate::MinutesDurationType, minutes);
    } else {
        return formatSingleDuration(KLocalePrivate::SecondsDurationType, seconds);
    }
}

QString KLocalePrivate::formatDate(const QDate &date, KLocale::DateFormat format)
{
    return calendar()->formatDate(date, format);
}

void KLocalePrivate::setMainCatalog(const char *catalog)
{
    KLocaleStaticData *s = staticData;
    s->maincatalog = QString::fromUtf8(catalog);
}

double KLocalePrivate::readNumber(const QString &_str, bool * ok) const
{
    QString str = _str.trimmed();
    bool neg = false;

    // Check negative or positive signs
    // Assumes blank sign is positive even if pos sign set, unless already taken by negative
    if (!negativeSign().isEmpty() && str.indexOf(negativeSign()) == 0) {
        neg = true;
        str.remove(0, negativeSign().length());
        str = str.trimmed();
    } else if (!positiveSign().isEmpty() && str.indexOf(positiveSign()) == 0) {
        neg = false;
        str.remove(0, positiveSign().length());
        str = str.trimmed();
    } else if (negativeSign().isEmpty() && str[0].isDigit()) {
        neg = true;
    }

    /* will hold the scientific notation portion of the number.
       Example, with 2.34E+23, exponentialPart == "E+23"
    */
    QString exponentialPart;
    int EPos;

    EPos = str.indexOf(QLatin1Char('E'), 0, Qt::CaseInsensitive);

    if (EPos != -1) {
        exponentialPart = str.mid(EPos);
        str = str.left(EPos);
        str = str.trimmed();
    }

    // Remove group separators
    bool groupOk = true;
    if(str.contains(thousandsSeparator())) {
        str = parseDigitGroup(str, thousandsSeparator(), decimalSymbol(),
                              numericDigitGrouping(), &groupOk);
    }

    if (!groupOk) {
        if (ok) {
            *ok = false;
        }
        return 0.0;
    }

    int pos = str.indexOf(decimalSymbol());
    QString major;
    QString minor;
    if (pos == -1) {
        major = str;
    } else {
        major = str.left(pos);
        minor = str.mid(pos + decimalSymbol().length());
    }

    // Check the major and minor parts are only digits
    bool digitTest = true;
    foreach (const QChar &ch, major) {
        if (!ch.isDigit()) {
            digitTest = false;
            break;
        }
    }
    foreach (const QChar &ch, minor) {
        if (!ch.isDigit()) {
            digitTest = false;
            break;
        }
    }
    if (!digitTest) {
        if (ok) {
            *ok = false;
        }
        return 0.0;
    }

    QString tot;
    if (neg) {
        tot = QLatin1Char('-');
    }
    tot += major + QLatin1Char('.') + minor + exponentialPart;
    tot = toArabicDigits(tot);
    return tot.toDouble(ok);
}

double KLocalePrivate::readMoney(const QString &_str, bool *ok) const
{
    QString str = _str.trimmed();
    bool neg = false;
    bool currencyFound = false;
    QString symbol = currencySymbol();

    // First try removing currency symbol from either end
    int pos = str.indexOf(symbol);
    if (pos == 0 || pos == (int) str.length() - symbol.length()) {
        str.remove(pos, symbol.length());
        str = str.trimmed();
        currencyFound = true;
    }
    if (str.isEmpty()) {
        if (ok) {
            *ok = false;
        }
        return 0;
    }

    // Then try removing sign from either end (with a special case for parenthesis)
    if (str[0] == QLatin1Char('(') && str[str.length()-1] == QLatin1Char(')')) {
        if (positiveMonetarySignPosition() != KLocale::ParensAround) {
            neg = true;
        }
        str.remove(str.length() - 1, 1);
        str.remove(0, 1);
        str = str.trimmed();
    } else {
        int len = 0;
        QString sign;
        int negLen = negativeSign().length();
        QString negSign = negativeSign();
        if (!negSign.isEmpty() && (str.left(negLen) == negSign || str.right(negSign.length()) == negSign)) {
            neg = true;
            len = negLen;
            sign = negSign;
        } else {
            int posLen = positiveSign().length();
            QString posSign = positiveSign();
            if (!posSign.isEmpty() && (str.left(posLen) == posSign || str.right(posSign.length()) == posSign)) {
                len = posLen;
                sign = posSign;
            } else if (negSign.isEmpty() && str[0].isDigit() && str[str.length() - 1].isDigit()){
                neg = true;
            }
        }
        if (!sign.isEmpty()) {
            if (str.left(len) == sign) {
                str.remove(0, len);
            } else {
                str.remove(str.length() - len, len);
            }
            str = str.trimmed();
        }
    }

    // Finally try again for the currency symbol, if we didn't find
    // it already (because of the negative sign being in the way).
    if (!currencyFound) {
        pos = str.indexOf(symbol);
        if (pos == 0 || pos == (int) str.length() - symbol.length()) {
            str.remove(pos, symbol.length());
            str = str.trimmed();
        }
    }

    // Remove group separators
    bool groupOk = true;
    if(str.contains(monetaryThousandsSeparator())) {
        str = parseDigitGroup(str, monetaryThousandsSeparator(), monetaryDecimalSymbol(),
                              monetaryDigitGrouping(), &groupOk);
    }

    if (!groupOk) {
        if (ok) {
            *ok = false;
        }
        return 0.0;
    }

    // And parse the rest as a number
    pos = str.indexOf(monetaryDecimalSymbol());
    QString major;
    QString minor;
    if (pos == -1) {
        major = str;
    } else {
        major = str.left(pos);
        minor = str.mid(pos + monetaryDecimalSymbol().length());
    }

    // Check the major and minor parts are only digits
    bool digitTest = true;
    foreach (const QChar &ch, major) {
        if (!ch.isDigit()) {
            digitTest = false;
            break;
        }
    }
    foreach (const QChar &ch, minor) {
        if (!ch.isDigit()) {
            digitTest = false;
            break;
        }
    }
    if (!digitTest) {
        if (ok) {
            *ok = false;
        }
        return 0.0;
    }

    QString tot;
    if (neg) {
        tot = QLatin1Char('-');
    }
    tot += major + QLatin1Char('.') + minor;
    tot = toArabicDigits(tot);
    return tot.toDouble(ok);
}

/**
 * helper function to read integers
 * @param str
 * @param pos the position to start at. It will be updated when we parse it.
 * @return the integer read in the string, or -1 if no string
 */
static int readInt(const QString &str, int &pos)
{
    if (!str.at(pos).isDigit()) {
        return -1;
    }
    int result = 0;
    for (; str.length() > pos && str.at(pos).isDigit(); ++pos) {
        result *= 10;
        result += str.at(pos).digitValue();
    }

    return result;
}

QDate KLocalePrivate::readDate(const QString &intstr, bool *ok)
{
    return calendar()->readDate(intstr, ok);
}

QDate KLocalePrivate::readDate(const QString &intstr, KLocale::ReadDateFlags flags, bool *ok)
{
    return calendar()->readDate(intstr, flags, ok);
}

QDate KLocalePrivate::readDate(const QString &intstr, const QString &fmt, bool *ok)
{
    return calendar()->readDate(intstr, fmt, ok);
}

QTime KLocalePrivate::readTime(const QString &intstr, bool *ok) const
{
    QTime time = readLocaleTime(intstr, ok, KLocale::TimeDefault, KLocale::ProcessStrict);
    if (time.isValid()) {
        return time;
    }
    return readLocaleTime(intstr, ok, KLocale::TimeWithoutSeconds, KLocale::ProcessStrict);
}

QTime KLocalePrivate::readTime(const QString &intstr, KLocale::ReadTimeFlags flags, bool *ok) const
{
    return readLocaleTime(intstr, ok, (flags == KLocale::WithSeconds) ? KLocale::TimeDefault : KLocale::TimeWithoutSeconds,
                          KLocale::ProcessStrict);
}

// remove the first occurrence of the 2-character string
// strip2char from inout and if found, also remove one preceding
// punctuation character and arbitrary number of spaces.
static void stripStringAndPreceedingSeparator(QString &inout, const QLatin1String &strip2char)
{
    int remPos = inout.indexOf(strip2char);
    if (remPos == -1) {
        return;
    }
    int endPos = remPos + 2;
    int curPos = remPos - 1;
    while (curPos >= 0 && inout.at(curPos).isSpace()) {
        curPos--;
    }
    // remove the separator sign before the seconds
    // and assume that works everywhere
    if (curPos >= 0 && inout.at(curPos).isPunct() && inout.at(curPos) != QLatin1Char('%')) {
        curPos--;
    }
    while (curPos >= 0 && inout.at(curPos).isSpace()) {
        curPos--;
    }

    remPos = qMax(curPos + 1, 0);
    inout.remove(remPos, endPos - remPos);
}

// remove the first occurrence of the 2-character string
// strip2char from inout and if found, also remove one
// succeeding punctuation character and arbitrary number of spaces.
static void stripStringAndSucceedingSeparator(QString &inout, const QLatin1String &strip2char)
{
    int remPos = inout.indexOf(strip2char);
    if (remPos == -1) {
        return;
    }
    int curPos = remPos + 2;
    while (curPos < inout.size() &&
           (inout.at(curPos).isSpace() ||
            (inout.at(curPos).isPunct() && inout.at(curPos) != QLatin1Char('%')))) {
        curPos++;
    }
    inout.remove(remPos, curPos - remPos);
}

// remove the first occurrence of "%p" from the inout.
static void stripAmPmFormat(QString &inout)
{
    // NOTE: this function assumes that %p - if it's present -
    //       is either the first or the last element of the format
    //       string. Either a succeeding or a preceding
    //       punctuation symbol is stripped.
    int length = inout.size();
    int ppos = inout.indexOf(QLatin1String("%p"));
    if (ppos == -1) {
        return;
    } else if (ppos == 0) {
        // first element, eat succeeding punctuation and spaces
        ppos = 2;
        while (ppos < length && (inout.at(ppos).isSpace() || inout.at(ppos).isPunct()) &&
                inout.at(ppos) != QLatin1Char('%')) {
            ppos++;
        }
        inout = inout.mid(ppos);
    } else {
        stripStringAndPreceedingSeparator(inout, QLatin1String("%p"));
    }
}

QTime KLocalePrivate::readLocaleTime(const QString &intstr, bool *ok, KLocale::TimeFormatOptions options,
                                     KLocale::TimeProcessingOptions processing) const
{
    QString str(intstr.simplified().toLower());
    QString format(timeFormat().simplified());

    int hour = -1;
    int minute = -1;
    int second = -1;
    bool useDayPeriod = false;
    KDayPeriod dayPeriod = dayPeriodForTime(QTime(0,0,0));
    int strpos = 0;
    int formatpos = 0;
    bool error = false;

    bool excludeSecs = ((options & KLocale::TimeWithoutSeconds) == KLocale::TimeWithoutSeconds);
    bool isDuration = ((options & KLocale::TimeDuration) == KLocale::TimeDuration);
    bool noAmPm = ((options & KLocale::TimeWithoutAmPm) == KLocale::TimeWithoutAmPm);
    bool foldHours = ((options & KLocale::TimeFoldHours) == KLocale::TimeFoldHours);
    bool strict = ((processing & KLocale::ProcessStrict) == KLocale::ProcessStrict);

    // if seconds aren't needed, strip them from the timeFormat
    if (excludeSecs) {
        stripStringAndPreceedingSeparator(format, QLatin1String("%S"));
        second = 0; // seconds are always 0
    }

    // if hours are folded, strip them from the timeFormat
    if (foldHours) {
        stripStringAndSucceedingSeparator(format, QLatin1String("%H"));
        stripStringAndSucceedingSeparator(format, QLatin1String("%k"));
        stripStringAndSucceedingSeparator(format, QLatin1String("%I"));
        stripStringAndSucceedingSeparator(format, QLatin1String("%l"));
    }

    // if am/pm isn't needed, strip it from the timeFormat
    if (noAmPm) {
        stripAmPmFormat(format);
    }

    while (!error && (format.length() > formatpos || str.length() > strpos)) {
        if (!(format.length() > formatpos && str.length() > strpos)) {
            error = true;
            break;
        }

        QChar c = format.at(formatpos++);
        if (c.isSpace()) {
            if (strict) { // strict processing: space is needed
                if (!str.at(strpos).isSpace()) {
                    error = true;
                    break;
                }
                strpos++;
            } else { // lax processing: space in str not needed
                // 1 space maximum as str is simplified
                if (str.at(strpos).isSpace()) {
                    strpos++;
                }
            }
            continue;
        }

        if (c != QLatin1Char('%')) {
            if (c != str.at(strpos++)) {
                error = true;
                break;
            }
            continue;
        }

        c = format.at(formatpos++);
        switch (c.unicode()) {

        case 'p': // Day Period, normally AM/PM
        case 'P': // Lowercase Day Period, normally am/pm
        {
            error = true;
            foreach (const KDayPeriod &testDayPeriod, dayPeriods()) {
                QString dayPeriodText = testDayPeriod.periodName(KLocale::ShortName);
                int len = dayPeriodText.length();
                if (str.mid(strpos, len) == dayPeriodText.toLower()) {
                    dayPeriod = testDayPeriod;
                    strpos += len;
                    error = false;
                    useDayPeriod = true;
                    break;
                }
            }
            break;
        }

        case 'k':  // 24h Hours Short Number
        case 'H':  // 24h Hours Long Number
            useDayPeriod = false;
            hour = readInt(str, strpos);
            break;

        case 'l': // 12h Hours Short Number
        case 'I': // 12h Hours Long Number
            useDayPeriod = !isDuration;
            hour = readInt(str, strpos);
            break;

        case 'M':
            minute = readInt(str, strpos);
            // minutes can be bigger than 59 if hours are folded
            if (foldHours) {
                // if hours are folded, make sure minutes doesn't get bigger than 59.
                hour = minute / 60;
                minute = minute % 60;
            }
            break;

        case 'S':
            second = readInt(str, strpos);
            break;
        }

        // NOTE: if anything is performed inside this loop, be sure to
        //       check for error!
    }

    QTime returnTime;
    if (!error) {
        if (useDayPeriod) {
            returnTime = dayPeriod.time(hour, minute, second);
        } else {
            returnTime = QTime(hour, minute, second);
        }
    }
    if (ok) {
        *ok = returnTime.isValid();
    }
    return returnTime;
}

QString KLocalePrivate::formatTime(const QTime &time, bool includeSecs, bool isDuration) const
{
    KLocale::TimeFormatOptions options = KLocale::TimeDefault;
    if (!includeSecs) {
        options |= KLocale::TimeWithoutSeconds;
    }
    if (isDuration) {
        options |= KLocale::TimeDuration;
    }
    return formatLocaleTime(time, options);
}

QString KLocalePrivate::formatLocaleTime(const QTime &time, KLocale::TimeFormatOptions options) const
{
    QString rst(timeFormat());

    bool excludeSecs = ((options & KLocale::TimeWithoutSeconds) == KLocale::TimeWithoutSeconds);
    bool isDuration = ((options & KLocale::TimeDuration) == KLocale::TimeDuration);
    bool noAmPm = ((options & KLocale::TimeWithoutAmPm) == KLocale::TimeWithoutAmPm);
    bool foldHours = ((options & KLocale::TimeFoldHours) == KLocale::TimeFoldHours);

    // if seconds aren't needed, strip them from the timeFormat
    if (excludeSecs) {
        stripStringAndPreceedingSeparator(rst, QLatin1String("%S"));
    }

    // if hours should be folded, strip all hour symbols from the timeFormat
    if (foldHours) {
        stripStringAndSucceedingSeparator(rst, QLatin1String("%H"));
        stripStringAndSucceedingSeparator(rst, QLatin1String("%k"));
        stripStringAndSucceedingSeparator(rst, QLatin1String("%I"));
        stripStringAndSucceedingSeparator(rst, QLatin1String("%l"));
    }

    // if am/pm isn't needed, strip it from the timeFormat
    if (noAmPm) {
        stripAmPmFormat(rst);
    }

    // only "pm/am" and %M here can grow, the rest shrinks, but
    // I'm rather safe than sorry
    QChar *buffer = new QChar[rst.length() * 3 / 2 + 32];

    int index = 0;
    bool escape = false;
    int number = 0;

    for (int format_index = 0; format_index < rst.length(); format_index++) {
        if (!escape) {
            if (rst.at(format_index).unicode() == '%') {
                escape = true;
            } else {
                buffer[index++] = rst.at(format_index);
            }
        } else {
            switch (rst.at(format_index).unicode()) {
            case '%':
                buffer[index++] = QLatin1Char('%');
                break;
            case 'H':
                put_it_in(buffer, index, time.hour());
                break;
            case 'I':
                if (isDuration) {
                    put_it_in(buffer, index, time.hour());
                } else {
                    put_it_in(buffer, index, dayPeriodForTime(time).hourInPeriod(time));
                }
                break;
            case 'M':
                if (foldHours) {
                    put_it_in(buffer, index, QString::number(time.hour() * 60 + time.minute()));
                } else {
                    put_it_in(buffer, index, time.minute());
                }
                break;
            case 'S':
                put_it_in(buffer, index, time.second());
                break;
            case 'k':
            case 'l':
                // to share the code
                if (!isDuration && rst.at(format_index).unicode() == 'l') {
                    number = dayPeriodForTime(time).hourInPeriod(time);
                } else {
                    number = time.hour();
                }
                if (number / 10) {
                    buffer[index++] = number / 10 + '0';
                }
                buffer[index++] = number % 10 + '0';
                break;
            case 'p':
            {
                put_it_in(buffer, index, dayPeriodForTime(time).periodName(KLocale::ShortName));
                break;
            }
            default:
                buffer[index++] = rst.at(format_index);
                break;
            }
            escape = false;
        }
    }
    QString ret(buffer, index);
    delete [] buffer;
    ret = convertDigits(ret, dateTimeDigitSet());
    return ret.trimmed();
}

bool KLocalePrivate::use12Clock() const
{
    if ((timeFormat().contains(QString::fromLatin1("%I")) > 0) ||
        (timeFormat().contains(QString::fromLatin1("%l")) > 0)) {
        return true;
    } else {
        return false;
    }
}

void KLocalePrivate::setDayPeriods(const QList<KDayPeriod> &dayPeriods)
{
    if (dayPeriods.count() > 0) {
        foreach (const KDayPeriod &dayPeriod, dayPeriods) {
            if (!dayPeriod.isValid()) {
                return;
            }
        }
        m_dayPeriods = dayPeriods;
    }
}

QList<KDayPeriod> KLocalePrivate::dayPeriods() const
{
    // If no Day Periods currently loaded then it means there were no country specific ones defined
    // in the country l10n file, so default to standard AM/PM translations for the users language.
    // Note we couldn't do this in initDayPeriods() as i18n isn't available until we have a
    // valid loacle constructed.
    if (m_dayPeriods.isEmpty()) {
        m_dayPeriods.append(KDayPeriod(QString::fromLatin1("am"),
                                       i18nc( "Before Noon KLocale::LongName", "Ante Meridiem" ),
                                       i18nc( "Before Noon KLocale::ShortName", "AM" ),
                                       i18nc( "Before Noon KLocale::NarrowName", "A" ),
                                       QTime( 0, 0, 0 ), QTime( 11, 59, 59, 999 ), 0, 12 ));
        m_dayPeriods.append(KDayPeriod(QString::fromLatin1("pm"),
                                       i18nc( "After Noon KLocale::LongName", "Post Meridiem" ),
                                       i18nc( "After Noon KLocale::ShortName", "PM" ),
                                       i18nc( "After Noon KLocale::NarrowName", "P" ),
                                       QTime( 12, 0, 0 ), QTime( 23, 59, 59, 999 ), 0, 12 ));
    }
    return m_dayPeriods;
}

KDayPeriod KLocalePrivate::dayPeriodForTime(const QTime &time) const
{
    if (time.isValid()) {
        foreach (const KDayPeriod &dayPeriod, dayPeriods()) {
            if (dayPeriod.isValid(time)) {
                return dayPeriod;
            }
        }
    }
    return KDayPeriod();
}

QStringList KLocalePrivate::languageList() const
{
    return m_languageList;
}

QStringList KLocalePrivate::currencyCodeList() const
{
    return m_currencyCodeList;
}

QString KLocalePrivate::formatDateTime(const KLocale *locale, const QDateTime &dateTime, KLocale::DateFormat format,
                                       bool includeSeconds, int daysTo, int secsTo)
{
    // Have to do Fancy Date formatting here rather than using normal KCalendarSystem::formatDate()
    // as daysTo is relative to the time spec which formatDate doesn't know about.  Needs to be
    // kept in sync with Fancy Date code in KCalendarSystem::formatDate().  Fix in KDE5.

    // Only do Fancy if less than an hour into the future or less than a week in the past
    if ((daysTo == 0 && secsTo > 3600) ||  daysTo < 0 || daysTo > 6) {
        if (format == KLocale::FancyShortDate) {
            format = KLocale::ShortDate;
        } else if (format == KLocale::FancyLongDate) {
            format = KLocale::LongDate;
        }
    }

    QString dateStr;
    if (format == KLocale::FancyShortDate || format == KLocale::FancyLongDate) {
        switch (daysTo) {
        case 0:
            dateStr = i18n("Today");
            break;
        case 1:
            dateStr = i18n("Yesterday");
            break;
        default:
            dateStr = locale->calendar()->weekDayName(dateTime.date());
        }
    } else {
        dateStr = locale->formatDate(dateTime.date(), format);
    }

    KLocale::TimeFormatOption timeFormat;
    if (includeSeconds) {
        timeFormat = KLocale::TimeDefault;
    } else {
        timeFormat = KLocale::TimeWithoutSeconds;
    }

    return i18nc("concatenation of dates and time", "%1 %2", dateStr,
                 locale->formatLocaleTime(dateTime.time(), timeFormat));
}

QString KLocalePrivate::formatDateTime(const QDateTime &dateTime, KLocale::DateFormat format, bool includeSeconds) const
{
    QDateTime now = QDateTime::currentDateTime();
    int daysTo = dateTime.date().daysTo(now.date());
    int secsTo = now.secsTo(dateTime);
    return KLocalePrivate::formatDateTime(q, dateTime, format, includeSeconds, daysTo, secsTo);
}

QString KLocalePrivate::formatDateTime(const KDateTime &dateTime, KLocale::DateFormat format,
                                       KLocale::DateTimeFormatOptions options)
{
    QString dt;

    if (dateTime.isDateOnly()) {
        dt = formatDate(dateTime.date(), format);
    } else {
        KDateTime now = KDateTime::currentDateTime(dateTime.timeSpec());
        int daysTo = dateTime.date().daysTo(now.date());
        int secsTo = now.secsTo(dateTime);
        dt = KLocalePrivate::formatDateTime(q, dateTime.dateTime(), format, (options & KLocale::Seconds), daysTo, secsTo);
    }

    if (options & KLocale::TimeZone) {
        QString tz;
        switch (dateTime.timeType()) {
        case KDateTime::OffsetFromUTC:
            tz = i18n(dateTime.toString(QString::fromLatin1("%z")).toUtf8());
            break;
        case KDateTime::UTC:
        case KDateTime::TimeZone:
            tz = i18n(dateTime.toString(QString::fromLatin1((format == KLocale::ShortDate) ? "%Z" : "%:Z")).toUtf8());
            break;
        case KDateTime::ClockTime:
        default:
            break;
        }
        return i18nc("concatenation of date/time and time zone", "%1 %2", dt, tz);
    }

    return dt;
}

QString KLocalePrivate::langLookup(const QString &fname, const char *rtype)
{
    QStringList search;

    // assemble the local search paths
    const QStringList localDoc = KGlobal::dirs()->resourceDirs(rtype);

    // look up the different languages
    for (int id = localDoc.count() - 1; id >= 0; --id) {
        QStringList langs = KGlobal::locale()->languageList();
        // FIXME: KDE 4.5, change such that English is not assumed.
        langs.replaceInStrings(QLatin1String("en_US"), QLatin1String("en"));
        langs.append(QLatin1String("en"));
        Q_FOREACH(const QString &lang, langs)
            search.append(QString::fromLatin1("%1%2/%3").arg(localDoc[id]).arg(lang).arg(fname));
    }

    // try to locate the file
    Q_FOREACH(const QString &file, search) {
        kDebug(173) << "Looking for help in: " << file;

        QFileInfo info(file);
        if (info.exists() && info.isFile() && info.isReadable())
            return file;
    }

    return QString();
}

bool KLocalePrivate::useDefaultLanguage() const
{
    return language() == KLocale::defaultLanguage();
}

void KLocalePrivate::initEncoding()
{
    m_codecForEncoding = 0;

    // This all made more sense when we still had the EncodingEnum config key.

    QByteArray codeset = systemCodeset();

    if (!codeset.isEmpty()) {
        QTextCodec* codec = QTextCodec::codecForName(codeset);
        if (codec) {
            setEncoding(codec->mibEnum());
        }
    } else {
        setEncoding(QTextCodec::codecForLocale()->mibEnum());
    }

    if (!m_codecForEncoding) {
        kWarning() << "Cannot resolve system encoding, defaulting to ISO 8859-1.";
        const int mibDefault = 4; // ISO 8859-1
        setEncoding(mibDefault);
    }

    Q_ASSERT(m_codecForEncoding);
}

QByteArray KLocalePrivate::systemCodeset() const
{
    QByteArray codeset;
#if HAVE_LANGINFO_H
    // Qt since 4.2 always returns 'System' as codecForLocale and KDE (for example
    // KEncodingFileDialog) expects real encoding name. So on systems that have langinfo.h use
    // nl_langinfo instead, just like Qt compiled without iconv does. Windows already has its own
    // workaround

    codeset = nl_langinfo(CODESET);

    if ((codeset == "ANSI_X3.4-1968") || (codeset == "US-ASCII")) {
        // means ascii, "C"; QTextCodec doesn't know, so avoid warning
        codeset = "ISO-8859-1";
    }
#endif
    return codeset;
}

void KLocalePrivate::initFileNameEncoding()
{
    // If the following environment variable is set, assume all filenames
    // are in UTF-8 regardless of the current C locale.
    m_utf8FileEncoding = !qgetenv("KDE_UTF8_FILENAMES").isEmpty();
    if (m_utf8FileEncoding) {
        QFile::setEncodingFunction(KLocalePrivate::encodeFileNameUTF8);
        QFile::setDecodingFunction(KLocalePrivate::decodeFileNameUTF8);
    } else {
        const QByteArray ctype = setlocale(LC_CTYPE, 0);
        int indexOfDot = ctype.indexOf('.');
        if (indexOfDot != -1) {
            if (!qstrnicmp(ctype.data() + indexOfDot + 1, "UTF-8", 5)) {
                QFile::setEncodingFunction(KLocalePrivate::encodeFileNameUTF8);
                QFile::setDecodingFunction(KLocalePrivate::decodeFileNameUTF8);
                m_utf8FileEncoding = true;
            }
            return;
        }
        QByteArray lang = qgetenv("LC_ALL");
        if (lang.isEmpty() || lang == "C") {
            lang = qgetenv("LC_CTYPE");
        }
        if (lang.isEmpty() || lang == "C") {
            lang = qgetenv("LANG");
        }
        indexOfDot = lang.indexOf('.');
        if (indexOfDot != -1) {
            if (!qstrnicmp(lang.data() + indexOfDot + 1, "UTF-8", 5)) {
                QFile::setEncodingFunction(KLocalePrivate::encodeFileNameUTF8);
                QFile::setDecodingFunction(KLocalePrivate::decodeFileNameUTF8);
                m_utf8FileEncoding = true;
            }
        }
    }
    // Otherwise, stay with QFile's default filename encoding functions
    // which, on Unix platforms, use the locale's codec.
}

static inline bool isUnicodeNonCharacter(uint ucs4)
{
    return (ucs4 & 0xfffe) == 0xfffe || (ucs4 - 0xfdd0U) < 16;
}

QByteArray KLocalePrivate::encodeFileNameUTF8(const QString & fileName)
{
    if (fileName.isNull()) return QByteArray();
    int len = fileName.length();
    const QChar *uc = fileName.constData();

    uchar replacement = '?';
    int rlen = 3*len;
    int surrogate_high = -1;

    QByteArray rstr;
    rstr.resize(rlen);
    uchar* cursor = (uchar*)rstr.data();
    const QChar *ch = uc;
    int invalid = 0;

    const QChar *end = ch + len;
    while (ch < end) {
        uint u = ch->unicode();
        if (surrogate_high >= 0) {
            if (ch->isLowSurrogate()) {
                u = QChar::surrogateToUcs4(surrogate_high, u);
                surrogate_high = -1;
            } else {
                // high surrogate without low
                *cursor = replacement;
                ++ch;
                ++invalid;
                surrogate_high = -1;
                continue;
            }
        } else if (ch->isLowSurrogate()) {
            // low surrogate without high
            *cursor = replacement;
            ++ch;
            ++invalid;
            continue;
        } else if (ch->isHighSurrogate()) {
            surrogate_high = u;
            ++ch;
            continue;
        }

        if (u >= 0x10FE00 && u <= 0x10FE7F) {
            *cursor++ = (uchar)(u - 0x10FE00 + 128) ;
        }
        else if (u < 0x80) {
            *cursor++ = (uchar)u;
        } else {
            if (u < 0x0800) {
                *cursor++ = 0xc0 | ((uchar) (u >> 6));
            } else {
                // is it one of the Unicode non-characters?
                if (isUnicodeNonCharacter(u)) {
                    *cursor++ = replacement;
                    ++ch;
                    ++invalid;
                    continue;
                }

                if (u > 0xffff) {
                    *cursor++ = 0xf0 | ((uchar) (u >> 18));
                    *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f);
                } else {
                    *cursor++ = 0xe0 | (((uchar) (u >> 12)) & 0x3f);
                }
                *cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f);
            }
            *cursor++ = 0x80 | ((uchar) (u&0x3f));
        }
        ++ch;
    }

    rstr.resize(cursor - (const uchar*)rstr.constData());
    return rstr;
}

QString KLocalePrivate::decodeFileNameUTF8(const QByteArray &localFileName)
{
    const char *chars = localFileName;
    int len = qstrlen(chars);
    int need = 0;
    uint uc = 0;
    uint min_uc = 0;

    QString result(2 * (len + 1), Qt::Uninitialized); // worst case
    ushort *qch = (ushort *)result.unicode();
    uchar ch;

    for (int i = 0; i < len; ++i) {
        ch = chars[i];
        if (need) {
            if ((ch&0xc0) == 0x80) {
                uc = (uc << 6) | (ch & 0x3f);
                --need;
                if (!need) {
                    bool nonCharacter;
                    if (!(nonCharacter = isUnicodeNonCharacter(uc)) && uc > 0xffff && uc < 0x110000) {
                        // surrogate pair
                        Q_ASSERT((qch - (ushort*)result.unicode()) + 2 < result.length());
                        *qch++ = QChar::highSurrogate(uc);
                        *qch++ = QChar::lowSurrogate(uc);
                    } else if ((uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff) || nonCharacter || uc >= 0x110000) {
                        // error: overlong sequence, UTF16 surrogate or non-character
                        goto error;
                    } else {
                        *qch++ = uc;
                    }
                }
            } else {
                goto error;
            }
        } else {
            if (ch < 128) {
                *qch++ = ushort(ch);
            } else if ((ch & 0xe0) == 0xc0) {
                uc = ch & 0x1f;
                need = 1;
                min_uc = 0x80;
            } else if ((ch & 0xf0) == 0xe0) {
                uc = ch & 0x0f;
                need = 2;
                min_uc = 0x800;
            } else if ((ch&0xf8) == 0xf0) {
                uc = ch & 0x07;
                need = 3;
                min_uc = 0x10000;
            } else {
                goto error;
            }
        }
    }
    if (need > 0) {
        // unterminated UTF sequence
        goto error;
    }
    result.truncate(qch - (ushort *)result.unicode());
    return result;

error: 

    qch = (ushort *)result.unicode();
    for (int i = 0; i < len; ++i) {
        ch = chars[i];
        if (ch < 128) {
            *qch++ = ushort(ch);
        } else {
            uint uc = ch - 128 + 0x10FE00; //U+10FE00-U+10FE7F
            *qch++ = QChar::highSurrogate(uc);
            *qch++ = QChar::lowSurrogate(uc);
        }
    }
    result.truncate(qch - (ushort *)result.unicode());
    return result;
}

void KLocalePrivate::setDateFormat(const QString &format)
{
    m_dateFormat = format.trimmed();
}

void KLocalePrivate::setDateFormatShort(const QString &format)
{
    m_dateFormatShort = format.trimmed();
}

void KLocalePrivate::setDateMonthNamePossessive(bool possessive)
{
    m_dateMonthNamePossessive = possessive;
}

void KLocalePrivate::setTimeFormat(const QString &format)
{
    m_timeFormat = format.trimmed();
}

void KLocalePrivate::setWeekStartDay(int day)
{
    if (day >= 1 && day <= calendar()->daysInWeek(QDate())) {
        m_weekStartDay = day;
    }
}

void KLocalePrivate::setWorkingWeekStartDay(int day)
{
    if (day >= 1 && day <= calendar()->daysInWeek(QDate())) {
        m_workingWeekStartDay = day;
    }
}

void KLocalePrivate::setWorkingWeekEndDay(int day)
{
    if (day >= 1 && day <= calendar()->daysInWeek(QDate())) {
        m_workingWeekEndDay = day;
    }
}

void KLocalePrivate::setWeekDayOfPray(int day)
{
    if (day >= 0 && day <= calendar()->daysInWeek(QDate())) { // 0 = None
        m_weekDayOfPray = day;
    }
}

QString KLocalePrivate::dateFormat() const
{
    return m_dateFormat;
}

QString KLocalePrivate::dateFormatShort() const
{
    return m_dateFormatShort;
}

QString KLocalePrivate::timeFormat() const
{
    return m_timeFormat;
}

void KLocalePrivate::setDecimalPlaces(int digits)
{
    m_decimalPlaces = digits;
}

void KLocalePrivate::setDecimalSymbol(const QString &symbol)
{
    m_decimalSymbol = symbol.trimmed();
}

void KLocalePrivate::setThousandsSeparator(const QString &separator)
{
    // allow spaces here
    m_thousandsSeparator = separator;
}

void KLocalePrivate::setNumericDigitGrouping(QList<int> groupList)
{
    m_numericDigitGrouping = groupList;
}

void KLocalePrivate::setPositiveSign(const QString &sign)
{
    m_positiveSign = sign.trimmed();
}

void KLocalePrivate::setNegativeSign(const QString &sign)
{
    m_negativeSign = sign.trimmed();
}

void KLocalePrivate::setPositiveMonetarySignPosition(KLocale::SignPosition signpos)
{
    m_positiveMonetarySignPosition = signpos;
}

void KLocalePrivate::setNegativeMonetarySignPosition(KLocale::SignPosition signpos)
{
    m_negativeMonetarySignPosition = signpos;
}

void KLocalePrivate::setPositivePrefixCurrencySymbol(bool prefix)
{
    m_positivePrefixCurrencySymbol = prefix;
}

void KLocalePrivate::setNegativePrefixCurrencySymbol(bool prefix)
{
    m_negativePrefixCurrencySymbol = prefix;
}

void KLocalePrivate::setMonetaryDecimalPlaces(int digits)
{
    m_monetaryDecimalPlaces = digits;
}

void KLocalePrivate::setMonetaryThousandsSeparator(const QString &separator)
{
    // allow spaces here
    m_monetaryThousandsSeparator = separator;
}

void KLocalePrivate::setMonetaryDigitGrouping(QList<int> groupList)
{
    m_monetaryDigitGrouping = groupList;
}

void KLocalePrivate::setMonetaryDecimalSymbol(const QString &symbol)
{
    m_monetaryDecimalSymbol = symbol.trimmed();
}

void KLocalePrivate::setCurrencySymbol(const QString & symbol)
{
    m_currencySymbol = symbol.trimmed();
}

int KLocalePrivate::pageSize() const
{
    return m_pageSize;
}

void KLocalePrivate::setPageSize(int size)
{
    // #### check if it's in range??
    m_pageSize = size;
}

KLocale::MeasureSystem KLocalePrivate::measureSystem() const
{
    return m_measureSystem;
}

void KLocalePrivate::setMeasureSystem(KLocale::MeasureSystem value)
{
    m_measureSystem = value;
}

QString KLocalePrivate::defaultLanguage()
{
    static const QString en_US = QString::fromLatin1("en_US");
    return en_US;
}

QString KLocalePrivate::defaultCountry()
{
    return QString::fromLatin1("C");
}

QString KLocalePrivate::defaultCurrencyCode()
{
    return QString::fromLatin1("USD");
}

bool KLocalePrivate::useTranscript() const
{
    return m_useTranscript;
}

const QByteArray KLocalePrivate::encoding()
{
    return codecForEncoding()->name();
}

int KLocalePrivate::encodingMib() const
{
    return codecForEncoding()->mibEnum();
}

int KLocalePrivate::fileEncodingMib() const
{
    if (m_utf8FileEncoding) {
        return 106;
    }
    return codecForEncoding()->mibEnum();
}

QTextCodec *KLocalePrivate::codecForEncoding() const
{
    return m_codecForEncoding;
}

bool KLocalePrivate::setEncoding(int mibEnum)
{
    QTextCodec * codec = QTextCodec::codecForMib(mibEnum);
    if (codec) {
        m_codecForEncoding = codec;
    }

    return codec != 0;
}

QStringList KLocalePrivate::allLanguagesList()
{
    if (!m_languages) {
        m_languages = new KConfig(QLatin1String("all_languages"), KConfig::NoGlobals, "locale");
    }
    return m_languages->groupList();
}

QStringList KLocalePrivate::installedLanguages()
{
    QStringList languages;
    QStringList paths = KGlobal::dirs()->findAllResources("locale", QLatin1String("*/entry.desktop"));
    foreach (const QString &path, paths) {
        QString part = path.left(path.length() - 14);
        languages.append(part.mid(part.lastIndexOf(QLatin1Char('/')) + 1));
    }
    languages.sort();
    return languages;
}

QString KLocalePrivate::languageCodeToName(const QString &language)
{
    if (!m_languages) {
        m_languages = new KConfig(QLatin1String("all_languages"), KConfig::NoGlobals, "locale");
    }

    KConfigGroup cg(m_languages, language);
    return cg.readEntry("Name");
}

QStringList KLocalePrivate::allCountriesList() const
{
    QStringList countries;
    const QStringList paths = KGlobal::dirs()->findAllResources("locale", QLatin1String("l10n/*/entry.desktop"));
    for (QStringList::ConstIterator it = paths.begin(); it != paths.end(); ++it) {
        QString code = (*it).mid((*it).length() - 16, 2);
        if (code != QLatin1String("/C")) {
            countries.append(code);
        }
    }
    return countries;
}

QString KLocalePrivate::countryCodeToName(const QString &country) const
{
    QString countryName;
    QString entryFile = KStandardDirs::locate("locale", QString::fromLatin1("l10n/") + country.toLower() + QLatin1String("/entry.desktop"));
    if (!entryFile.isEmpty()) {
        KConfig cfg(entryFile);
        KConfigGroup cg(&cfg, "KCM Locale");
        countryName = cg.readEntry("Name");
    }
    return countryName;
}

KLocale::CalendarSystem KLocalePrivate::calendarTypeToCalendarSystem(const QString &calendarType) const
{
    if (calendarType == QLatin1String("coptic")) {
        return KLocale::CopticCalendar;
    } else if (calendarType == QLatin1String("ethiopian")) {
        return KLocale::EthiopianCalendar;
    } else if (calendarType == QLatin1String("gregorian")) {
        return KLocale::QDateCalendar;
    } else if (calendarType == QLatin1String("gregorian-proleptic")) {
        return KLocale::GregorianCalendar;
    } else if (calendarType == QLatin1String("hebrew")) {
        return KLocale::HebrewCalendar;
    } else if (calendarType == QLatin1String("hijri")) {
        return KLocale::IslamicCivilCalendar;
    } else if (calendarType == QLatin1String("indian-national")) {
        return KLocale::IndianNationalCalendar;
    } else if (calendarType == QLatin1String("jalali")) {
        return KLocale::JalaliCalendar;
    } else if (calendarType == QLatin1String("japanese")) {
        return KLocale::JapaneseCalendar;
    } else if (calendarType == QLatin1String("julian")) {
        return KLocale::JulianCalendar;
    } else if (calendarType == QLatin1String("minguo")) {
        return KLocale::MinguoCalendar;
    } else if (calendarType == QLatin1String("thai")) {
        return KLocale::ThaiCalendar;
    } else {
        return KLocale::QDateCalendar;
    }
}

QString KLocalePrivate::calendarSystemToCalendarType(KLocale::CalendarSystem calendarSystem) const
{
    switch (calendarSystem) {
    case KLocale::QDateCalendar:
        return QLatin1String("gregorian");
    case KLocale::CopticCalendar:
        return QLatin1String("coptic");
    case KLocale::EthiopianCalendar:
        return QLatin1String("ethiopian");
    case KLocale::GregorianCalendar:
        return QLatin1String("gregorian-proleptic");
    case KLocale::HebrewCalendar:
        return QLatin1String("hebrew");
    case KLocale::IslamicCivilCalendar:
        return QLatin1String("hijri");
    case KLocale::IndianNationalCalendar:
        return QLatin1String("indian-national");
    case KLocale::JalaliCalendar:
        return QLatin1String("jalali");
    case KLocale::JapaneseCalendar:
        return QLatin1String("japanese");
    case KLocale::JulianCalendar:
        return QLatin1String("julian");
    case KLocale::MinguoCalendar:
        return QLatin1String("minguo");
    case KLocale::ThaiCalendar:
        return QLatin1String("thai");
    default:
        return QLatin1String("gregorian");
    }
}

void KLocalePrivate::setCalendar(const QString &calendarType)
{
    setCalendarSystem(calendarTypeToCalendarSystem(calendarType));
}

void KLocalePrivate::setCalendarSystem(KLocale::CalendarSystem calendarSystem)
{
    m_calendarSystem = calendarSystem;
    delete m_calendar;
    m_calendar = 0;
}

QString KLocalePrivate::calendarType() const
{
    return calendarSystemToCalendarType(m_calendarSystem);
}

KLocale::CalendarSystem KLocalePrivate::calendarSystem() const
{
    return m_calendarSystem;
}

const KCalendarSystem * KLocalePrivate::calendar()
{
    if (!m_calendar) {
        m_calendar = KCalendarSystem::create(m_calendarSystem, m_config, q);
    }

    return m_calendar;
}

void KLocalePrivate::setWeekNumberSystem(KLocale::WeekNumberSystem weekNumberSystem)
{
    m_weekNumberSystem = weekNumberSystem;
}

KLocale::WeekNumberSystem KLocalePrivate::weekNumberSystem()
{
    return m_weekNumberSystem;
}

void KLocalePrivate::copyCatalogsTo(KLocale *locale)
{
    QMutexLocker lock(kLocaleMutex());
    locale->d->m_catalogNames = m_catalogNames;
    locale->d->updateCatalogs();
}

QString KLocalePrivate::localizedFilePath(const QString &filePath) const
{
    // Stop here if the default language is primary.
    if (useDefaultLanguage()) {
        return filePath;
    }

    // Check if l10n sudir is present, stop if not.
    QFileInfo fileInfo(filePath);
    QString locDirPath = fileInfo.path() + QLatin1String("/l10n");
    QFileInfo locDirInfo(locDirPath);
    if (!locDirInfo.isDir()) {
        return filePath;
    }

    // Go through possible localized paths by priority of languages,
    // return first that exists.
    QString fileName = fileInfo.fileName();
    foreach(const QString &lang, languageList()) {
        // Stop when the default language is reached.
        if (lang == KLocale::defaultLanguage()) {
            return filePath;
        }
        QString locFilePath = locDirPath + QLatin1Char('/') + lang + QLatin1Char('/') + fileName;
        QFileInfo locFileInfo(locFilePath);
        if (locFileInfo.isFile() && locFileInfo.isReadable()) {
            return locFilePath;
        }
    }

    return filePath;
}

QString KLocalePrivate::removeAcceleratorMarker(const QString &label) const
{
    return ::removeAcceleratorMarker(label);
}

void KLocalePrivate::setDigitSet(KLocale::DigitSet digitSet)
{
    m_digitSet = digitSet;
}

KLocale::DigitSet KLocalePrivate::digitSet() const
{
    return m_digitSet;
}

void KLocalePrivate::setMonetaryDigitSet(KLocale::DigitSet digitSet)
{
    m_monetaryDigitSet = digitSet;
}

KLocale::DigitSet KLocalePrivate::monetaryDigitSet() const
{
    return m_monetaryDigitSet;
}

void KLocalePrivate::setDateTimeDigitSet(KLocale::DigitSet digitSet)
{
    m_dateTimeDigitSet = digitSet;
}

KLocale::DigitSet KLocalePrivate::dateTimeDigitSet() const
{
    return m_dateTimeDigitSet;
}

Q_GLOBAL_STATIC_WITH_ARGS(QMutex, s_kLocaleMutex, (QMutex::Recursive))

QMutex *kLocaleMutex()
{
    return s_kLocaleMutex();
}