OpenShot Library | libopenshot-audio  0.2.0
juce::LocalisedStrings Class Reference

Used to convert strings to localised foreign-language versions. More...

#include <juce_LocalisedStrings.h>

Public Member Functions

 LocalisedStrings (const String &fileContents, bool ignoreCaseOfKeys)
 Creates a set of translations from the text of a translation file. More...
 
 LocalisedStrings (const File &fileToLoad, bool ignoreCaseOfKeys)
 Creates a set of translations from a file. More...
 
 LocalisedStrings (const LocalisedStrings &)
 
LocalisedStringsoperator= (const LocalisedStrings &)
 
 ~LocalisedStrings ()
 Destructor. More...
 
String translate (const String &text) const
 Attempts to look up a string and return its localised version. More...
 
String translate (const String &text, const String &resultIfNotFound) const
 Attempts to look up a string and return its localised version. More...
 
String getLanguageName () const
 Returns the name of the language specified in the translation file. More...
 
const StringArraygetCountryCodes () const
 Returns the list of suitable country codes listed in the translation file. More...
 
const StringPairArraygetMappings () const
 Provides access to the actual list of mappings. More...
 
void addStrings (const LocalisedStrings &)
 Adds and merges another set of translations into this set. More...
 
void setFallback (LocalisedStrings *fallbackStrings)
 Gives this object a set of strings to use as a fallback if a string isn't found. More...
 

Static Public Member Functions

static void setCurrentMappings (LocalisedStrings *newTranslations)
 Selects the current set of mappings to be used by the system. More...
 
static LocalisedStringsgetCurrentMappings ()
 Returns the currently selected set of mappings. More...
 
static String translateWithCurrentMappings (const String &text)
 Tries to translate a string using the currently selected set of mappings. More...
 
static String translateWithCurrentMappings (const char *text)
 Tries to translate a string using the currently selected set of mappings. More...
 

Detailed Description

Used to convert strings to localised foreign-language versions.

This is basically a look-up table of strings and their translated equivalents. It can be loaded from a text file, so that you can supply a set of localised versions of strings that you use in your app.

To use it in your code, simply call the translate() method on each string that might have foreign versions, and if none is found, the method will just return the original string.

The translation file should start with some lines specifying a description of the language it contains, and also a list of ISO country codes where it might be appropriate to use the file. After that, each line of the file should contain a pair of quoted strings with an '=' sign.

E.g. for a french translation, the file might be:

language: French
countries: fr be mc ch lu
"hello" = "bonjour"
"goodbye" = "au revoir"

If the strings need to contain a quote character, they can use '"' instead, and if the first non-whitespace character on a line isn't a quote, then it's ignored, (you can use this to add comments).

Note that this is a singleton class, so don't create or destroy the object directly. There's also a TRANS(text) macro defined to make it easy to use the this.

E.g.

printSomething (TRANS("hello"));

This macro is used in the JUCE classes themselves, so your application has a chance to intercept and translate any internal JUCE text strings that might be shown. (You can easily get a list of all the messages by searching for the TRANS() macro in the JUCE source code).

Definition at line 75 of file juce_LocalisedStrings.h.

Constructor & Destructor Documentation

◆ LocalisedStrings() [1/2]

juce::LocalisedStrings::LocalisedStrings ( const String fileContents,
bool  ignoreCaseOfKeys 
)

Creates a set of translations from the text of a translation file.

When you create one of these, you can call setCurrentMappings() to make it the set of mappings that the system's using.

Definition at line 26 of file juce_LocalisedStrings.cpp.

Referenced by LocalisedStrings().

◆ LocalisedStrings() [2/2]

juce::LocalisedStrings::LocalisedStrings ( const File fileToLoad,
bool  ignoreCaseOfKeys 
)

Creates a set of translations from a file.

When you create one of these, you can call setCurrentMappings() to make it the set of mappings that the system's using.

Definition at line 31 of file juce_LocalisedStrings.cpp.

References juce::File::loadFileAsString(), and LocalisedStrings().

◆ ~LocalisedStrings()

juce::LocalisedStrings::~LocalisedStrings ( )

Destructor.

Definition at line 51 of file juce_LocalisedStrings.cpp.

Member Function Documentation

◆ setCurrentMappings()

void juce::LocalisedStrings::setCurrentMappings ( LocalisedStrings newTranslations)
static

