-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add String>>#localized and String>>#localizedWithAll:
Add CurrentLocale dynamic variable
- Loading branch information
Showing
13 changed files
with
375 additions
and
7 deletions.
There are no files selected for viewing
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
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
35 changes: 35 additions & 0 deletions
35
source/Buoy-Localization-GS64-Extensions/NaturalLanguageTranslator.class.st
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
" | ||
A NaturalLanguageTranslator is a dummy translator. | ||
The localization framework is found in the gettext package usually | ||
overriding this class completely. | ||
As an alternative you can register a translator using | ||
NaturalLanguageTranslator current: myTranslator | ||
If this is done the messages will be dispatched to it | ||
" | ||
Class { | ||
#name : 'NaturalLanguageTranslator', | ||
#superclass : 'Object', | ||
#classVars : [ | ||
'Current' | ||
], | ||
#category : 'Buoy-Localization-GS64-Extensions', | ||
#package : 'Buoy-Localization-GS64-Extensions' | ||
} | ||
|
||
{ #category : 'accessing' } | ||
NaturalLanguageTranslator class >> current [ | ||
"Return the currently registered translator" | ||
|
||
^Current | ||
] | ||
|
||
{ #category : 'accessing' } | ||
NaturalLanguageTranslator class >> current: aTranslator [ | ||
"Register a translator that should receiver the translation messages" | ||
|
||
Current := aTranslator | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package { #name : 'Buoy-Localization-GS64-Extensions' } |
15 changes: 15 additions & 0 deletions
15
source/Buoy-Localization-Pharo-Extensions/PolyglotNaturalLanguageTranslator.extension.st
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Extension { #name : 'PolyglotNaturalLanguageTranslator' } | ||
|
||
{ #category : '*Buoy-Localization-Pharo-Extensions' } | ||
PolyglotNaturalLanguageTranslator >> translate: string [ | ||
"Just for Pharo compatibility" | ||
|
||
^ string | ||
] | ||
|
||
{ #category : '*Buoy-Localization-Pharo-Extensions' } | ||
PolyglotNaturalLanguageTranslator >> translate: string toLocale: localeID [ | ||
"Just for Pharo compatibility" | ||
|
||
^ self translate: string | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package { #name : 'Buoy-Localization-Pharo-Extensions' } |
61 changes: 61 additions & 0 deletions
61
source/Buoy-Localization-Tests/PolyglotNaturalLanguageTranslatorTest.class.st
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
" | ||
A PolyglotNaturalLanguageTranslatorTest is a test class for testing the behavior of PolyglotNaturalLanguageTranslator | ||
" | ||
Class { | ||
#name : 'PolyglotNaturalLanguageTranslatorTest', | ||
#superclass : 'TestCase', | ||
#instVars : [ | ||
'translator' | ||
], | ||
#category : 'Buoy-Localization-Tests', | ||
#package : 'Buoy-Localization-Tests' | ||
} | ||
|
||
{ #category : 'running' } | ||
PolyglotNaturalLanguageTranslatorTest >> setUp [ | ||
|
||
super setUp. | ||
translator := PolyglotNaturalLanguageTranslator new | ||
] | ||
|
||
{ #category : 'tests' } | ||
PolyglotNaturalLanguageTranslatorTest >> testLocalizedToLanguageWithTranslation [ | ||
|
||
translator | ||
translationFor: 'Happy new year' in: 'es' is: 'Feliz año nuevo'; | ||
translationFor: 'Happy new year! \u{1F389}' in: 'es' is: 'Feliz año nuevo! \u{1F389}'; | ||
translationFor: 'Happy {1} year' in: 'es' is: 'Feliz año {1}'; | ||
translationFor: 'Happy new year! \u{1F389}' in: 'es-AR' is: '¡Feliz año nuevo! \u{1F389}'. | ||
|
||
self | ||
assert: ( translator localize: 'Happy new year' to: 'es-AR' ) equals: 'Feliz año nuevo'; | ||
assert: ( translator localize: 'Happy new year! \u{1F389}' to: 'es-AR' ) | ||
equals: '¡Feliz año nuevo! 🎉'; | ||
assert: ( translator localize: 'Happy {1} year' withAll: { 2024 } to: 'es-AR' ) | ||
equals: 'Feliz año 2024'. | ||
|
||
self | ||
assert: ( translator localize: 'Happy new year' to: 'es' ) equals: 'Feliz año nuevo'; | ||
assert: ( translator localize: 'Happy new year! \u{1F389}' to: 'es' ) | ||
equals: 'Feliz año nuevo! 🎉'; | ||
assert: ( translator localize: 'Happy {1} year' withAll: { 2024 } to: 'es' ) | ||
equals: 'Feliz año 2024'. | ||
|
||
self | ||
assert: ( translator localize: 'Happy new year' to: 'es-ES' ) equals: 'Feliz año nuevo'; | ||
assert: ( translator localize: 'Happy new year! \u{1F389}' to: 'es-ES' ) | ||
equals: 'Feliz año nuevo! 🎉'; | ||
assert: ( translator localize: 'Happy {1} year' withAll: { 2024 } to: 'es-ES' ) | ||
equals: 'Feliz año 2024' | ||
] | ||
|
||
{ #category : 'tests' } | ||
PolyglotNaturalLanguageTranslatorTest >> testLocalizedToLanguageWithoutTranslations [ | ||
|
||
self | ||
assert: ( translator localize: 'Happy new year' to: 'es-AR' ) equals: 'Happy new year'; | ||
assert: ( translator localize: 'Happy new year! \u{1F389}' to: 'es-AR' ) | ||
equals: 'Happy new year! 🎉'; | ||
assert: ( translator localize: 'Happy {1} year' withAll: { 2024 } to: 'es-AR' ) | ||
equals: 'Happy 2024 year' | ||
] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Class { | ||
#name : 'CurrentLocale', | ||
#superclass : 'DynamicVariable', | ||
#category : 'Buoy-Localization', | ||
#package : 'Buoy-Localization' | ||
} | ||
|
||
{ #category : 'configuring' } | ||
CurrentLocale class >> use: languageTagOrString during: aBlock [ | ||
|
||
^ self value: languageTagOrString asLanguageTag during: aBlock | ||
] | ||
|
||
{ #category : 'accessing' } | ||
CurrentLocale >> default [ | ||
|
||
^ 'en' asLanguageTag | ||
] |
66 changes: 66 additions & 0 deletions
66
source/Buoy-Localization/MonoglotNaturalLanguageTranslator.class.st
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Class { | ||
#name : 'MonoglotNaturalLanguageTranslator', | ||
#superclass : 'Object', | ||
#instVars : [ | ||
'targetLanguageRange', | ||
'translations' | ||
], | ||
#category : 'Buoy-Localization', | ||
#package : 'Buoy-Localization' | ||
} | ||
|
||
{ #category : 'instance creation' } | ||
MonoglotNaturalLanguageTranslator class >> for: aLanguageRange [ | ||
|
||
^ self new initializeFor: aLanguageRange | ||
] | ||
|
||
{ #category : 'private' } | ||
MonoglotNaturalLanguageTranslator >> hashCodeFor: string [ | ||
|
||
^ LanguagePlatform current messageDigest: string | ||
] | ||
|
||
{ #category : 'initialization' } | ||
MonoglotNaturalLanguageTranslator >> initializeFor: aLanguageRange [ | ||
|
||
targetLanguageRange := aLanguageRange. | ||
translations := Dictionary new | ||
] | ||
|
||
{ #category : 'testing' } | ||
MonoglotNaturalLanguageTranslator >> isFor: aLanguageRange [ | ||
|
||
^ targetLanguageRange = aLanguageRange | ||
] | ||
|
||
{ #category : 'testing' } | ||
MonoglotNaturalLanguageTranslator >> matches: aLanguageTag [ | ||
|
||
^ targetLanguageRange matches: aLanguageTag | ||
] | ||
|
||
{ #category : 'accessing' } | ||
MonoglotNaturalLanguageTranslator >> specificity [ | ||
|
||
^targetLanguageRange specificity | ||
|
||
] | ||
|
||
{ #category : 'private' } | ||
MonoglotNaturalLanguageTranslator >> translationAt: hash put: translatedString [ | ||
|
||
translations at: hash put: translatedString | ||
] | ||
|
||
{ #category : 'configuring' } | ||
MonoglotNaturalLanguageTranslator >> translationFor: string is: translatedString [ | ||
|
||
^ self translationAt: ( self hashCodeFor: string unescaped ) put: translatedString unescaped | ||
] | ||
|
||
{ #category : 'localization' } | ||
MonoglotNaturalLanguageTranslator >> withTranslationOf: string do: block [ | ||
|
||
translations at: ( self hashCodeFor: string ) ifPresent: block | ||
] |
62 changes: 62 additions & 0 deletions
62
source/Buoy-Localization/PolyglotNaturalLanguageTranslator.class.st
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Class { | ||
#name : 'PolyglotNaturalLanguageTranslator', | ||
#superclass : 'Object', | ||
#instVars : [ | ||
'translators' | ||
], | ||
#category : 'Buoy-Localization', | ||
#package : 'Buoy-Localization' | ||
} | ||
|
||
{ #category : 'class initialization' } | ||
PolyglotNaturalLanguageTranslator class >> initialize [ | ||
|
||
<ignoreForCoverage> | ||
NaturalLanguageTranslator current: self new | ||
] | ||
|
||
{ #category : 'initialization' } | ||
PolyglotNaturalLanguageTranslator >> initialize [ | ||
|
||
super initialize. | ||
translators := SortedCollection sortBlock: #specificity descending | ||
] | ||
|
||
{ #category : 'localization' } | ||
PolyglotNaturalLanguageTranslator >> localize: string to: stringOrLanguageTag [ | ||
|
||
^ self localize: string withAll: #( ) to: stringOrLanguageTag | ||
] | ||
|
||
{ #category : 'localization' } | ||
PolyglotNaturalLanguageTranslator >> localize: string withAll: collection to: stringOrLanguageTag [ | ||
|
||
| languageTag stringToTranslate | | ||
languageTag := stringOrLanguageTag asLanguageTag. | ||
stringToTranslate := string unescaped. | ||
translators do: [ :translator | | ||
( translator matches: languageTag ) then: [ | ||
translator | ||
withTranslationOf: stringToTranslate | ||
do: [ :translatedString | ^ translatedString format: collection ] | ||
] | ||
]. | ||
^ stringToTranslate format: collection | ||
] | ||
|
||
{ #category : 'configuring' } | ||
PolyglotNaturalLanguageTranslator >> translationFor: string in: targetLanguageRange is: translatedString [ | ||
|
||
^ ( self translatorFor: targetLanguageRange ) translationFor: string is: translatedString | ||
] | ||
|
||
{ #category : 'private' } | ||
PolyglotNaturalLanguageTranslator >> translatorFor: stringOrLanguageRange [ | ||
|
||
| range | | ||
range := stringOrLanguageRange asLanguageRange. | ||
|
||
^ translators | ||
detect: [ :translator | translator isFor: range ] | ||
ifNone: [ translators add: ( MonoglotNaturalLanguageTranslator for: range ) ] | ||
] |
Oops, something went wrong.