Skip to content

Commit ff6dda9

Browse files
UmairJibranST-DDT
andauthoredDec 14, 2024··
feat(location): add list of spoken languages (#3333)
* add list of spoken languages * remove dupes * add language definition * add language module * add test case for `language()` * autogenerated supporting code * add languages for urdu * add test to make sure the values are truthy * update seed to match new format * update language list slim them down to a few languages as the long list was not easy to build up * update documentation * update returns * language: convert alpha2 to lowercase * update seed flies * covert alpha3 to lowercase * update seeders * update example * update version * use interface for language * Update index.ts * Revert "Update index.ts" This reverts commit 72a18e9. * Update src/modules/location/index.ts Co-authored-by: ST-DDT <ST-DDT@gmx.de> * Update src/modules/location/index.ts Co-authored-by: ST-DDT <ST-DDT@gmx.de> * Update src/definitions/location.ts Co-authored-by: ST-DDT <ST-DDT@gmx.de> * language semantic Co-authored-by: ST-DDT <ST-DDT@gmx.de> * add additional test cases * add examples for each property * add languages for the supported locales * update seeds * use example of german instead of english * Update src/definitions/location.ts Co-authored-by: ST-DDT <ST-DDT@gmx.de> --------- Co-authored-by: ST-DDT <ST-DDT@gmx.de>
1 parent 3c7abb5 commit ff6dda9

File tree

9 files changed

+199
-0
lines changed

9 files changed

+199
-0
lines changed
 

‎src/definitions/location.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Language } from '../modules/location';
12
import type { LocaleEntry } from './definitions';
23

34
/**
@@ -147,4 +148,11 @@ export type LocationDefinition = LocaleEntry<{
147148
* @see [IANA Time Zone Database](https://www.iana.org/time-zones)
148149
*/
149150
time_zone: string[];
151+
152+
/**
153+
* A list of spoken languages.
154+
*
155+
* @see [ISO 639-2 Language Code List](https://www.loc.gov/standards/iso639-2/php/code_list.php)
156+
*/
157+
language: Language[];
150158
}>;

‎src/locales/en/location/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import continent from './continent';
1212
import country from './country';
1313
import county from './county';
1414
import direction from './direction';
15+
import language from './language';
1516
import postcode from './postcode';
1617
import secondary_address from './secondary_address';
1718
import state from './state';
@@ -31,6 +32,7 @@ const location: LocationDefinition = {
3132
country,
3233
county,
3334
direction,
35+
language,
3436
postcode,
3537
secondary_address,
3638
state,

‎src/locales/en/location/language.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
export default [
2+
{ name: 'Afrikaans', alpha2: 'af', alpha3: 'afr' },
3+
{ name: 'Azerbaijani', alpha2: 'az', alpha3: 'aze' },
4+
{ name: 'Maldivian', alpha2: 'dv', alpha3: 'div' },
5+
{ name: 'Farsi/Persian', alpha2: 'fa', alpha3: 'fas' },
6+
{ name: 'Latvian', alpha2: 'lv', alpha3: 'lav' },
7+
{ name: 'Indonesian', alpha2: 'id', alpha3: 'ind' },
8+
{ name: 'Nepali', alpha2: 'ne', alpha3: 'nep' },
9+
{ name: 'Thai', alpha2: 'th', alpha3: 'tha' },
10+
{ name: 'Uzbek', alpha2: 'uz', alpha3: 'uzb' },
11+
{ name: 'Yoruba', alpha2: 'yo', alpha3: 'yor' },
12+
{ name: 'Pashto', alpha2: 'ps', alpha3: 'pus' },
13+
{ name: 'English', alpha2: 'en', alpha3: 'eng' },
14+
{ name: 'Urdu', alpha2: 'ur', alpha3: 'urd' },
15+
{ name: 'German', alpha2: 'de', alpha3: 'deu' },
16+
{ name: 'French', alpha2: 'fr', alpha3: 'fra' },
17+
{ name: 'Spanish', alpha2: 'es', alpha3: 'spa' },
18+
{ name: 'Italian', alpha2: 'it', alpha3: 'ita' },
19+
{ name: 'Dutch', alpha2: 'nl', alpha3: 'nld' },
20+
{ name: 'Russian', alpha2: 'ru', alpha3: 'rus' },
21+
{ name: 'Portuguese', alpha2: 'pt', alpha3: 'por' },
22+
{ name: 'Polish', alpha2: 'pl', alpha3: 'pol' },
23+
{ name: 'Arabic', alpha2: 'ar', alpha3: 'ara' },
24+
{ name: 'Japanese', alpha2: 'ja', alpha3: 'jpn' },
25+
{ name: 'Chinese', alpha2: 'zh', alpha3: 'zho' },
26+
{ name: 'Hindi', alpha2: 'hi', alpha3: 'hin' },
27+
{ name: 'Bengali', alpha2: 'bn', alpha3: 'ben' },
28+
{ name: 'Gujarati', alpha2: 'gu', alpha3: 'guj' },
29+
{ name: 'Tamil', alpha2: 'ta', alpha3: 'tam' },
30+
{ name: 'Telugu', alpha2: 'te', alpha3: 'tel' },
31+
{ name: 'Punjabi', alpha2: 'pa', alpha3: 'pan' },
32+
{ name: 'Vietnamese', alpha2: 'vi', alpha3: 'vie' },
33+
{ name: 'Korean', alpha2: 'ko', alpha3: 'kor' },
34+
{ name: 'Turkish', alpha2: 'tr', alpha3: 'tur' },
35+
{ name: 'Swedish', alpha2: 'sv', alpha3: 'swe' },
36+
{ name: 'Greek', alpha2: 'el', alpha3: 'ell' },
37+
{ name: 'Czech', alpha2: 'cs', alpha3: 'ces' },
38+
{ name: 'Hungarian', alpha2: 'hu', alpha3: 'hun' },
39+
{ name: 'Romanian', alpha2: 'ro', alpha3: 'ron' },
40+
{ name: 'Ukrainian', alpha2: 'uk', alpha3: 'ukr' },
41+
{ name: 'Norwegian', alpha2: 'no', alpha3: 'nor' },
42+
{ name: 'Serbian', alpha2: 'sr', alpha3: 'srp' },
43+
{ name: 'Croatian', alpha2: 'hr', alpha3: 'hrv' },
44+
{ name: 'Slovak', alpha2: 'sk', alpha3: 'slk' },
45+
{ name: 'Slovenian', alpha2: 'sl', alpha3: 'slv' },
46+
{ name: 'Icelandic', alpha2: 'is', alpha3: 'isl' },
47+
{ name: 'Finnish', alpha2: 'fi', alpha3: 'fin' },
48+
{ name: 'Danish', alpha2: 'da', alpha3: 'dan' },
49+
{ name: 'Swahili', alpha2: 'sw', alpha3: 'swa' },
50+
{ name: 'Bashkir', alpha2: 'ba', alpha3: 'bak' },
51+
{ name: 'Basque', alpha2: 'eu', alpha3: 'eus' },
52+
{ name: 'Catalan', alpha2: 'ca', alpha3: 'cat' },
53+
{ name: 'Galician', alpha2: 'gl', alpha3: 'glg' },
54+
{ name: 'Esperanto', alpha2: 'eo', alpha3: 'epo' },
55+
{ name: 'Fijian', alpha2: 'fj', alpha3: 'fij' },
56+
{ name: 'Malagasy', alpha2: 'mg', alpha3: 'mlg' },
57+
{ name: 'Maltese', alpha2: 'mt', alpha3: 'mlt' },
58+
{ name: 'Albanian', alpha2: 'sq', alpha3: 'sqi' },
59+
{ name: 'Armenian', alpha2: 'hy', alpha3: 'hye' },
60+
{ name: 'Georgian', alpha2: 'ka', alpha3: 'kat' },
61+
{ name: 'Macedonian', alpha2: 'mk', alpha3: 'mkd' },
62+
{ name: 'Kazakh', alpha2: 'kk', alpha3: 'kaz' },
63+
{ name: 'Haitian Creole', alpha2: 'ht', alpha3: 'hat' },
64+
{ name: 'Mongolian', alpha2: 'mn', alpha3: 'mon' },
65+
{ name: 'Kyrgyz', alpha2: 'ky', alpha3: 'kir' },
66+
{ name: 'Finnish', alpha2: 'fi', alpha3: 'fin' },
67+
{ name: 'Tagalog', alpha2: 'tl', alpha3: 'tgl' },
68+
{ name: 'Malay', alpha2: 'ms', alpha3: 'msa' },
69+
{ name: 'Tajik', alpha2: 'tg', alpha3: 'tgk' },
70+
{ name: 'Swati', alpha2: 'ss', alpha3: 'ssw' },
71+
{ name: 'Tatar', alpha2: 'tt', alpha3: 'tat' },
72+
{ name: 'Zulu', alpha2: 'zu', alpha3: 'zul' },
73+
];

‎src/locales/ur/location/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import city_prefix from './city_prefix';
1010
import city_suffix from './city_suffix';
1111
import country from './country';
1212
import direction from './direction';
13+
import language from './language';
1314
import postcode from './postcode';
1415
import secondary_address from './secondary_address';
1516
import state from './state';
@@ -26,6 +27,7 @@ const location: LocationDefinition = {
2627
city_suffix,
2728
country,
2829
direction,
30+
language,
2931
postcode,
3032
secondary_address,
3133
state,

‎src/locales/ur/location/language.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default [
2+
{ name: 'پشتو', alpha2: 'ps', alpha3: 'pus' },
3+
{ name: 'اردو', alpha2: 'ur', alpha3: 'urd' },
4+
{ name: 'انگریزی', alpha2: 'en', alpha3: 'eng' },
5+
{ name: 'جرمن', alpha2: 'de', alpha3: 'deu' },
6+
{ name: 'فرانسیسی', alpha2: 'fr', alpha3: 'fra' },
7+
{ name: 'اسپینش', alpha2: 'es', alpha3: 'spa' },
8+
{ name: 'دچ', alpha2: 'nl', alpha3: 'nld' },
9+
{ name: 'روسی', alpha2: 'ru', alpha3: 'rus' },
10+
{ name: 'پرتگالی', alpha2: 'pt', alpha3: 'por' },
11+
{ name: 'پولش', alpha2: 'pl', alpha3: 'pol' },
12+
{ name: 'عربی', alpha2: 'ar', alpha3: 'ara' },
13+
{ name: 'جاپانی', alpha2: 'ja', alpha3: 'jpn' },
14+
{ name: 'چینی', alpha2: 'zh', alpha3: 'zho' },
15+
{ name: 'ہندی', alpha2: 'hi', alpha3: 'hin' },
16+
{ name: 'بنگالی', alpha2: 'bn', alpha3: 'ben' },
17+
{ name: 'تمل', alpha2: 'ta', alpha3: 'tam' },
18+
{ name: 'تلگو', alpha2: 'te', alpha3: 'tel' },
19+
{ name: 'پنجابی', alpha2: 'pa', alpha3: 'pan' },
20+
{ name: 'ترکی', alpha2: 'tr', alpha3: 'tur' },
21+
{ name: 'سویڈش', alpha2: 'sv', alpha3: 'swe' },
22+
{ name: 'یونانی', alpha2: 'el', alpha3: 'ell' },
23+
{ name: 'چیک', alpha2: 'cs', alpha3: 'ces' },
24+
{ name: 'ہنگرین', alpha2: 'hu', alpha3: 'hun' },
25+
{ name: 'نارویجن', alpha2: 'no', alpha3: 'nor' },
26+
{ name: 'کروشیائی', alpha2: 'hr', alpha3: 'hrv' },
27+
{ name: 'سلوواک', alpha2: 'sk', alpha3: 'slk' },
28+
{ name: 'سلووینیائی', alpha2: 'sl', alpha3: 'slv' },
29+
{ name: 'فنش', alpha2: 'fi', alpha3: 'fin' },
30+
{ name: 'دنش', alpha2: 'da', alpha3: 'dan' },
31+
{ name: 'مالٹی', alpha2: 'mt', alpha3: 'mlt' },
32+
{ name: 'تاجک', alpha2: 'tg', alpha3: 'tgk' },
33+
];

‎src/modules/location/index.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import { FakerError } from '../../errors/faker-error';
22
import { ModuleBase } from '../../internal/module-base';
33

4+
/**
5+
* Represents a language with its full name, 2 character ISO 639-1 code, and 3 character ISO 639-2 code.
6+
*/
7+
export interface Language {
8+
/**
9+
* The full name for the language (e.g. `English`).
10+
*/
11+
name: string;
12+
13+
/**
14+
* The 2 character [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) code.
15+
*/
16+
alpha2: string;
17+
18+
/**
19+
* The 3 character [ISO 639-2](https://en.wikipedia.org/wiki/ISO_639-2) code.
20+
*/
21+
alpha3: string;
22+
}
23+
424
/**
525
* Module to generate addresses and locations. Prior to Faker 8.0.0, this module was known as `faker.address`.
626
*
@@ -628,4 +648,25 @@ export class LocationModule extends ModuleBase {
628648
this.faker.definitions.location.time_zone
629649
);
630650
}
651+
652+
/**
653+
* Returns a random spoken language.
654+
*
655+
* @see [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1)
656+
* @see [ISO 639-2](https://en.wikipedia.org/wiki/ISO_639-2)
657+
* @see [ISO 639-2 Language Code List](https://www.loc.gov/standards/iso639-2/php/code_list.php)
658+
*
659+
* @example
660+
* faker.location.language() // { alpha2: 'de', alpha3: 'deu', name: 'German' }
661+
* faker.location.language().name // German
662+
* faker.location.language().alpha2 // de
663+
* faker.location.language().alpha3 // deu
664+
*
665+
* @since 9.4.0
666+
*/
667+
language(): Language {
668+
return this.faker.helpers.arrayElement(
669+
this.faker.definitions.location.language
670+
);
671+
}
631672
}

‎test/modules/__snapshots__/location.spec.ts.snap

+24
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ exports[`location > 42 > direction > noArgs 1`] = `"South"`;
3232

3333
exports[`location > 42 > direction > with abbreviated option 1`] = `"S"`;
3434

35+
exports[`location > 42 > language 1`] = `
36+
{
37+
"alpha2": "gu",
38+
"alpha3": "guj",
39+
"name": "Gujarati",
40+
}
41+
`;
42+
3543
exports[`location > 42 > latitude > noArgs 1`] = `-22.5828`;
3644

3745
exports[`location > 42 > latitude > with max and min option 1`] = `-2.5092`;
@@ -170,6 +178,14 @@ exports[`location > 1211 > direction > noArgs 1`] = `"Southwest"`;
170178

171179
exports[`location > 1211 > direction > with abbreviated option 1`] = `"SW"`;
172180

181+
exports[`location > 1211 > language 1`] = `
182+
{
183+
"alpha2": "tl",
184+
"alpha3": "tgl",
185+
"name": "Tagalog",
186+
}
187+
`;
188+
173189
exports[`location > 1211 > latitude > noArgs 1`] = `77.1337`;
174190

175191
exports[`location > 1211 > latitude > with max and min option 1`] = `8.5704`;
@@ -308,6 +324,14 @@ exports[`location > 1337 > direction > noArgs 1`] = `"South"`;
308324

309325
exports[`location > 1337 > direction > with abbreviated option 1`] = `"S"`;
310326

327+
exports[`location > 1337 > language 1`] = `
328+
{
329+
"alpha2": "ru",
330+
"alpha3": "rus",
331+
"name": "Russian",
332+
}
333+
`;
334+
311335
exports[`location > 1337 > latitude > noArgs 1`] = `-42.8356`;
312336

313337
exports[`location > 1337 > latitude > with max and min option 1`] = `-4.7595`;

‎test/modules/location.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ describe('location', () => {
126126

127127
t.it('timeZone');
128128

129+
t.it('language');
130+
129131
t.describeEach(
130132
'direction',
131133
'cardinalDirection',
@@ -415,6 +417,19 @@ describe('location', () => {
415417
expect(faker.definitions.location.time_zone).toContain(actual);
416418
});
417419
});
420+
421+
describe('language()', () => {
422+
it('should return a random language', () => {
423+
const actual = faker.location.language();
424+
expect(actual.name).toBeTruthy();
425+
expect(actual.alpha2).toBeTruthy();
426+
expect(actual.alpha2).toHaveLength(2);
427+
expect(actual.alpha3).toBeTruthy();
428+
expect(actual.alpha3).toHaveLength(3);
429+
430+
expect(faker.definitions.location.language).toContain(actual);
431+
});
432+
});
418433
}
419434
);
420435
});

‎test/scripts/apidocs/__snapshots__/verify-jsdoc-tags.spec.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ exports[`check docs completeness > all modules and methods are present 1`] = `
288288
"countryCode",
289289
"county",
290290
"direction",
291+
"language",
291292
"latitude",
292293
"longitude",
293294
"nearbyGPSCoordinate",

0 commit comments

Comments
 (0)
Please sign in to comment.