Selects the current set of mappings to be used by the system.

The object you pass in will be automatically deleted when no longer needed, so don't keep a pointer to it. You can also pass in nullptr to remove the current mappings.

See also the TRANS() macro, which uses the current set to do its translation.

See also
translateWithCurrentMappings

Definition at line 178 of file juce_LocalisedStrings.cpp.

◆ getCurrentMappings()

LocalisedStrings * juce::LocalisedStrings::getCurrentMappings ( )
static

Returns the currently selected set of mappings.

This is the object that was last passed to setCurrentMappings(). It may be nullptr if none has been created.

Definition at line 184 of file juce_LocalisedStrings.cpp.

Referenced by translateWithCurrentMappings().

◆ translateWithCurrentMappings() [1/2]

String juce::LocalisedStrings::translateWithCurrentMappings ( const String text)
static

Tries to translate a string using the currently selected set of mappings.

If no mapping has been set, or if the mapping doesn't contain a translation for the string, this will just return the original string.

See also the TRANS() macro, which uses this method to do its translation.

See also
setCurrentMappings, getCurrentMappings

Definition at line 189 of file juce_LocalisedStrings.cpp.

◆ translateWithCurrentMappings() [2/2]

String juce::LocalisedStrings::translateWithCurrentMappings ( const char *  text)
static

Tries to translate a string using the currently selected set of mappings.

If no mapping has been set, or if the mapping doesn't contain a translation for the string, this will just return the original string.

See also the TRANS() macro, which uses this method to do its translation.

See also
setCurrentMappings, getCurrentMappings

Definition at line 190 of file juce_LocalisedStrings.cpp.

References getCurrentMappings(), JUCE_API, and translate().

◆ translate() [1/2]

String juce::LocalisedStrings::translate ( const String text) const

Attempts to look up a string and return its localised version.

If the string isn't found in the list, the original string will be returned.

Definition at line 56 of file juce_LocalisedStrings.cpp.

References juce::StringPairArray::containsKey(), and juce::StringPairArray::getValue().

Referenced by translateWithCurrentMappings().

◆ translate() [2/2]

String juce::LocalisedStrings::translate ( const String text,
const String resultIfNotFound 
) const

Attempts to look up a string and return its localised version.

If the string isn't found in the list, the resultIfNotFound string will be returned.

Definition at line 64 of file juce_LocalisedStrings.cpp.

References juce::StringArray::addLines(), juce::StringPairArray::containsKey(), juce::CharPointer_UTF8::getAndAdvance(), juce::String::getCharPointer(), juce::StringPairArray::getValue(), and juce::String::replace().

◆ getLanguageName()

String juce::LocalisedStrings::getLanguageName ( ) const
inline

Returns the name of the language specified in the translation file.

This is specified in the file using a line starting with "language:", e.g.

language: german

Definition at line 159 of file juce_LocalisedStrings.h.

◆ getCountryCodes()

const StringArray& juce::LocalisedStrings::getCountryCodes ( ) const
inline

Returns the list of suitable country codes listed in the translation file.

These is specified in the file using a line starting with "countries:", e.g.

countries: fr be mc ch lu

The country codes are supposed to be 2-character ISO complient codes.

Definition at line 170 of file juce_LocalisedStrings.h.

◆ getMappings()

const StringPairArray& juce::LocalisedStrings::getMappings ( ) const
inline

Provides access to the actual list of mappings.

Definition at line 173 of file juce_LocalisedStrings.h.

References JUCE_API.

◆ addStrings()

void juce::LocalisedStrings::addStrings ( const LocalisedStrings other)

Adds and merges another set of translations into this set.

Note that the language name and country codes of the new LocalisedStrings object must match that of this object - an assertion will be thrown if they don't match.

Any existing values will have their mappings overwritten by the new ones.

Definition at line 164 of file juce_LocalisedStrings.cpp.

◆ setFallback()

void juce::LocalisedStrings::setFallback ( LocalisedStrings fallbackStrings)

Gives this object a set of strings to use as a fallback if a string isn't found.

The object that is passed-in will be owned and deleted by this object when no longer needed. It can be nullptr to clear the existing fallback object.

Definition at line 172 of file juce_LocalisedStrings.cpp.


The documentation for this class was generated from the following files: