-
-
Save programmerr47/44c11ee8f519d99abe2bcc55a7943705 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
import groovy.json.StringEscapeUtils | |
import groovy.xml.MarkupBuilder | |
File.metaClass.leftShift = { Object text -> | |
delegate.append text | |
delegate.append "\n" | |
return delegate | |
} | |
File.metaClass.writeLn << { Object text -> | |
delegate.write text | |
delegate.append "\n" | |
return delegate | |
} | |
Project project = project | |
class TranslationExtension { | |
private final String s = File.separator | |
Object defaultLang = null | |
File dir | |
TranslationExtension(Project project) { | |
//we put resources into a separate module 'translations' | |
dir = project.rootProject.project("translations").projectDir | |
} | |
String getStrPath(String locale) { | |
final String prefix | |
if (locale == defaultLang.locale) prefix = "" else prefix = "-${locale}" | |
return "src${s}main${s}res${s}values${prefix}${s}translations.xml" | |
} | |
} | |
project.extensions.create("translation", TranslationExtension, project) | |
void generateTranslationClass() { | |
final def defaultLang | |
final Map<String, Map> translations | |
(defaultLang, translations) = new TranslationManager().getAllTranslations() | |
project.translation.defaultLang = defaultLang | |
generateAllLanguageResources(translations) | |
} | |
void generateAllLanguageResources(Map<String, Map> translations) { | |
translations.each { locale, jsonSection -> | |
generateStrResources(locale, jsonSection) | |
} | |
} | |
private void generateStrResources(String locale, Map jsonSection) { | |
def sw = new StringWriter() | |
def xml = new MarkupBuilder(sw) | |
xml.resources() { | |
jsonSection.each { String section, content -> | |
content.each { String key, translation -> | |
if (key != "privacyPolicyText" && key != "termsConditionsText") { | |
string( | |
name: "${section.trim()}_${key.trim()}", | |
//do all your conversions with translation variable here | |
) | |
} | |
} | |
} | |
} | |
def stringsFile = new File(project.translation.dir, project.translation.getStrPath(locale)) | |
stringsFile.parentFile.mkdirs() | |
stringsFile.writeLn "<?xml version=\"1.0\" encoding=\"utf-8\"?>" | |
stringsFile << "<!-- This file is auto-generated, do not make changes in it -->" | |
stringsFile << "" | |
stringsFile << sw.toString() | |
} | |
class TranslationManager { | |
def getAllTranslations() { | |
ArrayList allLanguages = getAllLanguages() | |
HashMap map = new HashMap() | |
def defaultLang | |
allLanguages.each { lang -> | |
if (lang.is_default) defaultLang = lang | |
map[lang.locale] = getLanguage(lang.id, lang.locale) | |
} | |
return [defaultLang, allLanguages, map] | |
} | |
private static ArrayList getAllLanguages() { | |
// Get our json string from the provided url | |
String jsonString = //make your request here (analogue of LangsApi.getLanguages) | |
if (jsonString.isEmpty()) { | |
println("Got no languages, returning empty languages map") | |
return new ArrayList() | |
} | |
// Pull our json data from that json string we get | |
return new JsonSlurper().parseText(jsonString).languages | |
} | |
private static Object getLanguage(int id, String lang) { | |
final String languageDetailUrl = //make your request here (analogue of LangsApi.getTranslations) | |
// Get our json string from the provided url | |
String jsonString = Util.getTextFromUrl(languageDetailUrl, lang) | |
if (jsonString.isEmpty()) { | |
println("Could not fetch language id: " + id) | |
return null | |
} | |
// Pull our json data from that json string we get | |
return new JsonSlurper().parseText(jsonString).data | |
} | |
} | |
task updateTranslation { | |
group = "transifex" | |
doLast { | |
generateTranslationClass() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment