Skip to content

Commit

Permalink
Merge pull request #129 from ba-st/translation_tools
Browse files Browse the repository at this point in the history
Translation tools
  • Loading branch information
mtabacman authored Aug 27, 2024
2 parents 5da4215 + 65d8c7a commit 033ddb5
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"
I'm a tool scanning the source code of a project to detect senders of the localization messages to string literals,
so they can be exported and later translated.
Use me with:
```smalltalk
(NaturalLanguageTranslationScanner forProjectNamed: 'ProjectName') export
```
"
Class {
#name : 'NaturalLanguageTranslationScanner',
#superclass : 'Object',
#instVars : [
'targetRepository',
'environment'
],
#category : 'Buoy-Development-Tools-Pharo-12',
#package : 'Buoy-Development-Tools-Pharo-12'
}

{ #category : 'instance creation' }
NaturalLanguageTranslationScanner class >> forProjectNamed: aProjectName [

^ self new initializeForProjectNamed: aProjectName
]

{ #category : 'exporting' }
NaturalLanguageTranslationScanner >> export [

^ self exportTo: targetRepository repositoryDirectory / 'locales' / 'en'
/ ( '<1s>.json' expandMacrosWith: self projectName asLowercase )
]

{ #category : 'exporting' }
NaturalLanguageTranslationScanner >> exportOn: writeStream [

| stringsToTranslate translator translations |
stringsToTranslate := SortedCollection new.
self scanProjectMethodsCollectingTranslationsIn: stringsToTranslate.
translator := MonoglotNaturalLanguageTranslator for: 'en' asLanguageRange.
translations := OrderedDictionary new.
stringsToTranslate do: [ :string |
translations at: ( translator hashCodeFor: string ) hex put: string ].

( NeoJSONWriter on: writeStream )
prettyPrint: true;
nextPut: ( OrderedDictionary new
at: self projectName put: translations;
yourself ).
writeStream lf
]

{ #category : 'exporting' }
NaturalLanguageTranslationScanner >> exportTo: targetFileReference [

targetFileReference
ensureCreateFile;
writeStreamDo: [ :stream | self exportOn: stream ]
]

{ #category : 'initialization' }
NaturalLanguageTranslationScanner >> initializeForProjectNamed: aProjectName [

targetRepository := IceRepository registry
detect: [ :repository | repository name = aProjectName ]
ifNone: [
ObjectNotFound signal:
( 'Missing repository for project named <1s>' expandMacrosWith:
aProjectName )
].
environment := RBBrowserEnvironment new forPackageNames:
( targetRepository project packageNames select: [ :name |
PackageOrganizer default hasPackage: name ] )
]

{ #category : 'accessing' }
NaturalLanguageTranslationScanner >> projectName [

^ targetRepository name
]

{ #category : 'private' }
NaturalLanguageTranslationScanner >> scanProjectMethodsCollectingTranslationsIn: stringsToTranslate [

| searchRule |
searchRule := RBParseTreeLintRule new.
searchRule matcher
matches: '`#string localized'
do: [ :node :answer | stringsToTranslate add: node receiver value ];
matches: '`#string localizedWithAll: `@values'
do: [ :node :answer | stringsToTranslate add: node receiver value ].
searchRule runOnEnvironment: environment
]
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,43 @@ Class {
#package : 'Buoy-Localization-Tests'
}

{ #category : 'private' }
PolyglotNaturalLanguageTranslatorTest >> exportSpanishTranslationsIn: location [

| spanishLocation |
spanishLocation := location / 'locales' / 'es'.
spanishLocation ensureCreateDirectory.

spanishLocation / 'tests.json' writeStreamDo: [ :stream |
stream nextPutAll:
'{"Tests":{"ea0b937ea317101ee2c26b03a4843a19ceced8a2b9673c3cf409a726ca2b0fd8":"Argentina","07f62b021771d3cf67e2e1faf18769cc5e5c119ad7d4d1847a11e11d6d5a7ecb":"Brasil","aa5ab35a9174c2062b7f7697b33fafe5ce404cf5fecf6bfbbf0dc96ba0d90046":"EEUU"}}'
]
]

{ #category : 'running' }
PolyglotNaturalLanguageTranslatorTest >> setUp [

super setUp.
translator := PolyglotNaturalLanguageTranslator new
]

{ #category : 'tests' }
PolyglotNaturalLanguageTranslatorTest >> testLoadJSONTranslationFilesIn [

| tempLocation |
tempLocation := FileLocator temp / 'Buoy' / self selector asString.
tempLocation ensureCreateDirectory.
[
self exportSpanishTranslationsIn: tempLocation.

self assert: ( translator localize: 'Brazil' to: 'es' ) equals: 'Brazil'.

translator loadJSONTranslationFilesIn: tempLocation / 'locales'.
self assert: ( translator localize: 'Brazil' to: 'es' ) equals: 'Brasil'.
self assert: ( translator localize: 'USA' to: 'es' ) equals: 'EEUU'
] ensure: [tempLocation ensureDeleteAll]
]

{ #category : 'tests' }
PolyglotNaturalLanguageTranslatorTest >> testLocalizedToLanguageWithTranslation [

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ PolyglotNaturalLanguageTranslator >> initialize [
translators := SortedCollection sortBlock: [ :a :b | a specificity > b specificity ]
]

{ #category : 'configuring' }
PolyglotNaturalLanguageTranslator >> loadJSONTranslationFilesIn: aLocaleFileReference [
"Loads in the language translator the translation files in the location.
It expectes a directory for each language tag containing any number of JSON
files. "

aLocaleFileReference directories do: [ :localeDirectory |
self loadJSONTranslationsFor: localeDirectory basename asLanguageRange in: localeDirectory ]
]

{ #category : 'private' }
PolyglotNaturalLanguageTranslator >> loadJSONTranslationsFor: languageRange in: localeDirectory [
"Each file has the following structure: a first key level with a group,
and a second level where the key is the translation key and the value is the
translated string."

| monoglotTranslator |
monoglotTranslator := self translatorFor: languageRange.

^ localeDirectory files select: [ :file | file extension = 'json' ] thenDo: [ :file |
| groupedTranslations |
groupedTranslations := STON fromString: file contents.
groupedTranslations keysAndValuesDo: [ :group :translations |
translations keysAndValuesDo: [ :translationKey :translation |
monoglotTranslator
translationAt: ( ByteArray readHexFrom: translationKey )
put: translation ]
]
]
]

{ #category : 'localization' }
PolyglotNaturalLanguageTranslator >> localize: string to: stringOrLanguageTag [

Expand Down

0 comments on commit 033ddb5

Please sign in to comment.