File size: 3,903 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { find, get } from 'lodash';
import {
	GP_PROJECT,
	GP_BASE_URL,
	GP_PROJECT_TRANSLATION_SET_SLUGS,
} from 'calypso/lib/i18n-utils/constants';
import { postRequest } from 'calypso/lib/i18n-utils/glotpress';

/**
 * Prepares and triggers a request to get GP string
 * @param {Object} locale and item from `languages` array in config/_shared.json
 * @param {Object} originalStringData GP string information { singular, context, plural }
 * @param {string} apiBaseUrl Base API url to get translations
 * @param {string} project GP project
 * @param {Function} post see postRequest()
 * @returns {Object} request object
 */
export function getSingleTranslationData(
	locale,
	originalStringData,
	apiBaseUrl = GP_BASE_URL + '/api',
	project = GP_PROJECT,
	post = postRequest
) {
	const glotPressUrl = `${ apiBaseUrl }/translations/-query-by-originals`;
	const postFormData = [
		`project=${ project }`,
		`&locale_slug=${ locale.parentLangSlug || locale.langSlug }`,
		`&translation_set_slug=${ GP_PROJECT_TRANSLATION_SET_SLUGS[ locale.langSlug ] || 'default' }`,
		`&original_strings=${ encodeURIComponent( JSON.stringify( [ originalStringData ] ) ) }`,
	];

	return post( glotPressUrl, postFormData.join( '' ) ).then( ( glotPressDataEntries ) =>
		normalizeDetailsFromTranslationData( glotPressDataEntries[ 0 ] )
	);
}

/**
 * Prepares and triggers a request to get GP string
 * @param {string} originalId GP original string id
 * @param {Object} translationObject GP string information { singular, context, plural }
 * @param {Object} locale and item from `languages` array in config/_shared.json
 * @param {string} apiBaseUrl Base API url to get translations
 * @param {string} project GP project
 * @param {Function} post see postRequest()
 * @returns {Object} request object
 */
export function submitTranslation(
	originalId,
	translationObject,
	locale,
	apiBaseUrl = GP_BASE_URL + '/api',
	project = GP_PROJECT,
	post = postRequest
) {
	const glotPressUrl = `${ apiBaseUrl }/translations/-new`;
	const postFormData = [
		`project=${ project }`,
		`&locale_slug=${ locale.parentLangSlug || locale.langSlug }`,
		`&translation_set_slug=${ GP_PROJECT_TRANSLATION_SET_SLUGS[ locale.langSlug ] || 'default' }`,
		...Object.keys( translationObject ).map(
			( key ) =>
				translationObject[ key ] &&
				`&translation[${ originalId }][]=${ encodeURIComponent( translationObject[ key ] ) }`
		),
	];

	return post( glotPressUrl, postFormData.join( '' ) ).then( ( glotPressData ) =>
		normalizeDetailsFromTranslationData( glotPressData )
	);
}

/**
 * Normalizes raw data from GP API
 * @param {Object} glotPressData raw API response
 * @returns {Object} normalized data
 */
export function normalizeDetailsFromTranslationData( glotPressData ) {
	const translationDetails = find( glotPressData.translations, {
		original_id: glotPressData.original_id,
	} );

	return {
		originalId: glotPressData.original_id,
		comment: glotPressData.original_comment,
		translatedSingular: get( translationDetails, 'translation_0', '' ),
		translatedPlural: get( translationDetails, 'translation_1', '' ),
		lastModified: get( translationDetails, 'date_modified', '' ),
	};
}

/**
 * Normalizes raw data from GP API
 * @param {string} originalId GP original string id
 * @param {Object} locale and item from `languages` array in config/_shared.json
 * @param {string} project GP project
 * @returns {string} the permalink to the translation on GlotPress
 */
export function getTranslationPermaLink( originalId, locale, project = GP_PROJECT ) {
	if ( ! originalId || ! locale ) {
		return null;
	}
	const urlBase = GP_BASE_URL + '/projects';
	const localeSlug = locale.parentLangSlug || locale.langSlug;
	const translationSetSlug = GP_PROJECT_TRANSLATION_SET_SLUGS[ locale.langSlug ] || 'default';
	return `${ urlBase }/${ project }/${ localeSlug }/${ translationSetSlug }?filters[original_id]=${ originalId }`;
}